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