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