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