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