William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Cache management |
| 3 | * |
| 4 | * Copyright 2017 HAProxy Technologies |
| 5 | * William Lallemand <wlallemand@haproxy.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 13 | #include <import/eb32tree.h> |
| 14 | #include <import/sha1.h> |
| 15 | |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 16 | #include <haproxy/action-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 17 | #include <haproxy/api.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 18 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 19 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 20 | #include <haproxy/cli.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 21 | #include <haproxy/errors.h> |
Willy Tarreau | c7babd8 | 2020-06-04 21:29:29 +0200 | [diff] [blame] | 22 | #include <haproxy/filters.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 23 | #include <haproxy/hash.h> |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 24 | #include <haproxy/http.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 25 | #include <haproxy/http_ana.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 26 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 27 | #include <haproxy/http_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/htx.h> |
| 29 | #include <haproxy/net_helper.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 30 | #include <haproxy/proxy.h> |
Willy Tarreau | 334099c | 2020-06-03 18:38:48 +0200 | [diff] [blame] | 31 | #include <haproxy/shctx.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 32 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 33 | #include <haproxy/stream_interface.h> |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 34 | |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 35 | #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] | 36 | * the filter keyword) */ |
Tim Duesterhus | d7c6e6a | 2020-09-14 18:01:33 +0200 | [diff] [blame] | 37 | #define CACHE_FLT_INIT 0x00000002 /* Whether the cache name was freed. */ |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 38 | |
Christopher Faulet | f4a4ef7 | 2018-12-07 17:39:53 +0100 | [diff] [blame] | 39 | const char *cache_store_flt_id = "cache store filter"; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 40 | |
Willy Tarreau | 2231b63 | 2019-03-29 18:26:52 +0100 | [diff] [blame] | 41 | extern struct applet http_cache_applet; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 42 | |
| 43 | struct flt_ops cache_ops; |
| 44 | |
| 45 | struct cache { |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 46 | struct list list; /* cache linked list */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 47 | struct eb_root entries; /* head of cache entries based on keys */ |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 48 | unsigned int maxage; /* max-age */ |
| 49 | unsigned int maxblocks; |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 50 | unsigned int maxobjsz; /* max-object-size (in bytes) */ |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 51 | char id[33]; /* cache name */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 54 | /* cache config for filters */ |
| 55 | struct cache_flt_conf { |
| 56 | union { |
| 57 | struct cache *cache; /* cache used by the filter */ |
| 58 | char *name; /* cache name used during conf parsing */ |
| 59 | } c; |
| 60 | unsigned int flags; /* CACHE_FLT_F_* */ |
| 61 | }; |
| 62 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 63 | /* |
| 64 | * cache ctx for filters |
| 65 | */ |
| 66 | struct cache_st { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 67 | struct shared_block *first_block; |
| 68 | }; |
| 69 | |
| 70 | struct cache_entry { |
| 71 | unsigned int latest_validation; /* latest validation date */ |
| 72 | unsigned int expire; /* expiration date */ |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 73 | unsigned int age; /* Origin server "Age" header value */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 74 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 75 | struct eb32_node eb; /* ebtree node used to hold the cache object */ |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 76 | char hash[20]; |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 77 | |
| 78 | unsigned int etag_length; /* Length of the ETag value (if one was found in the response). */ |
| 79 | unsigned int etag_offset; /* Offset of the ETag value in the data buffer. */ |
| 80 | |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame^] | 81 | time_t last_modified; /* Origin server "Last-Modified" header value converted in |
| 82 | * seconds since epoch. If no "Last-Modified" |
| 83 | * header is found, use "Date" header value, |
| 84 | * otherwise use reception time. This field will |
| 85 | * be used in case of an "If-Modified-Since"-based |
| 86 | * conditional request. */ |
| 87 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 88 | unsigned char data[0]; |
| 89 | }; |
| 90 | |
| 91 | #define CACHE_BLOCKSIZE 1024 |
Willy Tarreau | 96062a1 | 2018-11-11 14:00:28 +0100 | [diff] [blame] | 92 | #define CACHE_ENTRY_MAX_AGE 2147483648U |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 93 | |
| 94 | static struct list caches = LIST_HEAD_INIT(caches); |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 95 | 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] | 96 | static struct cache *tmp_cache_config = NULL; |
| 97 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 98 | DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st)); |
| 99 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 100 | struct cache_entry *entry_exist(struct cache *cache, char *hash) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 101 | { |
| 102 | struct eb32_node *node; |
| 103 | struct cache_entry *entry; |
| 104 | |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 105 | node = eb32_lookup(&cache->entries, read_u32(hash)); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 106 | if (!node) |
| 107 | return NULL; |
| 108 | |
| 109 | entry = eb32_entry(node, struct cache_entry, eb); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 110 | |
| 111 | /* if that's not the right node */ |
| 112 | if (memcmp(entry->hash, hash, sizeof(entry->hash))) |
| 113 | return NULL; |
| 114 | |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 115 | if (entry->expire > now.tv_sec) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 116 | return entry; |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 117 | } else { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 118 | eb32_delete(node); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 119 | entry->eb.key = 0; |
| 120 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 121 | return NULL; |
| 122 | |
| 123 | } |
| 124 | |
| 125 | static inline struct shared_context *shctx_ptr(struct cache *cache) |
| 126 | { |
| 127 | return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data); |
| 128 | } |
| 129 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 130 | static inline struct shared_block *block_ptr(struct cache_entry *entry) |
| 131 | { |
| 132 | return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 137 | static int |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 138 | cache_store_init(struct proxy *px, struct flt_conf *fconf) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 139 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 140 | fconf->flags |= FLT_CFG_FL_HTX; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 144 | static void |
| 145 | cache_store_deinit(struct proxy *px, struct flt_conf *fconf) |
| 146 | { |
| 147 | struct cache_flt_conf *cconf = fconf->conf; |
| 148 | |
Tim Duesterhus | d7c6e6a | 2020-09-14 18:01:33 +0200 | [diff] [blame] | 149 | if (!(cconf->flags & CACHE_FLT_INIT)) |
| 150 | free(cconf->c.name); |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 151 | free(cconf); |
| 152 | } |
| 153 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 154 | static int |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 155 | cache_store_check(struct proxy *px, struct flt_conf *fconf) |
| 156 | { |
| 157 | struct cache_flt_conf *cconf = fconf->conf; |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 158 | struct flt_conf *f; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 159 | struct cache *cache; |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 160 | int comp = 0; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 161 | |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 162 | /* Find the cache corresponding to the name in the filter config. The |
| 163 | * cache will not be referenced now in the filter config because it is |
| 164 | * not fully allocated. This step will be performed during the cache |
| 165 | * post_check. |
| 166 | */ |
| 167 | list_for_each_entry(cache, &caches_config, list) { |
| 168 | if (!strcmp(cache->id, cconf->c.name)) |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 169 | goto found; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n", |
| 173 | proxy_type_str(px), px->id, (char *)cconf->c.name); |
| 174 | return 1; |
| 175 | |
| 176 | found: |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 177 | /* Here <cache> points on the cache the filter must use and <cconf> |
| 178 | * points on the cache filter configuration. */ |
| 179 | |
| 180 | /* Check all filters for proxy <px> to know if the compression is |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 181 | * enabled and if it is after the cache. When the compression is before |
| 182 | * the cache, an error is returned. Also check if the cache filter must |
| 183 | * be explicitly declaired or not. */ |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 184 | list_for_each_entry(f, &px->filter_configs, list) { |
| 185 | if (f == fconf) { |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 186 | /* The compression filter must be evaluated after the cache. */ |
| 187 | if (comp) { |
| 188 | ha_alert("config: %s '%s': unable to enable the compression filter before " |
| 189 | "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id); |
| 190 | return 1; |
| 191 | } |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 192 | } |
Christopher Faulet | 8f7fe1c | 2019-07-15 15:08:25 +0200 | [diff] [blame] | 193 | else if (f->id == http_comp_flt_id) |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 194 | comp = 1; |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 195 | else if (f->id == fcgi_flt_id) |
| 196 | continue; |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 197 | else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) { |
| 198 | /* Implicit declaration is only allowed with the |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 199 | * compression and fcgi. For other filters, an implicit |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 200 | * declaration is required. */ |
| 201 | ha_alert("config: %s '%s': require an explicit filter declaration " |
| 202 | "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id); |
| 203 | return 1; |
| 204 | } |
| 205 | |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 206 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static int |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 211 | cache_store_strm_init(struct stream *s, struct filter *filter) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 212 | { |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 213 | struct cache_st *st; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 214 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 215 | st = pool_alloc_dirty(pool_head_cache_st); |
| 216 | if (st == NULL) |
| 217 | return -1; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 218 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 219 | st->first_block = NULL; |
| 220 | filter->ctx = st; |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 221 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 222 | /* Register post-analyzer on AN_RES_WAIT_HTTP */ |
| 223 | filter->post_analyzers |= AN_RES_WAIT_HTTP; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 224 | return 1; |
| 225 | } |
| 226 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 227 | static void |
| 228 | cache_store_strm_deinit(struct stream *s, struct filter *filter) |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 229 | { |
| 230 | struct cache_st *st = filter->ctx; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 231 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 232 | struct cache *cache = cconf->c.cache; |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 233 | struct shared_context *shctx = shctx_ptr(cache); |
| 234 | |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 235 | /* Everything should be released in the http_end filter, but we need to do it |
| 236 | * there too, in case of errors */ |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 237 | if (st && st->first_block) { |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 238 | shctx_lock(shctx); |
| 239 | shctx_row_dec_hot(shctx, st->first_block); |
| 240 | shctx_unlock(shctx); |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 241 | } |
| 242 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 243 | pool_free(pool_head_cache_st, st); |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 244 | filter->ctx = NULL; |
| 245 | } |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 248 | static int |
| 249 | cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn, |
| 250 | unsigned an_bit) |
| 251 | { |
| 252 | struct http_txn *txn = s->txn; |
| 253 | struct http_msg *msg = &txn->rsp; |
| 254 | struct cache_st *st = filter->ctx; |
| 255 | |
| 256 | if (an_bit != AN_RES_WAIT_HTTP) |
| 257 | goto end; |
| 258 | |
| 259 | /* Here we need to check if any compression filter precedes the cache |
| 260 | * filter. This is only possible when the compression is configured in |
| 261 | * the frontend while the cache filter is configured on the |
| 262 | * backend. This case cannot be detected during HAProxy startup. So in |
| 263 | * such cases, the cache is disabled. |
| 264 | */ |
| 265 | if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) { |
| 266 | pool_free(pool_head_cache_st, st); |
| 267 | filter->ctx = NULL; |
| 268 | } |
| 269 | |
| 270 | end: |
| 271 | return 1; |
| 272 | } |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 273 | |
| 274 | static int |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 275 | cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 276 | { |
| 277 | struct cache_st *st = filter->ctx; |
| 278 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 279 | if (!(msg->chn->flags & CF_ISRESP) || !st) |
| 280 | return 1; |
| 281 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 282 | if (st->first_block) |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame] | 283 | register_data_filter(s, msg->chn, filter); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 284 | return 1; |
| 285 | } |
| 286 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 287 | static inline void disable_cache_entry(struct cache_st *st, |
| 288 | struct filter *filter, struct shared_context *shctx) |
| 289 | { |
| 290 | struct cache_entry *object; |
| 291 | |
| 292 | object = (struct cache_entry *)st->first_block->data; |
| 293 | filter->ctx = NULL; /* disable cache */ |
| 294 | shctx_lock(shctx); |
| 295 | shctx_row_dec_hot(shctx, st->first_block); |
| 296 | object->eb.key = 0; |
| 297 | shctx_unlock(shctx); |
| 298 | pool_free(pool_head_cache_st, st); |
| 299 | } |
| 300 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 301 | static int |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 302 | cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 303 | unsigned int offset, unsigned int len) |
| 304 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 305 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 306 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 307 | struct cache_st *st = filter->ctx; |
| 308 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 309 | struct htx_blk *blk; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 310 | struct shared_block *fb; |
Christopher Faulet | 497c759 | 2020-03-02 16:19:50 +0100 | [diff] [blame] | 311 | struct htx_ret htxret; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 312 | unsigned int orig_len, to_forward; |
| 313 | int ret; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 314 | |
| 315 | if (!len) |
| 316 | return len; |
| 317 | |
| 318 | if (!st->first_block) { |
| 319 | unregister_data_filter(s, msg->chn, filter); |
| 320 | return len; |
| 321 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 322 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 323 | chunk_reset(&trash); |
| 324 | orig_len = len; |
| 325 | to_forward = 0; |
Christopher Faulet | 497c759 | 2020-03-02 16:19:50 +0100 | [diff] [blame] | 326 | |
| 327 | htxret = htx_find_offset(htx, offset); |
| 328 | blk = htxret.blk; |
| 329 | offset = htxret.ret; |
| 330 | for (; blk && len; blk = htx_get_next_blk(htx, blk)) { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 331 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 332 | uint32_t info, sz = htx_get_blksz(blk); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 333 | struct ist v; |
| 334 | |
| 335 | switch (type) { |
| 336 | case HTX_BLK_UNUSED: |
| 337 | break; |
| 338 | |
| 339 | case HTX_BLK_DATA: |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 340 | v = htx_get_blk_value(htx, blk); |
| 341 | v.ptr += offset; |
| 342 | v.len -= offset; |
| 343 | if (v.len > len) |
| 344 | v.len = len; |
| 345 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 346 | info = (type << 28) + v.len; |
| 347 | chunk_memcat(&trash, (char *)&info, sizeof(info)); |
| 348 | chunk_memcat(&trash, v.ptr, v.len); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 349 | to_forward += v.len; |
| 350 | len -= v.len; |
| 351 | break; |
| 352 | |
| 353 | default: |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 354 | /* Here offset must always be 0 because only |
| 355 | * DATA blocks can be partially transferred. */ |
| 356 | if (offset) |
| 357 | goto no_cache; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 358 | if (sz > len) |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 359 | goto end; |
| 360 | |
| 361 | chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info)); |
| 362 | chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 363 | to_forward += sz; |
| 364 | len -= sz; |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | offset = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 369 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 370 | |
| 371 | end: |
| 372 | shctx_lock(shctx); |
| 373 | fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data); |
| 374 | if (!fb) { |
| 375 | shctx_unlock(shctx); |
| 376 | goto no_cache; |
| 377 | } |
| 378 | shctx_unlock(shctx); |
| 379 | |
| 380 | ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append, |
| 381 | (unsigned char *)b_head(&trash), b_data(&trash)); |
| 382 | if (ret < 0) |
| 383 | goto no_cache; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 384 | |
| 385 | return to_forward; |
| 386 | |
| 387 | no_cache: |
| 388 | disable_cache_entry(st, filter, shctx); |
| 389 | unregister_data_filter(s, msg->chn, filter); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 390 | return orig_len; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static int |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 394 | cache_store_http_end(struct stream *s, struct filter *filter, |
| 395 | struct http_msg *msg) |
| 396 | { |
| 397 | struct cache_st *st = filter->ctx; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 398 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 399 | struct cache *cache = cconf->c.cache; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 400 | struct shared_context *shctx = shctx_ptr(cache); |
| 401 | struct cache_entry *object; |
| 402 | |
| 403 | if (!(msg->chn->flags & CF_ISRESP)) |
| 404 | return 1; |
| 405 | |
| 406 | if (st && st->first_block) { |
| 407 | |
| 408 | object = (struct cache_entry *)st->first_block->data; |
| 409 | |
| 410 | /* does not need to test if the insertion worked, if it |
| 411 | * doesn't, the blocks will be reused anyway */ |
| 412 | |
| 413 | shctx_lock(shctx); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 414 | if (eb32_insert(&cache->entries, &object->eb) != &object->eb) { |
| 415 | object->eb.key = 0; |
| 416 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 417 | /* remove from the hotlist */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 418 | shctx_row_dec_hot(shctx, st->first_block); |
| 419 | shctx_unlock(shctx); |
| 420 | |
| 421 | } |
| 422 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 423 | pool_free(pool_head_cache_st, st); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 424 | filter->ctx = NULL; |
| 425 | } |
| 426 | |
| 427 | return 1; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * This intends to be used when checking HTTP headers for some |
| 432 | * word=value directive. Return a pointer to the first character of value, if |
| 433 | * the word was not found or if there wasn't any value assigned ot it return NULL |
| 434 | */ |
| 435 | char *directive_value(const char *sample, int slen, const char *word, int wlen) |
| 436 | { |
| 437 | int st = 0; |
| 438 | |
| 439 | if (slen < wlen) |
| 440 | return 0; |
| 441 | |
| 442 | while (wlen) { |
| 443 | char c = *sample ^ *word; |
| 444 | if (c && c != ('A' ^ 'a')) |
| 445 | return NULL; |
| 446 | sample++; |
| 447 | word++; |
| 448 | slen--; |
| 449 | wlen--; |
| 450 | } |
| 451 | |
| 452 | while (slen) { |
| 453 | if (st == 0) { |
| 454 | if (*sample != '=') |
| 455 | return NULL; |
| 456 | sample++; |
| 457 | slen--; |
| 458 | st = 1; |
| 459 | continue; |
| 460 | } else { |
| 461 | return (char *)sample; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Return the maxage in seconds of an HTTP response. |
| 470 | * Compute the maxage using either: |
| 471 | * - the assigned max-age of the cache |
| 472 | * - the s-maxage directive |
| 473 | * - the max-age directive |
| 474 | * - (Expires - Data) headers |
| 475 | * - the default-max-age of the cache |
| 476 | * |
| 477 | */ |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 478 | int http_calc_maxage(struct stream *s, struct cache *cache) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 479 | { |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 480 | struct htx *htx = htxbuf(&s->res.buf); |
| 481 | struct http_hdr_ctx ctx = { .blk = NULL }; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 482 | int smaxage = -1; |
| 483 | int maxage = -1; |
| 484 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 485 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
| 486 | char *value; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 487 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 488 | value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8); |
| 489 | if (value) { |
| 490 | struct buffer *chk = get_trash_chunk(); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 491 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 492 | chunk_strncat(chk, value, ctx.value.len - 8 + 1); |
| 493 | chunk_strncat(chk, "", 1); |
| 494 | maxage = atoi(chk->area); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 495 | } |
| 496 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 497 | value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7); |
| 498 | if (value) { |
| 499 | struct buffer *chk = get_trash_chunk(); |
Christopher Faulet | 5f2c49f | 2019-07-15 20:49:46 +0200 | [diff] [blame] | 500 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 501 | chunk_strncat(chk, value, ctx.value.len - 7 + 1); |
| 502 | chunk_strncat(chk, "", 1); |
| 503 | smaxage = atoi(chk->area); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
| 507 | /* TODO: Expires - Data */ |
| 508 | |
| 509 | |
| 510 | if (smaxage > 0) |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 511 | return MIN(smaxage, cache->maxage); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 512 | |
| 513 | if (maxage > 0) |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 514 | return MIN(maxage, cache->maxage); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 515 | |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 516 | return cache->maxage; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 517 | |
| 518 | } |
| 519 | |
| 520 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 521 | static void cache_free_blocks(struct shared_block *first, struct shared_block *block) |
| 522 | { |
Willy Tarreau | 5bd37fa | 2018-04-04 20:17:03 +0200 | [diff] [blame] | 523 | struct cache_entry *object = (struct cache_entry *)block->data; |
| 524 | |
| 525 | if (first == block && object->eb.key) |
| 526 | eb32_delete(&object->eb); |
| 527 | object->eb.key = 0; |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 528 | } |
| 529 | |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame^] | 530 | |
| 531 | /* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the |
| 532 | * date value should be compared to a date determined by in a previous response (for |
| 533 | * the same entity). This date could either be the "Last-Modified" value, or the "Date" |
| 534 | * value of the response's reception time (by decreasing order of priority). */ |
| 535 | static time_t get_last_modified_time(struct htx *htx) |
| 536 | { |
| 537 | time_t last_modified = 0; |
| 538 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 539 | struct tm tm = {}; |
| 540 | |
| 541 | if (http_find_header(htx, ist("last-modified"), &ctx, 1)) { |
| 542 | if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) { |
| 543 | last_modified = my_timegm(&tm); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | if (!last_modified) { |
| 548 | ctx.blk = NULL; |
| 549 | if (http_find_header(htx, ist("date"), &ctx, 1)) { |
| 550 | if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) { |
| 551 | last_modified = my_timegm(&tm); |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /* Fallback on the current time if no "Last-Modified" or "Date" header |
| 557 | * was found. */ |
| 558 | if (!last_modified) |
| 559 | last_modified = now.tv_sec; |
| 560 | |
| 561 | return last_modified; |
| 562 | } |
| 563 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 564 | /* |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 565 | * 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] | 566 | * register a filter to store the data |
| 567 | */ |
| 568 | 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] | 569 | struct session *sess, struct stream *s, int flags) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 570 | { |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 571 | unsigned int age; |
| 572 | long long hdr_age; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 573 | struct http_txn *txn = s->txn; |
| 574 | struct http_msg *msg = &txn->rsp; |
| 575 | struct filter *filter; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 576 | struct shared_block *first = NULL; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 577 | struct cache_flt_conf *cconf = rule->arg.act.p[0]; |
| 578 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 579 | struct cache_st *cache_ctx = NULL; |
| 580 | struct cache_entry *object, *old; |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 581 | unsigned int key = read_u32(txn->cache_hash); |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 582 | struct htx *htx; |
| 583 | struct http_hdr_ctx ctx; |
Christopher Faulet | b066747 | 2019-09-03 22:22:12 +0200 | [diff] [blame] | 584 | size_t hdrs_len = 0; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 585 | int32_t pos; |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 586 | unsigned int etag_length = 0; |
| 587 | unsigned int etag_offset = 0; |
| 588 | struct ist header_name = IST_NULL; |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame^] | 589 | time_t last_modified = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 590 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 591 | /* Don't cache if the response came from a cache */ |
| 592 | if ((obj_type(s->target) == OBJ_TYPE_APPLET) && |
| 593 | s->target == &http_cache_applet.obj_type) { |
| 594 | goto out; |
| 595 | } |
| 596 | |
| 597 | /* cache only HTTP/1.1 */ |
| 598 | if (!(txn->req.flags & HTTP_MSGF_VER_11)) |
| 599 | goto out; |
| 600 | |
Willy Tarreau | 6905d18 | 2019-10-01 17:59:17 +0200 | [diff] [blame] | 601 | /* cache only GET method */ |
| 602 | if (txn->meth != HTTP_METH_GET) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 603 | goto out; |
| 604 | |
Willy Tarreau | c9036c0 | 2019-01-11 19:38:25 +0100 | [diff] [blame] | 605 | /* cache key was not computed */ |
| 606 | if (!key) |
| 607 | goto out; |
| 608 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 609 | /* cache only 200 status code */ |
| 610 | if (txn->status != 200) |
| 611 | goto out; |
| 612 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 613 | /* Find the corresponding filter instance for the current stream */ |
| 614 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 615 | if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) { |
| 616 | /* No filter ctx, don't cache anything */ |
| 617 | if (!filter->ctx) |
| 618 | goto out; |
| 619 | cache_ctx = filter->ctx; |
| 620 | break; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | /* from there, cache_ctx is always defined */ |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 625 | htx = htxbuf(&s->res.buf); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 626 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 627 | /* Do not cache too big objects. */ |
| 628 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 && |
| 629 | htx->data + htx->extra > shctx->max_obj_size) |
| 630 | goto out; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 631 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 632 | /* Does not manage Vary at the moment. We will need a secondary key later for that */ |
| 633 | ctx.blk = NULL; |
| 634 | if (http_find_header(htx, ist("Vary"), &ctx, 0)) |
| 635 | goto out; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 636 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 637 | http_check_response_for_cacheability(s, &s->res); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 638 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 639 | if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK)) |
| 640 | goto out; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 641 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 642 | age = 0; |
| 643 | ctx.blk = NULL; |
| 644 | if (http_find_header(htx, ist("Age"), &ctx, 0)) { |
| 645 | if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) { |
| 646 | if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE)) |
| 647 | hdr_age = CACHE_ENTRY_MAX_AGE; |
| 648 | age = hdr_age; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 649 | } |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 650 | http_remove_header(htx, &ctx); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 651 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 652 | |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame^] | 653 | /* Build a last-modified time that will be stored in the cache_entry and |
| 654 | * compared to a future If-Modified-Since client header. */ |
| 655 | last_modified = get_last_modified_time(htx); |
| 656 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 657 | chunk_reset(&trash); |
| 658 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 659 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 660 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 661 | uint32_t sz = htx_get_blksz(blk); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 662 | |
Christopher Faulet | b066747 | 2019-09-03 22:22:12 +0200 | [diff] [blame] | 663 | hdrs_len += sizeof(*blk) + sz; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 664 | chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info)); |
| 665 | chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz); |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 666 | |
| 667 | /* Look for optional ETag header. |
| 668 | * We need to store the offset of the ETag value in order for |
| 669 | * future conditional requests to be able to perform ETag |
| 670 | * comparisons. */ |
| 671 | if (type == HTX_BLK_HDR) { |
| 672 | header_name = htx_get_blk_name(htx, blk); |
| 673 | if (isteq(header_name, ist("etag"))) { |
| 674 | etag_length = sz - istlen(header_name); |
| 675 | etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name); |
| 676 | } |
| 677 | } |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 678 | if (type == HTX_BLK_EOH) |
| 679 | break; |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 680 | } |
| 681 | |
Christopher Faulet | b066747 | 2019-09-03 22:22:12 +0200 | [diff] [blame] | 682 | /* Do not cache objects if the headers are too big. */ |
| 683 | if (hdrs_len > htx->size - global.tune.maxrewrite) |
| 684 | goto out; |
| 685 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 686 | shctx_lock(shctx); |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 687 | first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 688 | if (!first) { |
| 689 | shctx_unlock(shctx); |
| 690 | goto out; |
| 691 | } |
| 692 | shctx_unlock(shctx); |
| 693 | |
Willy Tarreau | 1093a45 | 2018-04-06 19:02:25 +0200 | [diff] [blame] | 694 | /* the received memory is not initialized, we need at least to mark |
| 695 | * the object as not indexed yet. |
| 696 | */ |
| 697 | object = (struct cache_entry *)first->data; |
| 698 | object->eb.node.leaf_p = NULL; |
| 699 | object->eb.key = 0; |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 700 | object->age = age; |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame^] | 701 | object->last_modified = last_modified; |
Willy Tarreau | 1093a45 | 2018-04-06 19:02:25 +0200 | [diff] [blame] | 702 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 703 | /* reserve space for the cache_entry structure */ |
| 704 | first->len = sizeof(struct cache_entry); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 705 | first->last_append = NULL; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 706 | /* cache the headers in a http action because it allows to chose what |
| 707 | * to cache, for example you might want to cache a response before |
| 708 | * modifying some HTTP headers, or on the contrary after modifying |
| 709 | * those headers. |
| 710 | */ |
| 711 | |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 712 | /* Write the ETag information in the cache_entry if needed. */ |
| 713 | object->etag_length = etag_length; |
| 714 | object->etag_offset = etag_offset; |
| 715 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 716 | /* does not need to be locked because it's in the "hot" list, |
| 717 | * copy the headers */ |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 718 | if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0) |
| 719 | goto out; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 720 | |
| 721 | /* register the buffer in the filter ctx for filling it with data*/ |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 722 | if (cache_ctx) { |
| 723 | cache_ctx->first_block = first; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 724 | |
Willy Tarreau | c9036c0 | 2019-01-11 19:38:25 +0100 | [diff] [blame] | 725 | object->eb.key = key; |
| 726 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 727 | memcpy(object->hash, txn->cache_hash, sizeof(object->hash)); |
| 728 | /* Insert the node later on caching success */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 729 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 730 | shctx_lock(shctx); |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 731 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 732 | old = entry_exist(cconf->c.cache, txn->cache_hash); |
| 733 | if (old) { |
| 734 | eb32_delete(&old->eb); |
| 735 | old->eb.key = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 736 | } |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 737 | shctx_unlock(shctx); |
| 738 | |
| 739 | /* store latest value and expiration time */ |
| 740 | object->latest_validation = now.tv_sec; |
| 741 | object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache); |
| 742 | return ACT_RET_CONT; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | out: |
| 746 | /* if does not cache */ |
| 747 | if (first) { |
| 748 | shctx_lock(shctx); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 749 | first->len = 0; |
| 750 | object->eb.key = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 751 | shctx_row_dec_hot(shctx, first); |
| 752 | shctx_unlock(shctx); |
| 753 | } |
| 754 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 755 | return ACT_RET_CONT; |
| 756 | } |
| 757 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 758 | #define HTX_CACHE_INIT 0 /* Initial state. */ |
| 759 | #define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */ |
| 760 | #define HTX_CACHE_DATA 2 /* Cache entry data forwarding */ |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 761 | #define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */ |
| 762 | #define HTX_CACHE_END 4 /* Cache entry treatment terminated */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 763 | |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 764 | static void http_cache_applet_release(struct appctx *appctx) |
| 765 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 766 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 767 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 768 | struct cache *cache = cconf->c.cache; |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 769 | struct shared_block *first = block_ptr(cache_ptr); |
| 770 | |
| 771 | shctx_lock(shctx_ptr(cache)); |
| 772 | shctx_row_dec_hot(shctx_ptr(cache), first); |
| 773 | shctx_unlock(shctx_ptr(cache)); |
| 774 | } |
| 775 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 776 | |
| 777 | static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type, |
| 778 | uint32_t info, struct shared_block *shblk, unsigned int offset) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 779 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 780 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 781 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 782 | struct htx_blk *blk; |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 783 | char *ptr; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 784 | unsigned int max, total; |
| 785 | uint32_t blksz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 786 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 787 | max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx)); |
| 788 | if (!max) |
| 789 | return 0; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 790 | blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR) |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 791 | ? (info & 0xff) + ((info >> 8) & 0xfffff) |
| 792 | : info & 0xfffffff); |
| 793 | if (blksz > max) |
| 794 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 795 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 796 | blk = htx_add_blk(htx, type, blksz); |
| 797 | if (!blk) |
| 798 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 799 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 800 | blk->info = info; |
| 801 | total = 4; |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 802 | ptr = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 803 | while (blksz) { |
| 804 | max = MIN(blksz, shctx->block_size - offset); |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 805 | memcpy(ptr, (const char *)shblk->data + offset, max); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 806 | offset += max; |
| 807 | blksz -= max; |
| 808 | total += max; |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 809 | ptr += max; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 810 | if (blksz || offset == shctx->block_size) { |
| 811 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 812 | offset = 0; |
| 813 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 814 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 815 | appctx->ctx.cache.offset = offset; |
| 816 | appctx->ctx.cache.next = shblk; |
| 817 | appctx->ctx.cache.sent += total; |
| 818 | return total; |
| 819 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 820 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 821 | static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx, |
| 822 | uint32_t info, struct shared_block *shblk, unsigned int offset) |
| 823 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 824 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 825 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 826 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
| 827 | unsigned int max, total, rem_data; |
| 828 | uint32_t blksz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 829 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 830 | max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx)); |
| 831 | if (!max) |
| 832 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 833 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 834 | rem_data = 0; |
Christopher Faulet | bda8397 | 2019-06-11 09:58:09 +0200 | [diff] [blame] | 835 | if (appctx->ctx.cache.rem_data) { |
| 836 | blksz = appctx->ctx.cache.rem_data; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 837 | total = 0; |
Christopher Faulet | bda8397 | 2019-06-11 09:58:09 +0200 | [diff] [blame] | 838 | } |
| 839 | else { |
| 840 | blksz = (info & 0xfffffff); |
| 841 | total = 4; |
| 842 | } |
| 843 | if (blksz > max) { |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 844 | rem_data = blksz - max; |
| 845 | blksz = max; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 846 | } |
| 847 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 848 | while (blksz) { |
| 849 | size_t sz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 850 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 851 | max = MIN(blksz, shctx->block_size - offset); |
| 852 | sz = htx_add_data(htx, ist2(shblk->data + offset, max)); |
| 853 | offset += sz; |
| 854 | blksz -= sz; |
| 855 | total += sz; |
| 856 | if (sz < max) |
| 857 | break; |
| 858 | if (blksz || offset == shctx->block_size) { |
| 859 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 860 | offset = 0; |
| 861 | } |
| 862 | } |
| 863 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 864 | appctx->ctx.cache.offset = offset; |
| 865 | appctx->ctx.cache.next = shblk; |
| 866 | appctx->ctx.cache.sent += total; |
| 867 | appctx->ctx.cache.rem_data = rem_data + blksz; |
| 868 | return total; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 869 | } |
| 870 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 871 | static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len, |
| 872 | enum htx_blk_type mark) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 873 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 874 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 875 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 876 | struct shared_block *shblk; |
| 877 | unsigned int offset, sz; |
| 878 | unsigned int ret, total = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 879 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 880 | while (len) { |
| 881 | enum htx_blk_type type; |
| 882 | uint32_t info; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 883 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 884 | shblk = appctx->ctx.cache.next; |
| 885 | offset = appctx->ctx.cache.offset; |
| 886 | if (appctx->ctx.cache.rem_data) { |
| 887 | type = HTX_BLK_DATA; |
| 888 | info = 0; |
| 889 | goto add_data_blk; |
| 890 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 891 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 892 | /* 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] | 893 | sz = MIN(4, shctx->block_size - offset); |
| 894 | memcpy((char *)&info, (const char *)shblk->data + offset, sz); |
| 895 | offset += sz; |
| 896 | if (sz < 4) { |
| 897 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 898 | memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz); |
| 899 | offset = (4 - sz); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 900 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 901 | |
| 902 | /* Get payload of the next HTX block and insert it. */ |
| 903 | type = (info >> 28); |
| 904 | if (type != HTX_BLK_DATA) |
| 905 | ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset); |
| 906 | else { |
| 907 | add_data_blk: |
| 908 | ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 909 | } |
| 910 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 911 | if (!ret) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 912 | break; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 913 | total += ret; |
| 914 | len -= ret; |
| 915 | |
| 916 | if (appctx->ctx.cache.rem_data || type == mark) |
| 917 | break; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 918 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 919 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 920 | return total; |
| 921 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 922 | |
| 923 | static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx) |
| 924 | { |
| 925 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 926 | unsigned int age; |
| 927 | char *end; |
| 928 | |
| 929 | chunk_reset(&trash); |
| 930 | age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age; |
| 931 | if (unlikely(age > CACHE_ENTRY_MAX_AGE)) |
| 932 | age = CACHE_ENTRY_MAX_AGE; |
| 933 | end = ultoa_o(age, b_head(&trash), b_size(&trash)); |
| 934 | b_set_data(&trash, end - b_head(&trash)); |
| 935 | if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash)))) |
| 936 | return 0; |
| 937 | return 1; |
| 938 | } |
| 939 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 940 | static void http_cache_io_handler(struct appctx *appctx) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 941 | { |
| 942 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 943 | struct shared_block *first = block_ptr(cache_ptr); |
| 944 | struct stream_interface *si = appctx->owner; |
| 945 | struct channel *req = si_oc(si); |
| 946 | struct channel *res = si_ic(si); |
| 947 | struct htx *req_htx, *res_htx; |
| 948 | struct buffer *errmsg; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 949 | unsigned int len; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 950 | size_t ret, total = 0; |
| 951 | |
| 952 | res_htx = htxbuf(&res->buf); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 953 | total = res_htx->data; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 954 | |
| 955 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 956 | goto out; |
| 957 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 958 | /* Check if the input buffer is available. */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 959 | if (!b_size(&res->buf)) { |
| 960 | si_rx_room_blk(si); |
| 961 | goto out; |
| 962 | } |
| 963 | |
Willy Tarreau | efef323 | 2018-12-16 00:37:45 +0100 | [diff] [blame] | 964 | if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW)) |
Willy Tarreau | 273e964 | 2018-12-16 00:35:15 +0100 | [diff] [blame] | 965 | appctx->st0 = HTX_CACHE_END; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 966 | |
| 967 | if (appctx->st0 == HTX_CACHE_INIT) { |
| 968 | appctx->ctx.cache.next = block_ptr(cache_ptr); |
| 969 | appctx->ctx.cache.offset = sizeof(*cache_ptr); |
| 970 | appctx->ctx.cache.sent = 0; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 971 | appctx->ctx.cache.rem_data = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 972 | appctx->st0 = HTX_CACHE_HEADER; |
| 973 | } |
| 974 | |
| 975 | if (appctx->st0 == HTX_CACHE_HEADER) { |
| 976 | /* Headers must be dump at once. Otherwise it is an error */ |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 977 | len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent; |
| 978 | ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH); |
| 979 | if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) || |
| 980 | !htx_cache_add_age_hdr(appctx, res_htx)) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 981 | goto error; |
| 982 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 983 | /* In case of a conditional request, we might want to send a |
| 984 | * "304 Not Modified" response instead of the stored data. */ |
Tim Duesterhus | e014234 | 2020-10-22 21:15:06 +0200 | [diff] [blame] | 985 | if (appctx->ctx.cache.send_notmodified) { |
| 986 | if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) { |
| 987 | /* If replacing the status code fails we need to send the full response. */ |
| 988 | appctx->ctx.cache.send_notmodified = 0; |
| 989 | } |
| 990 | } |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 991 | |
| 992 | /* Skip response body for HEAD requests or in case of "304 Not |
| 993 | * Modified" response. */ |
| 994 | if (si_strm(si)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified) |
Christopher Faulet | f0dd037 | 2019-02-25 11:08:34 +0100 | [diff] [blame] | 995 | appctx->st0 = HTX_CACHE_EOM; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 996 | else |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 997 | appctx->st0 = HTX_CACHE_DATA; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | if (appctx->st0 == HTX_CACHE_DATA) { |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1001 | len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent; |
| 1002 | if (len) { |
| 1003 | ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM); |
| 1004 | if (ret < len) { |
| 1005 | si_rx_room_blk(si); |
| 1006 | goto out; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1007 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1008 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1009 | appctx->st0 = HTX_CACHE_END; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | if (appctx->st0 == HTX_CACHE_EOM) { |
Christopher Faulet | 810df06 | 2020-07-22 16:20:34 +0200 | [diff] [blame] | 1013 | res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1014 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 1015 | si_rx_room_blk(si); |
| 1016 | goto out; |
| 1017 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1018 | appctx->st0 = HTX_CACHE_END; |
| 1019 | } |
| 1020 | |
| 1021 | end: |
Christopher Faulet | adb3631 | 2019-02-25 11:40:49 +0100 | [diff] [blame] | 1022 | if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1023 | res->flags |= CF_READ_NULL; |
| 1024 | si_shutr(si); |
| 1025 | } |
| 1026 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1027 | out: |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1028 | total = res_htx->data - total; |
Christopher Faulet | 6112391 | 2019-01-02 14:10:01 +0100 | [diff] [blame] | 1029 | if (total) |
| 1030 | channel_add_input(res, total); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1031 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | adb3631 | 2019-02-25 11:40:49 +0100 | [diff] [blame] | 1032 | |
| 1033 | /* eat the whole request */ |
| 1034 | if (co_data(req)) { |
| 1035 | req_htx = htx_from_buf(&req->buf); |
| 1036 | co_htx_skip(req, req_htx, co_data(req)); |
| 1037 | htx_to_buf(req_htx, &req->buf); |
| 1038 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1039 | return; |
| 1040 | |
| 1041 | error: |
| 1042 | /* Sent and HTTP error 500 */ |
| 1043 | b_reset(&res->buf); |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 1044 | errmsg = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1045 | res->buf.data = b_data(errmsg); |
| 1046 | memcpy(res->buf.area, b_head(errmsg), b_data(errmsg)); |
| 1047 | res_htx = htx_from_buf(&res->buf); |
| 1048 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1049 | total = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1050 | appctx->st0 = HTX_CACHE_END; |
| 1051 | goto end; |
| 1052 | } |
| 1053 | |
| 1054 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1055 | 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] | 1056 | { |
| 1057 | struct flt_conf *fconf; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1058 | struct cache_flt_conf *cconf = NULL; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1059 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1060 | if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1061 | memprintf(err, "expects a cache name"); |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1062 | goto err; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /* check if a cache filter was already registered with this cache |
| 1066 | * name, if that's the case, must use it. */ |
| 1067 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1068 | if (fconf->id == cache_store_flt_id) { |
| 1069 | cconf = fconf->conf; |
| 1070 | if (cconf && !strcmp((char *)cconf->c.name, name)) { |
| 1071 | rule->arg.act.p[0] = cconf; |
| 1072 | return 1; |
| 1073 | } |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1077 | /* Create the filter cache config */ |
| 1078 | cconf = calloc(1, sizeof(*cconf)); |
| 1079 | if (!cconf) { |
| 1080 | memprintf(err, "out of memory\n"); |
| 1081 | goto err; |
| 1082 | } |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1083 | cconf->flags = CACHE_FLT_F_IMPLICIT_DECL; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1084 | cconf->c.name = strdup(name); |
| 1085 | if (!cconf->c.name) { |
| 1086 | memprintf(err, "out of memory\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1087 | goto err; |
| 1088 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1089 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1090 | /* register a filter to fill the cache buffer */ |
| 1091 | fconf = calloc(1, sizeof(*fconf)); |
| 1092 | if (!fconf) { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1093 | memprintf(err, "out of memory\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1094 | goto err; |
| 1095 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1096 | fconf->id = cache_store_flt_id; |
| 1097 | fconf->conf = cconf; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1098 | fconf->ops = &cache_ops; |
| 1099 | LIST_ADDQ(&proxy->filter_configs, &fconf->list); |
| 1100 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1101 | rule->arg.act.p[0] = cconf; |
| 1102 | return 1; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1103 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1104 | err: |
| 1105 | free(cconf); |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
| 1109 | enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy, |
| 1110 | struct act_rule *rule, char **err) |
| 1111 | { |
| 1112 | rule->action = ACT_CUSTOM; |
| 1113 | rule->action_ptr = http_action_store_cache; |
| 1114 | |
| 1115 | if (!parse_cache_rule(proxy, args[*orig_arg], rule, err)) |
| 1116 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1117 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1118 | (*orig_arg)++; |
| 1119 | return ACT_RET_PRS_OK; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1120 | } |
| 1121 | |
Baptiste Assmann | db92a83 | 2019-08-05 16:55:32 +0200 | [diff] [blame] | 1122 | /* This produces a sha1 hash of the concatenation of the HTTP method, |
| 1123 | * the first occurrence of the Host header followed by the path component |
| 1124 | * if it begins with a slash ('/'). */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1125 | int sha1_hosturi(struct stream *s) |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1126 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1127 | struct http_txn *txn = s->txn; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1128 | struct htx *htx = htxbuf(&s->req.buf); |
| 1129 | struct htx_sl *sl; |
| 1130 | struct http_hdr_ctx ctx; |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1131 | struct ist uri; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1132 | blk_SHA_CTX sha1_ctx; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1133 | struct buffer *trash; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1134 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1135 | trash = get_trash_chunk(); |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1136 | ctx.blk = NULL; |
Baptiste Assmann | db92a83 | 2019-08-05 16:55:32 +0200 | [diff] [blame] | 1137 | |
| 1138 | switch (txn->meth) { |
| 1139 | case HTTP_METH_HEAD: |
| 1140 | case HTTP_METH_GET: |
| 1141 | chunk_memcat(trash, "GET", 3); |
| 1142 | break; |
| 1143 | default: |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1147 | sl = http_get_stline(htx); |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1148 | uri = htx_sl_req_uri(sl); // whole uri |
| 1149 | if (!uri.len) |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1150 | return 0; |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1151 | |
| 1152 | /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'), |
| 1153 | * unless haproxy is deployed in front of an outbound cache. In HTTP/2, |
| 1154 | * URIs are almost always sent in absolute form with their scheme. In |
| 1155 | * this case, the scheme is almost always "https". In order to support |
| 1156 | * sharing of cache objects between H1 and H2, we'll hash the absolute |
| 1157 | * URI whenever known, or prepend "https://" + the Host header for |
| 1158 | * relative URIs. The difference will only appear on absolute HTTP/1 |
| 1159 | * requests sent to an origin server, which practically is never met in |
| 1160 | * the real world so we don't care about the ability to share the same |
| 1161 | * key here.URIs are normalized from the absolute URI to an origin form as |
| 1162 | * well. |
| 1163 | */ |
| 1164 | if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) { |
Willy Tarreau | 20020ae | 2019-10-29 13:02:15 +0100 | [diff] [blame] | 1165 | chunk_istcat(trash, ist("https://")); |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1166 | if (!http_find_header(htx, ist("Host"), &ctx, 0)) |
| 1167 | return 0; |
Willy Tarreau | 20020ae | 2019-10-29 13:02:15 +0100 | [diff] [blame] | 1168 | chunk_istcat(trash, ctx.value); |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | chunk_memcat(trash, uri.ptr, uri.len); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1172 | |
| 1173 | /* hash everything */ |
| 1174 | blk_SHA1_Init(&sha1_ctx); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1175 | blk_SHA1_Update(&sha1_ctx, trash->area, trash->data); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1176 | blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx); |
| 1177 | |
| 1178 | return 1; |
| 1179 | } |
| 1180 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1181 | /* Looks for "If-None-Match" headers in the request and compares their value |
| 1182 | * with the one that might have been stored in the cache_entry. If any of them |
| 1183 | * matches, a "304 Not Modified" response should be sent instead of the cached |
| 1184 | * data. |
| 1185 | * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is |
| 1186 | * valid and should receive a "304 Not Modified" response (RFC 7434#4.3.2). |
| 1187 | * Returns 1 if "304 Not Modified" should be sent, 0 otherwise. |
| 1188 | */ |
| 1189 | static int should_send_notmodified_response(struct cache *cache, struct htx *htx, |
| 1190 | struct cache_entry *entry) |
| 1191 | { |
| 1192 | int retval = 0; |
| 1193 | |
| 1194 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 1195 | struct ist cache_entry_etag = IST_NULL; |
| 1196 | struct buffer *etag_buffer = NULL; |
| 1197 | |
| 1198 | if (entry->etag_length == 0) |
| 1199 | return 0; |
| 1200 | |
| 1201 | /* If we find a "If-None-Match" header in the request, rebuild the |
| 1202 | * cache_entry's ETag in order to perform comparisons. */ |
| 1203 | /* There could be multiple "if-none-match" header lines. */ |
| 1204 | while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) { |
| 1205 | |
| 1206 | /* A '*' matches everything. */ |
| 1207 | if (isteq(ctx.value, ist("*")) != 0) { |
| 1208 | retval = 1; |
| 1209 | break; |
| 1210 | } |
| 1211 | |
| 1212 | /* Rebuild the stored ETag. */ |
| 1213 | if (etag_buffer == NULL) { |
| 1214 | etag_buffer = get_trash_chunk(); |
| 1215 | |
| 1216 | if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry), |
| 1217 | (unsigned char*)b_orig(etag_buffer), |
| 1218 | entry->etag_offset, entry->etag_length) == 0) { |
| 1219 | cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length); |
| 1220 | } else { |
| 1221 | /* We could not rebuild the ETag in one go, we |
| 1222 | * won't send a "304 Not Modified" response. */ |
| 1223 | break; |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | if (http_compare_etags(cache_entry_etag, ctx.value) == 1) { |
| 1228 | retval = 1; |
| 1229 | break; |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | return retval; |
| 1234 | } |
| 1235 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1236 | enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px, |
| 1237 | struct session *sess, struct stream *s, int flags) |
| 1238 | { |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1239 | |
Christopher Faulet | b3d4bca | 2019-02-25 10:59:33 +0100 | [diff] [blame] | 1240 | struct http_txn *txn = s->txn; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1241 | struct cache_entry *res; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1242 | struct cache_flt_conf *cconf = rule->arg.act.p[0]; |
| 1243 | struct cache *cache = cconf->c.cache; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1244 | |
Willy Tarreau | 6905d18 | 2019-10-01 17:59:17 +0200 | [diff] [blame] | 1245 | /* Ignore cache for HTTP/1.0 requests and for requests other than GET |
| 1246 | * and HEAD */ |
Christopher Faulet | b3d4bca | 2019-02-25 10:59:33 +0100 | [diff] [blame] | 1247 | if (!(txn->req.flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 6905d18 | 2019-10-01 17:59:17 +0200 | [diff] [blame] | 1248 | (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD)) |
Christopher Faulet | b3d4bca | 2019-02-25 10:59:33 +0100 | [diff] [blame] | 1249 | txn->flags |= TX_CACHE_IGNORE; |
| 1250 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1251 | http_check_request_for_cacheability(s, &s->req); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1252 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 1253 | if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE) |
| 1254 | return ACT_RET_CONT; |
| 1255 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1256 | if (!sha1_hosturi(s)) |
Willy Tarreau | 7704b1e | 2017-12-22 16:32:43 +0100 | [diff] [blame] | 1257 | return ACT_RET_CONT; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1258 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 1259 | if (s->txn->flags & TX_CACHE_IGNORE) |
| 1260 | return ACT_RET_CONT; |
| 1261 | |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1262 | if (px == strm_fe(s)) |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1263 | _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1264 | else |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1265 | _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1266 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1267 | shctx_lock(shctx_ptr(cache)); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1268 | res = entry_exist(cache, s->txn->cache_hash); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1269 | if (res) { |
| 1270 | struct appctx *appctx; |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1271 | shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res)); |
| 1272 | shctx_unlock(shctx_ptr(cache)); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1273 | s->target = &http_cache_applet.obj_type; |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1274 | if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) { |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1275 | appctx->st0 = HTX_CACHE_INIT; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1276 | appctx->rule = rule; |
| 1277 | appctx->ctx.cache.entry = res; |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 1278 | appctx->ctx.cache.next = NULL; |
| 1279 | appctx->ctx.cache.sent = 0; |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1280 | appctx->ctx.cache.send_notmodified = |
| 1281 | should_send_notmodified_response(cache, htxbuf(&s->req.buf), res); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1282 | |
| 1283 | if (px == strm_fe(s)) |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1284 | _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1285 | else |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1286 | _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 1287 | return ACT_RET_CONT; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1288 | } else { |
William Lallemand | 55e7674 | 2017-11-21 20:01:28 +0100 | [diff] [blame] | 1289 | shctx_lock(shctx_ptr(cache)); |
| 1290 | shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res)); |
| 1291 | shctx_unlock(shctx_ptr(cache)); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 1292 | return ACT_RET_YIELD; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1293 | } |
| 1294 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1295 | shctx_unlock(shctx_ptr(cache)); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 1296 | return ACT_RET_CONT; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | |
| 1300 | enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy, |
| 1301 | struct act_rule *rule, char **err) |
| 1302 | { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1303 | rule->action = ACT_CUSTOM; |
| 1304 | rule->action_ptr = http_action_req_cache_use; |
| 1305 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1306 | if (!parse_cache_rule(proxy, args[*orig_arg], rule, err)) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1307 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1308 | |
| 1309 | (*orig_arg)++; |
| 1310 | return ACT_RET_PRS_OK; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | int cfg_parse_cache(const char *file, int linenum, char **args, int kwm) |
| 1314 | { |
| 1315 | int err_code = 0; |
| 1316 | |
| 1317 | if (strcmp(args[0], "cache") == 0) { /* new cache section */ |
| 1318 | |
| 1319 | if (!*args[1]) { |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1320 | ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1321 | file, linenum, args[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1322 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1323 | goto out; |
| 1324 | } |
| 1325 | |
| 1326 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1327 | err_code |= ERR_ABORT; |
| 1328 | goto out; |
| 1329 | } |
| 1330 | |
| 1331 | if (tmp_cache_config == NULL) { |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1332 | struct cache *cache_config; |
| 1333 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1334 | tmp_cache_config = calloc(1, sizeof(*tmp_cache_config)); |
| 1335 | if (!tmp_cache_config) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1336 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1337 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1338 | goto out; |
| 1339 | } |
| 1340 | |
| 1341 | strlcpy2(tmp_cache_config->id, args[1], 33); |
| 1342 | if (strlen(args[1]) > 32) { |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1343 | 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] | 1344 | file, linenum, tmp_cache_config->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1345 | err_code |= ERR_WARN; |
| 1346 | } |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1347 | |
| 1348 | list_for_each_entry(cache_config, &caches_config, list) { |
| 1349 | if (strcmp(tmp_cache_config->id, cache_config->id) == 0) { |
| 1350 | ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n", |
| 1351 | file, linenum, tmp_cache_config->id); |
| 1352 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1353 | goto out; |
| 1354 | } |
| 1355 | } |
| 1356 | |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 1357 | tmp_cache_config->maxage = 60; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1358 | tmp_cache_config->maxblocks = 0; |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1359 | tmp_cache_config->maxobjsz = 0; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1360 | } |
| 1361 | } else if (strcmp(args[0], "total-max-size") == 0) { |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1362 | unsigned long int maxsize; |
| 1363 | char *err; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1364 | |
| 1365 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1366 | err_code |= ERR_ABORT; |
| 1367 | goto out; |
| 1368 | } |
| 1369 | |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1370 | maxsize = strtoul(args[1], &err, 10); |
| 1371 | if (err == args[1] || *err != '\0') { |
| 1372 | ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n", |
| 1373 | file, linenum, args[1]); |
| 1374 | err_code |= ERR_ABORT; |
| 1375 | goto out; |
| 1376 | } |
| 1377 | |
| 1378 | if (maxsize > (UINT_MAX >> 20)) { |
| 1379 | ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n", |
| 1380 | file, linenum, args[1], UINT_MAX >> 20); |
| 1381 | err_code |= ERR_ABORT; |
| 1382 | goto out; |
| 1383 | } |
| 1384 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1385 | /* size in megabytes */ |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1386 | maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1387 | tmp_cache_config->maxblocks = maxsize; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 1388 | } else if (strcmp(args[0], "max-age") == 0) { |
| 1389 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1390 | err_code |= ERR_ABORT; |
| 1391 | goto out; |
| 1392 | } |
| 1393 | |
| 1394 | if (!*args[1]) { |
| 1395 | ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n", |
| 1396 | file, linenum, args[0]); |
| 1397 | err_code |= ERR_WARN; |
| 1398 | } |
| 1399 | |
| 1400 | tmp_cache_config->maxage = atoi(args[1]); |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1401 | } else if (strcmp(args[0], "max-object-size") == 0) { |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1402 | unsigned int maxobjsz; |
| 1403 | char *err; |
| 1404 | |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1405 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1406 | err_code |= ERR_ABORT; |
| 1407 | goto out; |
| 1408 | } |
| 1409 | |
| 1410 | if (!*args[1]) { |
| 1411 | ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n", |
| 1412 | file, linenum, args[0]); |
| 1413 | err_code |= ERR_WARN; |
| 1414 | } |
| 1415 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1416 | maxobjsz = strtoul(args[1], &err, 10); |
| 1417 | if (err == args[1] || *err != '\0') { |
| 1418 | ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n", |
| 1419 | file, linenum, args[1]); |
| 1420 | err_code |= ERR_ABORT; |
| 1421 | goto out; |
| 1422 | } |
| 1423 | tmp_cache_config->maxobjsz = maxobjsz; |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1424 | } |
| 1425 | else if (*args[0] != 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1426 | 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] | 1427 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1428 | goto out; |
| 1429 | } |
| 1430 | out: |
| 1431 | return err_code; |
| 1432 | } |
| 1433 | |
| 1434 | /* once the cache section is parsed */ |
| 1435 | |
| 1436 | int cfg_post_parse_section_cache() |
| 1437 | { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1438 | int err_code = 0; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1439 | |
| 1440 | if (tmp_cache_config) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1441 | |
| 1442 | if (tmp_cache_config->maxblocks <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1443 | 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] | 1444 | err_code |= ERR_FATAL | ERR_ALERT; |
| 1445 | goto out; |
| 1446 | } |
| 1447 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1448 | if (!tmp_cache_config->maxobjsz) { |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1449 | /* Default max. file size is a 256th of the cache size. */ |
| 1450 | tmp_cache_config->maxobjsz = |
| 1451 | (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8; |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1452 | } |
| 1453 | else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) { |
| 1454 | ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2); |
| 1455 | err_code |= ERR_FATAL | ERR_ALERT; |
| 1456 | goto out; |
| 1457 | } |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1458 | |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1459 | /* add to the list of cache to init and reinit tmp_cache_config |
| 1460 | * for next cache section, if any. |
| 1461 | */ |
| 1462 | LIST_ADDQ(&caches_config, &tmp_cache_config->list); |
| 1463 | tmp_cache_config = NULL; |
| 1464 | return err_code; |
| 1465 | } |
| 1466 | out: |
| 1467 | free(tmp_cache_config); |
| 1468 | tmp_cache_config = NULL; |
| 1469 | return err_code; |
| 1470 | |
| 1471 | } |
| 1472 | |
| 1473 | int post_check_cache() |
| 1474 | { |
| 1475 | struct proxy *px; |
| 1476 | struct cache *back, *cache_config, *cache; |
| 1477 | struct shared_context *shctx; |
| 1478 | int ret_shctx; |
| 1479 | int err_code = 0; |
| 1480 | |
| 1481 | list_for_each_entry_safe(cache_config, back, &caches_config, list) { |
| 1482 | |
| 1483 | ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE, |
| 1484 | cache_config->maxobjsz, sizeof(struct cache), 1); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1485 | |
Frédéric Lécaille | bc58449 | 2018-10-25 20:18:59 +0200 | [diff] [blame] | 1486 | if (ret_shctx <= 0) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1487 | if (ret_shctx == SHCTX_E_INIT_LOCK) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1488 | ha_alert("Unable to initialize the lock for the cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1489 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1490 | ha_alert("Unable to allocate cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1491 | |
| 1492 | err_code |= ERR_FATAL | ERR_ALERT; |
| 1493 | goto out; |
| 1494 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1495 | shctx->free_block = cache_free_blocks; |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1496 | /* the cache structure is stored in the shctx and added to the |
| 1497 | * caches list, we can remove the entry from the caches_config |
| 1498 | * list */ |
| 1499 | memcpy(shctx->data, cache_config, sizeof(struct cache)); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1500 | cache = (struct cache *)shctx->data; |
| 1501 | cache->entries = EB_ROOT_UNIQUE; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1502 | LIST_ADDQ(&caches, &cache->list); |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1503 | LIST_DEL(&cache_config->list); |
| 1504 | free(cache_config); |
| 1505 | |
| 1506 | /* Find all references for this cache in the existing filters |
| 1507 | * (over all proxies) and reference it in matching filters. |
| 1508 | */ |
| 1509 | for (px = proxies_list; px; px = px->next) { |
| 1510 | struct flt_conf *fconf; |
| 1511 | struct cache_flt_conf *cconf; |
| 1512 | |
| 1513 | list_for_each_entry(fconf, &px->filter_configs, list) { |
| 1514 | if (fconf->id != cache_store_flt_id) |
| 1515 | continue; |
| 1516 | |
| 1517 | cconf = fconf->conf; |
| 1518 | if (!strcmp(cache->id, cconf->c.name)) { |
| 1519 | free(cconf->c.name); |
Tim Duesterhus | d7c6e6a | 2020-09-14 18:01:33 +0200 | [diff] [blame] | 1520 | cconf->flags |= CACHE_FLT_INIT; |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1521 | cconf->c.cache = cache; |
| 1522 | break; |
| 1523 | } |
| 1524 | } |
| 1525 | } |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1526 | } |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1527 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1528 | out: |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1529 | return err_code; |
| 1530 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1531 | } |
| 1532 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1533 | struct flt_ops cache_ops = { |
| 1534 | .init = cache_store_init, |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1535 | .check = cache_store_check, |
| 1536 | .deinit = cache_store_deinit, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1537 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 1538 | /* Handle stream init/deinit */ |
| 1539 | .attach = cache_store_strm_init, |
| 1540 | .detach = cache_store_strm_deinit, |
| 1541 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1542 | /* Handle channels activity */ |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 1543 | .channel_post_analyze = cache_store_post_analyze, |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1544 | |
| 1545 | /* Filter HTTP requests and responses */ |
| 1546 | .http_headers = cache_store_http_headers, |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1547 | .http_payload = cache_store_http_payload, |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1548 | .http_end = cache_store_http_end, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1549 | }; |
| 1550 | |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1551 | |
| 1552 | |
| 1553 | static int |
| 1554 | parse_cache_flt(char **args, int *cur_arg, struct proxy *px, |
| 1555 | struct flt_conf *fconf, char **err, void *private) |
| 1556 | { |
| 1557 | struct flt_conf *f, *back; |
Willy Tarreau | a73da1e | 2018-12-14 10:19:28 +0100 | [diff] [blame] | 1558 | struct cache_flt_conf *cconf = NULL; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1559 | char *name = NULL; |
| 1560 | int pos = *cur_arg; |
| 1561 | |
Christopher Faulet | 2a37cdb | 2020-05-18 11:58:16 +0200 | [diff] [blame] | 1562 | /* Get the cache filter name. <pos> point on "cache" keyword */ |
| 1563 | if (!*args[pos + 1]) { |
Tim Duesterhus | ea969f6 | 2020-08-18 22:06:51 +0200 | [diff] [blame] | 1564 | memprintf(err, "%s : expects a <name> argument", args[pos]); |
Christopher Faulet | 2a37cdb | 2020-05-18 11:58:16 +0200 | [diff] [blame] | 1565 | goto error; |
| 1566 | } |
| 1567 | name = strdup(args[pos + 1]); |
| 1568 | if (!name) { |
| 1569 | memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]); |
| 1570 | goto error; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1571 | } |
Christopher Faulet | 2a37cdb | 2020-05-18 11:58:16 +0200 | [diff] [blame] | 1572 | pos += 2; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1573 | |
| 1574 | /* Check if an implicit filter with the same name already exists. If so, |
| 1575 | * we remove the implicit filter to use the explicit one. */ |
| 1576 | list_for_each_entry_safe(f, back, &px->filter_configs, list) { |
| 1577 | if (f->id != cache_store_flt_id) |
| 1578 | continue; |
| 1579 | |
| 1580 | cconf = f->conf; |
| 1581 | if (strcmp(name, cconf->c.name)) { |
| 1582 | cconf = NULL; |
| 1583 | continue; |
| 1584 | } |
| 1585 | |
| 1586 | if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) { |
| 1587 | cconf = NULL; |
| 1588 | memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'", |
| 1589 | px->id, name); |
Tim Duesterhus | d34b1ce | 2020-01-18 01:46:18 +0100 | [diff] [blame] | 1590 | goto error; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | /* Remove the implicit filter. <cconf> is kept for the explicit one */ |
| 1594 | LIST_DEL(&f->list); |
| 1595 | free(f); |
| 1596 | free(name); |
| 1597 | break; |
| 1598 | } |
| 1599 | |
| 1600 | /* No implicit cache filter found, create configuration for the explicit one */ |
| 1601 | if (!cconf) { |
| 1602 | cconf = calloc(1, sizeof(*cconf)); |
| 1603 | if (!cconf) { |
| 1604 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 1605 | goto error; |
| 1606 | } |
| 1607 | cconf->c.name = name; |
| 1608 | } |
| 1609 | |
| 1610 | cconf->flags = 0; |
| 1611 | fconf->id = cache_store_flt_id; |
| 1612 | fconf->conf = cconf; |
| 1613 | fconf->ops = &cache_ops; |
| 1614 | |
| 1615 | *cur_arg = pos; |
| 1616 | return 0; |
| 1617 | |
| 1618 | error: |
| 1619 | free(name); |
| 1620 | free(cconf); |
| 1621 | return -1; |
| 1622 | } |
| 1623 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 1624 | 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] | 1625 | { |
| 1626 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1627 | return 1; |
| 1628 | |
| 1629 | return 0; |
| 1630 | } |
| 1631 | |
| 1632 | static int cli_io_handler_show_cache(struct appctx *appctx) |
| 1633 | { |
| 1634 | struct cache* cache = appctx->ctx.cli.p0; |
| 1635 | struct stream_interface *si = appctx->owner; |
| 1636 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1637 | if (cache == NULL) { |
| 1638 | cache = LIST_ELEM((caches).n, typeof(struct cache *), list); |
| 1639 | } |
| 1640 | |
| 1641 | list_for_each_entry_from(cache, &caches, list) { |
| 1642 | struct eb32_node *node = NULL; |
| 1643 | unsigned int next_key; |
| 1644 | struct cache_entry *entry; |
| 1645 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1646 | next_key = appctx->ctx.cli.i0; |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1647 | if (!next_key) { |
| 1648 | chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav); |
| 1649 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1650 | si_rx_room_blk(si); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1651 | return 0; |
| 1652 | } |
| 1653 | } |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1654 | |
| 1655 | appctx->ctx.cli.p0 = cache; |
| 1656 | |
| 1657 | while (1) { |
| 1658 | |
| 1659 | shctx_lock(shctx_ptr(cache)); |
| 1660 | node = eb32_lookup_ge(&cache->entries, next_key); |
| 1661 | if (!node) { |
| 1662 | shctx_unlock(shctx_ptr(cache)); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1663 | appctx->ctx.cli.i0 = 0; |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1664 | break; |
| 1665 | } |
| 1666 | |
| 1667 | entry = container_of(node, struct cache_entry, eb); |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 1668 | chunk_printf(&trash, "%p hash:%u size:%u (%u blocks), refcount:%u, expire:%d\n", entry, read_u32(entry->hash), block_ptr(entry)->len, block_ptr(entry)->block_count, block_ptr(entry)->refcount, entry->expire - (int)now.tv_sec); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1669 | |
| 1670 | next_key = node->key + 1; |
| 1671 | appctx->ctx.cli.i0 = next_key; |
| 1672 | |
| 1673 | shctx_unlock(shctx_ptr(cache)); |
| 1674 | |
| 1675 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1676 | si_rx_room_blk(si); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1677 | return 0; |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | } |
| 1682 | |
| 1683 | return 1; |
| 1684 | |
| 1685 | } |
| 1686 | |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1687 | /* Declare the filter parser for "cache" keyword */ |
| 1688 | static struct flt_kw_list filter_kws = { "CACHE", { }, { |
| 1689 | { "cache", parse_cache_flt, NULL }, |
| 1690 | { NULL, NULL, NULL }, |
| 1691 | } |
| 1692 | }; |
| 1693 | |
| 1694 | INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws); |
| 1695 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1696 | static struct cli_kw_list cli_kws = {{},{ |
William Lallemand | e899af8 | 2017-11-22 16:41:26 +0100 | [diff] [blame] | 1697 | { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL }, |
| 1698 | {{},} |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1699 | }}; |
| 1700 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1701 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1702 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1703 | static struct action_kw_list http_res_actions = { |
| 1704 | .kw = { |
| 1705 | { "cache-store", parse_cache_store }, |
| 1706 | { NULL, NULL } |
| 1707 | } |
| 1708 | }; |
| 1709 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1710 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions); |
| 1711 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1712 | static struct action_kw_list http_req_actions = { |
| 1713 | .kw = { |
| 1714 | { "cache-use", parse_cache_use }, |
| 1715 | { NULL, NULL } |
| 1716 | } |
| 1717 | }; |
| 1718 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1719 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 1720 | |
Willy Tarreau | 2231b63 | 2019-03-29 18:26:52 +0100 | [diff] [blame] | 1721 | struct applet http_cache_applet = { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1722 | .obj_type = OBJ_TYPE_APPLET, |
| 1723 | .name = "<CACHE>", /* used for logging */ |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1724 | .fct = http_cache_io_handler, |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 1725 | .release = http_cache_applet_release, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1726 | }; |
| 1727 | |
Willy Tarreau | e655251 | 2018-11-26 11:33:13 +0100 | [diff] [blame] | 1728 | /* config parsers for this section */ |
| 1729 | REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache); |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1730 | REGISTER_POST_CHECK(post_check_cache); |