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