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