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