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