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