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> |
| 26 | #include <proto/filters.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 27 | #include <proto/http_rules.h> |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 28 | #include <proto/proto_http.h> |
| 29 | #include <proto/log.h> |
| 30 | #include <proto/stream.h> |
| 31 | #include <proto/stream_interface.h> |
| 32 | #include <proto/shctx.h> |
| 33 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 34 | |
| 35 | #include <common/cfgparse.h> |
| 36 | #include <common/hash.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 37 | #include <common/initcall.h> |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 38 | |
| 39 | /* flt_cache_store */ |
Christopher Faulet | 1f672c5 | 2018-12-03 14:30:41 +0100 | [diff] [blame] | 40 | #define CACHE_F_LEGACY_HTTP 0x00000001 /* The cache is used to store raw HTTP |
| 41 | * messages (legacy implementation) */ |
| 42 | #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] | 43 | |
| 44 | static const char *cache_store_flt_id = "cache store filter"; |
| 45 | |
| 46 | struct applet http_cache_applet; |
| 47 | |
| 48 | struct flt_ops cache_ops; |
| 49 | |
| 50 | struct cache { |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 51 | struct list list; /* cache linked list */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 52 | struct eb_root entries; /* head of cache entries based on keys */ |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 53 | unsigned int maxage; /* max-age */ |
| 54 | unsigned int maxblocks; |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 55 | unsigned int maxobjsz; /* max-object-size (in bytes) */ |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 56 | char id[33]; /* cache name */ |
Christopher Faulet | 1f672c5 | 2018-12-03 14:30:41 +0100 | [diff] [blame] | 57 | unsigned int flags; /* CACHE_F_* */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | /* |
| 61 | * cache ctx for filters |
| 62 | */ |
| 63 | struct cache_st { |
| 64 | int hdrs_len; |
| 65 | struct shared_block *first_block; |
| 66 | }; |
| 67 | |
| 68 | struct cache_entry { |
| 69 | unsigned int latest_validation; /* latest validation date */ |
| 70 | unsigned int expire; /* expiration date */ |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 71 | unsigned int age; /* Origin server "Age" header value */ |
| 72 | unsigned int eoh; /* Origin server end of headers offset. */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 73 | struct eb32_node eb; /* ebtree node used to hold the cache object */ |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 74 | char hash[20]; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 75 | unsigned char data[0]; |
| 76 | }; |
| 77 | |
| 78 | #define CACHE_BLOCKSIZE 1024 |
Willy Tarreau | 96062a1 | 2018-11-11 14:00:28 +0100 | [diff] [blame] | 79 | #define CACHE_ENTRY_MAX_AGE 2147483648U |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 80 | |
| 81 | static struct list caches = LIST_HEAD_INIT(caches); |
| 82 | static struct cache *tmp_cache_config = NULL; |
| 83 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 84 | DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st)); |
| 85 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 86 | struct cache_entry *entry_exist(struct cache *cache, char *hash) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 87 | { |
| 88 | struct eb32_node *node; |
| 89 | struct cache_entry *entry; |
| 90 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 91 | node = eb32_lookup(&cache->entries, (*(unsigned int *)hash)); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 92 | if (!node) |
| 93 | return NULL; |
| 94 | |
| 95 | entry = eb32_entry(node, struct cache_entry, eb); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 96 | |
| 97 | /* if that's not the right node */ |
| 98 | if (memcmp(entry->hash, hash, sizeof(entry->hash))) |
| 99 | return NULL; |
| 100 | |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 101 | if (entry->expire > now.tv_sec) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 102 | return entry; |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 103 | } else { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 104 | eb32_delete(node); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 105 | entry->eb.key = 0; |
| 106 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 107 | return NULL; |
| 108 | |
| 109 | } |
| 110 | |
| 111 | static inline struct shared_context *shctx_ptr(struct cache *cache) |
| 112 | { |
| 113 | return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data); |
| 114 | } |
| 115 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 116 | static inline struct shared_block *block_ptr(struct cache_entry *entry) |
| 117 | { |
| 118 | return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 123 | static int |
| 124 | cache_store_init(struct proxy *px, struct flt_conf *f1conf) |
| 125 | { |
| 126 | return 0; |
| 127 | } |
| 128 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 129 | static int |
| 130 | cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 131 | { |
| 132 | if (!(chn->flags & CF_ISRESP)) |
| 133 | return 1; |
| 134 | |
| 135 | if (filter->ctx == NULL) { |
| 136 | struct cache_st *st; |
| 137 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 138 | st = pool_alloc_dirty(pool_head_cache_st); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 139 | if (st == NULL) |
| 140 | return -1; |
| 141 | |
| 142 | st->hdrs_len = 0; |
| 143 | st->first_block = NULL; |
| 144 | filter->ctx = st; |
| 145 | } |
| 146 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 147 | return 1; |
| 148 | } |
| 149 | |
| 150 | static int |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 151 | cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 152 | { |
| 153 | struct cache_st *st = filter->ctx; |
| 154 | struct cache *cache = filter->config->conf; |
| 155 | struct shared_context *shctx = shctx_ptr(cache); |
| 156 | |
| 157 | if (!(chn->flags & CF_ISRESP)) |
| 158 | return 1; |
| 159 | |
| 160 | /* Everything should be released in the http_end filter, but we need to do it |
| 161 | * there too, in case of errors */ |
| 162 | |
| 163 | if (st && st->first_block) { |
| 164 | |
| 165 | shctx_lock(shctx); |
| 166 | shctx_row_dec_hot(shctx, st->first_block); |
| 167 | shctx_unlock(shctx); |
| 168 | |
| 169 | } |
| 170 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 171 | pool_free(pool_head_cache_st, st); |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 172 | filter->ctx = NULL; |
| 173 | } |
| 174 | |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | static int |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 180 | cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 181 | { |
| 182 | struct cache_st *st = filter->ctx; |
| 183 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 184 | if (!(msg->chn->flags & CF_ISRESP) || !st) |
| 185 | return 1; |
| 186 | |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame^] | 187 | if (st->first_block) { |
| 188 | register_data_filter(s, msg->chn, filter); |
| 189 | st->hdrs_len = msg->sov; |
| 190 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 191 | return 1; |
| 192 | } |
| 193 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 194 | static inline void disable_cache_entry(struct cache_st *st, |
| 195 | struct filter *filter, struct shared_context *shctx) |
| 196 | { |
| 197 | struct cache_entry *object; |
| 198 | |
| 199 | object = (struct cache_entry *)st->first_block->data; |
| 200 | filter->ctx = NULL; /* disable cache */ |
| 201 | shctx_lock(shctx); |
| 202 | shctx_row_dec_hot(shctx, st->first_block); |
| 203 | object->eb.key = 0; |
| 204 | shctx_unlock(shctx); |
| 205 | pool_free(pool_head_cache_st, st); |
| 206 | } |
| 207 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 208 | static int |
| 209 | cache_store_http_forward_data(struct stream *s, struct filter *filter, |
| 210 | struct http_msg *msg, unsigned int len) |
| 211 | { |
| 212 | struct cache_st *st = filter->ctx; |
| 213 | struct shared_context *shctx = shctx_ptr((struct cache *)filter->config->conf); |
| 214 | int ret; |
| 215 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 216 | ret = 0; |
| 217 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 218 | /* |
| 219 | * We need to skip the HTTP headers first, because we saved them in the |
| 220 | * http-response action. |
| 221 | */ |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame^] | 222 | if (!(msg->chn->flags & CF_ISRESP) || !st) { |
| 223 | /* should never happen */ |
| 224 | unregister_data_filter(s, msg->chn, filter); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 225 | return len; |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame^] | 226 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 227 | |
| 228 | if (!len) { |
Joseph Herlant | 8dae5b3 | 2018-11-15 14:07:53 -0800 | [diff] [blame] | 229 | /* Nothing to forward */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 230 | ret = len; |
| 231 | } |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 232 | else if (st->hdrs_len >= len) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 233 | /* Forward part of headers */ |
| 234 | ret = len; |
| 235 | st->hdrs_len -= len; |
| 236 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 237 | else { |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 238 | /* Forward data */ |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame^] | 239 | if (st->first_block) { |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 240 | int to_append, append; |
| 241 | struct shared_block *fb; |
| 242 | |
| 243 | to_append = MIN(ci_contig_data(msg->chn), len - st->hdrs_len); |
| 244 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 245 | shctx_lock(shctx); |
| 246 | fb = shctx_row_reserve_hot(shctx, st->first_block, to_append); |
| 247 | if (!fb) { |
Olivier Houchard | cd2867a | 2017-11-01 13:58:21 +0100 | [diff] [blame] | 248 | shctx_unlock(shctx); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 249 | disable_cache_entry(st, filter, shctx); |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame^] | 250 | unregister_data_filter(s, msg->chn, filter); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 251 | return len; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 252 | } |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 253 | shctx_unlock(shctx); |
| 254 | |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 255 | /* Skip remaining headers to fill the cache */ |
| 256 | c_adv(msg->chn, st->hdrs_len); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 257 | append = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append, |
| 258 | (unsigned char *)ci_head(msg->chn), to_append); |
| 259 | ret = st->hdrs_len + to_append - append; |
| 260 | /* Rewind the buffer to forward all data */ |
| 261 | c_rew(msg->chn, st->hdrs_len); |
| 262 | st->hdrs_len = 0; |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame^] | 263 | if (ret < 0) { |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 264 | disable_cache_entry(st, filter, shctx); |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame^] | 265 | unregister_data_filter(s, msg->chn, filter); |
| 266 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 267 | } |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame^] | 268 | else { |
| 269 | /* should never happen */ |
| 270 | unregister_data_filter(s, msg->chn, filter); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 271 | ret = len; |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame^] | 272 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | if ((ret != len) || |
| 276 | (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret)) |
| 277 | task_wakeup(s->task, TASK_WOKEN_MSG); |
| 278 | |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | static int |
| 283 | cache_store_http_end(struct stream *s, struct filter *filter, |
| 284 | struct http_msg *msg) |
| 285 | { |
| 286 | struct cache_st *st = filter->ctx; |
| 287 | struct cache *cache = filter->config->conf; |
| 288 | struct shared_context *shctx = shctx_ptr(cache); |
| 289 | struct cache_entry *object; |
| 290 | |
| 291 | if (!(msg->chn->flags & CF_ISRESP)) |
| 292 | return 1; |
| 293 | |
| 294 | if (st && st->first_block) { |
| 295 | |
| 296 | object = (struct cache_entry *)st->first_block->data; |
| 297 | |
| 298 | /* does not need to test if the insertion worked, if it |
| 299 | * doesn't, the blocks will be reused anyway */ |
| 300 | |
| 301 | shctx_lock(shctx); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 302 | if (eb32_insert(&cache->entries, &object->eb) != &object->eb) { |
| 303 | object->eb.key = 0; |
| 304 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 305 | /* remove from the hotlist */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 306 | shctx_row_dec_hot(shctx, st->first_block); |
| 307 | shctx_unlock(shctx); |
| 308 | |
| 309 | } |
| 310 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 311 | pool_free(pool_head_cache_st, st); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 312 | filter->ctx = NULL; |
| 313 | } |
| 314 | |
| 315 | return 1; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * This intends to be used when checking HTTP headers for some |
| 320 | * word=value directive. Return a pointer to the first character of value, if |
| 321 | * the word was not found or if there wasn't any value assigned ot it return NULL |
| 322 | */ |
| 323 | char *directive_value(const char *sample, int slen, const char *word, int wlen) |
| 324 | { |
| 325 | int st = 0; |
| 326 | |
| 327 | if (slen < wlen) |
| 328 | return 0; |
| 329 | |
| 330 | while (wlen) { |
| 331 | char c = *sample ^ *word; |
| 332 | if (c && c != ('A' ^ 'a')) |
| 333 | return NULL; |
| 334 | sample++; |
| 335 | word++; |
| 336 | slen--; |
| 337 | wlen--; |
| 338 | } |
| 339 | |
| 340 | while (slen) { |
| 341 | if (st == 0) { |
| 342 | if (*sample != '=') |
| 343 | return NULL; |
| 344 | sample++; |
| 345 | slen--; |
| 346 | st = 1; |
| 347 | continue; |
| 348 | } else { |
| 349 | return (char *)sample; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | return NULL; |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * Return the maxage in seconds of an HTTP response. |
| 358 | * Compute the maxage using either: |
| 359 | * - the assigned max-age of the cache |
| 360 | * - the s-maxage directive |
| 361 | * - the max-age directive |
| 362 | * - (Expires - Data) headers |
| 363 | * - the default-max-age of the cache |
| 364 | * |
| 365 | */ |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 366 | int http_calc_maxage(struct stream *s, struct cache *cache) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 367 | { |
| 368 | struct http_txn *txn = s->txn; |
| 369 | struct hdr_ctx ctx; |
| 370 | |
| 371 | int smaxage = -1; |
| 372 | int maxage = -1; |
| 373 | |
| 374 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 375 | ctx.idx = 0; |
| 376 | |
| 377 | /* loop on the Cache-Control values */ |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 378 | 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] | 379 | char *directive = ctx.line + ctx.val; |
| 380 | char *value; |
| 381 | |
| 382 | value = directive_value(directive, ctx.vlen, "s-maxage", 8); |
| 383 | if (value) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 384 | struct buffer *chk = get_trash_chunk(); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 385 | |
| 386 | chunk_strncat(chk, value, ctx.vlen - 8 + 1); |
| 387 | chunk_strncat(chk, "", 1); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 388 | maxage = atoi(chk->area); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7); |
| 392 | if (value) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 393 | struct buffer *chk = get_trash_chunk(); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 394 | |
| 395 | chunk_strncat(chk, value, ctx.vlen - 7 + 1); |
| 396 | chunk_strncat(chk, "", 1); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 397 | smaxage = atoi(chk->area); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
| 401 | /* TODO: Expires - Data */ |
| 402 | |
| 403 | |
| 404 | if (smaxage > 0) |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 405 | return MIN(smaxage, cache->maxage); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 406 | |
| 407 | if (maxage > 0) |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 408 | return MIN(maxage, cache->maxage); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 409 | |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 410 | return cache->maxage; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 411 | |
| 412 | } |
| 413 | |
| 414 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 415 | static void cache_free_blocks(struct shared_block *first, struct shared_block *block) |
| 416 | { |
Willy Tarreau | 5bd37fa | 2018-04-04 20:17:03 +0200 | [diff] [blame] | 417 | struct cache_entry *object = (struct cache_entry *)block->data; |
| 418 | |
| 419 | if (first == block && object->eb.key) |
| 420 | eb32_delete(&object->eb); |
| 421 | object->eb.key = 0; |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 422 | } |
| 423 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 424 | /* |
| 425 | * This fonction will store the headers of the response in a buffer and then |
| 426 | * register a filter to store the data |
| 427 | */ |
| 428 | enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px, |
| 429 | struct session *sess, struct stream *s, int flags) |
| 430 | { |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 431 | unsigned int age; |
| 432 | long long hdr_age; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 433 | struct http_txn *txn = s->txn; |
| 434 | struct http_msg *msg = &txn->rsp; |
| 435 | struct filter *filter; |
| 436 | struct hdr_ctx ctx; |
| 437 | struct shared_block *first = NULL; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 438 | struct cache *cache = (struct cache *)rule->arg.act.p[0]; |
| 439 | struct shared_context *shctx = shctx_ptr(cache); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 440 | struct cache_entry *object; |
| 441 | |
| 442 | |
| 443 | /* Don't cache if the response came from a cache */ |
| 444 | if ((obj_type(s->target) == OBJ_TYPE_APPLET) && |
| 445 | s->target == &http_cache_applet.obj_type) { |
| 446 | goto out; |
| 447 | } |
| 448 | |
| 449 | /* cache only HTTP/1.1 */ |
| 450 | if (!(txn->req.flags & HTTP_MSGF_VER_11)) |
| 451 | goto out; |
| 452 | |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 453 | /* Do not cache too big objects. */ |
| 454 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 && |
| 455 | msg->sov + msg->body_len > shctx->max_obj_size) |
| 456 | goto out; |
| 457 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 458 | /* cache only GET method */ |
| 459 | if (txn->meth != HTTP_METH_GET) |
| 460 | goto out; |
| 461 | |
| 462 | /* cache only 200 status code */ |
| 463 | if (txn->status != 200) |
| 464 | goto out; |
| 465 | |
| 466 | /* Does not manage Vary at the moment. We will need a secondary key later for that */ |
| 467 | ctx.idx = 0; |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 468 | if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 469 | goto out; |
| 470 | |
Willy Tarreau | faf2909 | 2017-12-21 15:59:17 +0100 | [diff] [blame] | 471 | check_response_for_cacheability(s, &s->res); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 472 | |
Willy Tarreau | d4569d1 | 2017-12-22 18:03:04 +0100 | [diff] [blame] | 473 | if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK)) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 474 | goto out; |
| 475 | |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 476 | age = 0; |
| 477 | ctx.idx = 0; |
| 478 | if (http_find_header2("Age", 3, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) { |
| 479 | if (!strl2llrc(ctx.line + ctx.val, ctx.vlen, &hdr_age) && hdr_age > 0) { |
| 480 | if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE)) |
| 481 | hdr_age = CACHE_ENTRY_MAX_AGE; |
| 482 | age = hdr_age; |
| 483 | } |
| 484 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 485 | } |
| 486 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 487 | shctx_lock(shctx); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 488 | 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] | 489 | if (!first) { |
| 490 | shctx_unlock(shctx); |
| 491 | goto out; |
| 492 | } |
| 493 | shctx_unlock(shctx); |
| 494 | |
Willy Tarreau | 1093a45 | 2018-04-06 19:02:25 +0200 | [diff] [blame] | 495 | /* the received memory is not initialized, we need at least to mark |
| 496 | * the object as not indexed yet. |
| 497 | */ |
| 498 | object = (struct cache_entry *)first->data; |
| 499 | object->eb.node.leaf_p = NULL; |
| 500 | object->eb.key = 0; |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 501 | object->age = age; |
| 502 | object->eoh = msg->eoh; |
Willy Tarreau | 1093a45 | 2018-04-06 19:02:25 +0200 | [diff] [blame] | 503 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 504 | /* reserve space for the cache_entry structure */ |
| 505 | first->len = sizeof(struct cache_entry); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 506 | first->last_append = NULL; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 507 | /* cache the headers in a http action because it allows to chose what |
| 508 | * to cache, for example you might want to cache a response before |
| 509 | * modifying some HTTP headers, or on the contrary after modifying |
| 510 | * those headers. |
| 511 | */ |
| 512 | |
| 513 | /* does not need to be locked because it's in the "hot" list, |
| 514 | * copy the headers */ |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 515 | if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)ci_head(&s->res), msg->sov) < 0) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 516 | goto out; |
| 517 | |
| 518 | /* register the buffer in the filter ctx for filling it with data*/ |
| 519 | if (!LIST_ISEMPTY(&s->strm_flt.filters)) { |
| 520 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 521 | if (filter->config->id == cache_store_flt_id && |
| 522 | filter->config->conf == rule->arg.act.p[0]) { |
| 523 | if (filter->ctx) { |
| 524 | struct cache_st *cache_ctx = filter->ctx; |
Willy Tarreau | c9bd34c | 2017-12-22 17:42:46 +0100 | [diff] [blame] | 525 | struct cache_entry *old; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 526 | |
| 527 | cache_ctx->first_block = first; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 528 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 529 | object->eb.key = (*(unsigned int *)&txn->cache_hash); |
| 530 | memcpy(object->hash, txn->cache_hash, sizeof(object->hash)); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 531 | /* Insert the node later on caching success */ |
| 532 | |
| 533 | shctx_lock(shctx); |
Willy Tarreau | c9bd34c | 2017-12-22 17:42:46 +0100 | [diff] [blame] | 534 | |
| 535 | old = entry_exist(cache, txn->cache_hash); |
| 536 | if (old) { |
| 537 | eb32_delete(&old->eb); |
| 538 | old->eb.key = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 539 | } |
| 540 | shctx_unlock(shctx); |
| 541 | |
| 542 | /* store latest value and expiration time */ |
| 543 | object->latest_validation = now.tv_sec; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 544 | object->expire = now.tv_sec + http_calc_maxage(s, cache); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 545 | } |
| 546 | return ACT_RET_CONT; |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | out: |
| 552 | /* if does not cache */ |
| 553 | if (first) { |
| 554 | shctx_lock(shctx); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 555 | first->len = 0; |
| 556 | object->eb.key = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 557 | shctx_row_dec_hot(shctx, first); |
| 558 | shctx_unlock(shctx); |
| 559 | } |
| 560 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 561 | return ACT_RET_CONT; |
| 562 | } |
| 563 | |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 564 | #define HTTP_CACHE_INIT 0 /* Initial state. */ |
| 565 | #define HTTP_CACHE_HEADER 1 /* Cache entry headers forwarded. */ |
| 566 | #define HTTP_CACHE_FWD 2 /* Cache entry completely forwarded. */ |
| 567 | #define HTTP_CACHE_END 3 /* Cache entry treatment terminated. */ |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 568 | |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 569 | static void http_cache_applet_release(struct appctx *appctx) |
| 570 | { |
| 571 | struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0]; |
| 572 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 573 | struct shared_block *first = block_ptr(cache_ptr); |
| 574 | |
| 575 | shctx_lock(shctx_ptr(cache)); |
| 576 | shctx_row_dec_hot(shctx_ptr(cache), first); |
| 577 | shctx_unlock(shctx_ptr(cache)); |
| 578 | } |
| 579 | |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 580 | /* |
| 581 | * Append an "Age" header into <chn> channel for this <ce> cache entry. |
Joseph Herlant | 8dae5b3 | 2018-11-15 14:07:53 -0800 | [diff] [blame] | 582 | * 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] | 583 | * data in the channel. |
| 584 | * |
| 585 | * Returns the number of bytes inserted if succeeded, 0 if failed. |
| 586 | */ |
| 587 | static int cache_channel_append_age_header(struct cache_entry *ce, struct channel *chn) |
| 588 | { |
| 589 | unsigned int age; |
| 590 | |
| 591 | age = MAX(0, (int)(now.tv_sec - ce->latest_validation)) + ce->age; |
| 592 | if (unlikely(age > CACHE_ENTRY_MAX_AGE)) |
| 593 | age = CACHE_ENTRY_MAX_AGE; |
| 594 | |
| 595 | chunk_reset(&trash); |
| 596 | chunk_printf(&trash, "Age: %u", age); |
| 597 | |
| 598 | return ci_insert_line2(chn, ce->eoh, trash.area, trash.data); |
| 599 | } |
| 600 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 601 | static int cache_channel_row_data_get(struct appctx *appctx, int len) |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 602 | { |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 603 | int ret, total; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 604 | struct stream_interface *si = appctx->owner; |
| 605 | struct channel *res = si_ic(si); |
| 606 | struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0]; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 607 | struct shared_context *shctx = shctx_ptr(cache); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 608 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 609 | struct shared_block *blk, *next = appctx->ctx.cache.next; |
| 610 | int offset; |
| 611 | |
| 612 | total = 0; |
| 613 | offset = 0; |
| 614 | |
| 615 | if (!next) { |
| 616 | offset = sizeof(struct cache_entry); |
| 617 | next = block_ptr(cache_ptr); |
| 618 | } |
| 619 | |
| 620 | blk = next; |
| 621 | list_for_each_entry_from(blk, &shctx->hot, list) { |
| 622 | int sz; |
| 623 | |
| 624 | if (len <= 0) |
| 625 | break; |
| 626 | |
| 627 | sz = MIN(len, shctx->block_size - offset); |
| 628 | |
| 629 | ret = ci_putblk(res, (const char *)blk->data + offset, sz); |
| 630 | if (unlikely(offset)) |
| 631 | offset = 0; |
| 632 | if (ret <= 0) { |
| 633 | if (ret == -3 || ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 634 | si_rx_room_blk(si); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 635 | break; |
| 636 | } |
| 637 | return -1; |
| 638 | } |
| 639 | |
| 640 | total += sz; |
| 641 | len -= sz; |
| 642 | } |
| 643 | appctx->ctx.cache.next = blk; |
| 644 | |
| 645 | return total; |
| 646 | } |
| 647 | |
| 648 | static void http_cache_io_handler(struct appctx *appctx) |
| 649 | { |
| 650 | struct stream_interface *si = appctx->owner; |
| 651 | struct channel *res = si_ic(si); |
| 652 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 653 | struct shared_block *first = block_ptr(cache_ptr); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 654 | int *sent = &appctx->ctx.cache.sent; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 655 | |
| 656 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 657 | goto out; |
| 658 | |
Joseph Herlant | 8dae5b3 | 2018-11-15 14:07:53 -0800 | [diff] [blame] | 659 | /* Check if the input buffer is available. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 660 | if (res->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 661 | /* buf.size==0 means we failed to get a buffer and were |
| 662 | * already subscribed to a wait list to get a buffer. |
| 663 | */ |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 664 | goto out; |
| 665 | } |
| 666 | |
| 667 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
| 668 | appctx->st0 = HTTP_CACHE_END; |
| 669 | |
| 670 | /* buffer are aligned there, should be fine */ |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 671 | 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] | 672 | int len = first->len - *sent - sizeof(struct cache_entry); |
William Lallemand | 55e7674 | 2017-11-21 20:01:28 +0100 | [diff] [blame] | 673 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 674 | if (len > 0) { |
| 675 | int ret; |
| 676 | |
| 677 | ret = cache_channel_row_data_get(appctx, len); |
| 678 | if (ret == -1) |
| 679 | appctx->st0 = HTTP_CACHE_END; |
| 680 | else |
| 681 | *sent += ret; |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 682 | if (appctx->st0 == HTTP_CACHE_INIT && *sent > cache_ptr->eoh && |
| 683 | cache_channel_append_age_header(cache_ptr, res)) |
| 684 | appctx->st0 = HTTP_CACHE_HEADER; |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 685 | } |
| 686 | else { |
| 687 | *sent = 0; |
| 688 | appctx->st0 = HTTP_CACHE_FWD; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 689 | } |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | if (appctx->st0 == HTTP_CACHE_FWD) { |
| 693 | /* eat the whole request */ |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 694 | 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] | 695 | res->flags |= CF_READ_NULL; |
| 696 | si_shutr(si); |
| 697 | } |
| 698 | |
| 699 | if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) |
| 700 | si_shutw(si); |
| 701 | out: |
| 702 | ; |
| 703 | } |
| 704 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 705 | enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy, |
| 706 | struct act_rule *rule, char **err) |
| 707 | { |
| 708 | struct flt_conf *fconf; |
| 709 | int cur_arg = *orig_arg; |
| 710 | rule->action = ACT_CUSTOM; |
| 711 | rule->action_ptr = http_action_store_cache; |
| 712 | |
| 713 | if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 714 | memprintf(err, "expects a cache name"); |
| 715 | return ACT_RET_PRS_ERR; |
| 716 | } |
| 717 | |
| 718 | /* check if a cache filter was already registered with this cache |
| 719 | * name, if that's the case, must use it. */ |
| 720 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
| 721 | if (fconf->id == cache_store_flt_id && !strcmp((char *)fconf->conf, args[cur_arg])) { |
| 722 | rule->arg.act.p[0] = fconf->conf; |
| 723 | (*orig_arg)++; |
| 724 | /* filter already registered */ |
| 725 | return ACT_RET_PRS_OK; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | rule->arg.act.p[0] = strdup(args[cur_arg]); |
| 730 | if (!rule->arg.act.p[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 731 | ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 732 | err++; |
| 733 | goto err; |
| 734 | } |
| 735 | /* register a filter to fill the cache buffer */ |
| 736 | fconf = calloc(1, sizeof(*fconf)); |
| 737 | if (!fconf) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 738 | ha_alert("config: %s '%s': out of memory\n", |
| 739 | proxy_type_str(proxy), proxy->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 740 | err++; |
| 741 | goto err; |
| 742 | } |
| 743 | fconf->id = cache_store_flt_id; |
| 744 | fconf->conf = rule->arg.act.p[0]; /* store the proxy name */ |
| 745 | fconf->ops = &cache_ops; |
| 746 | LIST_ADDQ(&proxy->filter_configs, &fconf->list); |
| 747 | |
| 748 | (*orig_arg)++; |
| 749 | |
| 750 | return ACT_RET_PRS_OK; |
| 751 | |
| 752 | err: |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 753 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 754 | } |
| 755 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 756 | /* This produces a sha1 hash of the concatenation of the first |
| 757 | * occurrence of the Host header followed by the path component if it |
| 758 | * begins with a slash ('/'). */ |
| 759 | int sha1_hosturi(struct http_txn *txn) |
| 760 | { |
| 761 | struct hdr_ctx ctx; |
| 762 | |
| 763 | blk_SHA_CTX sha1_ctx; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 764 | struct buffer *trash; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 765 | char *path; |
| 766 | char *end; |
| 767 | trash = get_trash_chunk(); |
| 768 | |
| 769 | /* retrive the host */ |
| 770 | ctx.idx = 0; |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 771 | if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 772 | return 0; |
| 773 | chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen); |
| 774 | |
| 775 | /* now retrieve the path */ |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 776 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 777 | path = http_txn_get_path(txn); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 778 | if (!path) |
| 779 | return 0; |
| 780 | chunk_strncat(trash, path, end - path); |
| 781 | |
| 782 | /* hash everything */ |
| 783 | blk_SHA1_Init(&sha1_ctx); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 784 | blk_SHA1_Update(&sha1_ctx, trash->area, trash->data); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 785 | blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx); |
| 786 | |
| 787 | return 1; |
| 788 | } |
| 789 | |
| 790 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 791 | |
| 792 | enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px, |
| 793 | struct session *sess, struct stream *s, int flags) |
| 794 | { |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 795 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 796 | struct cache_entry *res; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 797 | struct cache *cache = (struct cache *)rule->arg.act.p[0]; |
| 798 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 799 | check_request_for_cacheability(s, &s->req); |
| 800 | if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE) |
| 801 | return ACT_RET_CONT; |
| 802 | |
Willy Tarreau | 7704b1e | 2017-12-22 16:32:43 +0100 | [diff] [blame] | 803 | if (!sha1_hosturi(s->txn)) |
| 804 | return ACT_RET_CONT; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 805 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 806 | if (s->txn->flags & TX_CACHE_IGNORE) |
| 807 | return ACT_RET_CONT; |
| 808 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 809 | shctx_lock(shctx_ptr(cache)); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 810 | res = entry_exist(cache, s->txn->cache_hash); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 811 | if (res) { |
| 812 | struct appctx *appctx; |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 813 | shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res)); |
| 814 | shctx_unlock(shctx_ptr(cache)); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 815 | s->target = &http_cache_applet.obj_type; |
| 816 | if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) { |
| 817 | appctx->st0 = HTTP_CACHE_INIT; |
| 818 | appctx->rule = rule; |
| 819 | appctx->ctx.cache.entry = res; |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 820 | appctx->ctx.cache.next = NULL; |
| 821 | appctx->ctx.cache.sent = 0; |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 822 | return ACT_RET_CONT; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 823 | } else { |
William Lallemand | 55e7674 | 2017-11-21 20:01:28 +0100 | [diff] [blame] | 824 | shctx_lock(shctx_ptr(cache)); |
| 825 | shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res)); |
| 826 | shctx_unlock(shctx_ptr(cache)); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 827 | return ACT_RET_YIELD; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 828 | } |
| 829 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 830 | shctx_unlock(shctx_ptr(cache)); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 831 | return ACT_RET_CONT; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | |
| 835 | enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy, |
| 836 | struct act_rule *rule, char **err) |
| 837 | { |
| 838 | int cur_arg = *orig_arg; |
| 839 | |
| 840 | rule->action = ACT_CUSTOM; |
| 841 | rule->action_ptr = http_action_req_cache_use; |
| 842 | |
| 843 | if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 844 | memprintf(err, "expects a cache name"); |
| 845 | return ACT_RET_PRS_ERR; |
| 846 | } |
| 847 | |
| 848 | rule->arg.act.p[0] = strdup(args[cur_arg]); |
| 849 | if (!rule->arg.act.p[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 850 | ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 851 | err++; |
| 852 | goto err; |
| 853 | } |
| 854 | |
| 855 | (*orig_arg)++; |
| 856 | return ACT_RET_PRS_OK; |
| 857 | |
| 858 | err: |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 859 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 860 | |
| 861 | } |
| 862 | |
| 863 | int cfg_parse_cache(const char *file, int linenum, char **args, int kwm) |
| 864 | { |
| 865 | int err_code = 0; |
| 866 | |
| 867 | if (strcmp(args[0], "cache") == 0) { /* new cache section */ |
| 868 | |
| 869 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 870 | ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n", |
| 871 | file, linenum, args[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 872 | err_code |= ERR_ALERT | ERR_ABORT; |
| 873 | goto out; |
| 874 | } |
| 875 | |
| 876 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 877 | err_code |= ERR_ABORT; |
| 878 | goto out; |
| 879 | } |
| 880 | |
| 881 | if (tmp_cache_config == NULL) { |
| 882 | tmp_cache_config = calloc(1, sizeof(*tmp_cache_config)); |
| 883 | if (!tmp_cache_config) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 884 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 885 | err_code |= ERR_ALERT | ERR_ABORT; |
| 886 | goto out; |
| 887 | } |
| 888 | |
| 889 | strlcpy2(tmp_cache_config->id, args[1], 33); |
| 890 | if (strlen(args[1]) > 32) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 891 | ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n", |
| 892 | file, linenum, tmp_cache_config->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 893 | err_code |= ERR_WARN; |
| 894 | } |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 895 | tmp_cache_config->maxage = 60; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 896 | tmp_cache_config->maxblocks = 0; |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 897 | tmp_cache_config->maxobjsz = 0; |
Christopher Faulet | 1f672c5 | 2018-12-03 14:30:41 +0100 | [diff] [blame] | 898 | tmp_cache_config->flags = 0; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 899 | } |
| 900 | } else if (strcmp(args[0], "total-max-size") == 0) { |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 901 | unsigned long int maxsize; |
| 902 | char *err; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 903 | |
| 904 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 905 | err_code |= ERR_ABORT; |
| 906 | goto out; |
| 907 | } |
| 908 | |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 909 | maxsize = strtoul(args[1], &err, 10); |
| 910 | if (err == args[1] || *err != '\0') { |
| 911 | ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n", |
| 912 | file, linenum, args[1]); |
| 913 | err_code |= ERR_ABORT; |
| 914 | goto out; |
| 915 | } |
| 916 | |
| 917 | if (maxsize > (UINT_MAX >> 20)) { |
| 918 | ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n", |
| 919 | file, linenum, args[1], UINT_MAX >> 20); |
| 920 | err_code |= ERR_ABORT; |
| 921 | goto out; |
| 922 | } |
| 923 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 924 | /* size in megabytes */ |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 925 | maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 926 | tmp_cache_config->maxblocks = maxsize; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 927 | } else if (strcmp(args[0], "max-age") == 0) { |
| 928 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 929 | err_code |= ERR_ABORT; |
| 930 | goto out; |
| 931 | } |
| 932 | |
| 933 | if (!*args[1]) { |
| 934 | ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n", |
| 935 | file, linenum, args[0]); |
| 936 | err_code |= ERR_WARN; |
| 937 | } |
| 938 | |
| 939 | tmp_cache_config->maxage = atoi(args[1]); |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 940 | } else if (strcmp(args[0], "max-object-size") == 0) { |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 941 | unsigned int maxobjsz; |
| 942 | char *err; |
| 943 | |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 944 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 945 | err_code |= ERR_ABORT; |
| 946 | goto out; |
| 947 | } |
| 948 | |
| 949 | if (!*args[1]) { |
| 950 | ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n", |
| 951 | file, linenum, args[0]); |
| 952 | err_code |= ERR_WARN; |
| 953 | } |
| 954 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 955 | maxobjsz = strtoul(args[1], &err, 10); |
| 956 | if (err == args[1] || *err != '\0') { |
| 957 | ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n", |
| 958 | file, linenum, args[1]); |
| 959 | err_code |= ERR_ABORT; |
| 960 | goto out; |
| 961 | } |
| 962 | tmp_cache_config->maxobjsz = maxobjsz; |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 963 | } |
| 964 | else if (*args[0] != 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 965 | 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] | 966 | err_code |= ERR_ALERT | ERR_FATAL; |
| 967 | goto out; |
| 968 | } |
| 969 | out: |
| 970 | return err_code; |
| 971 | } |
| 972 | |
| 973 | /* once the cache section is parsed */ |
| 974 | |
| 975 | int cfg_post_parse_section_cache() |
| 976 | { |
| 977 | struct shared_context *shctx; |
| 978 | int err_code = 0; |
| 979 | int ret_shctx; |
| 980 | |
| 981 | if (tmp_cache_config) { |
| 982 | struct cache *cache; |
| 983 | |
| 984 | if (tmp_cache_config->maxblocks <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 985 | 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] | 986 | err_code |= ERR_FATAL | ERR_ALERT; |
| 987 | goto out; |
| 988 | } |
| 989 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 990 | if (!tmp_cache_config->maxobjsz) { |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 991 | /* Default max. file size is a 256th of the cache size. */ |
| 992 | tmp_cache_config->maxobjsz = |
| 993 | (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8; |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 994 | } |
| 995 | else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) { |
| 996 | ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2); |
| 997 | err_code |= ERR_FATAL | ERR_ALERT; |
| 998 | goto out; |
| 999 | } |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1000 | |
| 1001 | ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE, |
| 1002 | tmp_cache_config->maxobjsz, sizeof(struct cache), 1); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1003 | |
Frédéric Lécaille | bc58449 | 2018-10-25 20:18:59 +0200 | [diff] [blame] | 1004 | if (ret_shctx <= 0) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1005 | if (ret_shctx == SHCTX_E_INIT_LOCK) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1006 | ha_alert("Unable to initialize the lock for the cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1007 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1008 | ha_alert("Unable to allocate cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1009 | |
| 1010 | err_code |= ERR_FATAL | ERR_ALERT; |
| 1011 | goto out; |
| 1012 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1013 | shctx->free_block = cache_free_blocks; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1014 | memcpy(shctx->data, tmp_cache_config, sizeof(struct cache)); |
| 1015 | cache = (struct cache *)shctx->data; |
| 1016 | cache->entries = EB_ROOT_UNIQUE; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1017 | LIST_ADDQ(&caches, &cache->list); |
| 1018 | } |
| 1019 | out: |
| 1020 | free(tmp_cache_config); |
| 1021 | tmp_cache_config = NULL; |
| 1022 | return err_code; |
| 1023 | |
| 1024 | } |
| 1025 | |
| 1026 | /* |
| 1027 | * Resolve the cache name to a pointer once the file is completely read. |
| 1028 | */ |
| 1029 | int cfg_cache_postparser() |
| 1030 | { |
| 1031 | struct act_rule *hresrule, *hrqrule; |
| 1032 | void *cache_ptr; |
| 1033 | struct cache *cache; |
| 1034 | struct proxy *curproxy = NULL; |
| 1035 | int err = 0; |
| 1036 | struct flt_conf *fconf; |
| 1037 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1038 | for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1039 | |
| 1040 | /* resolve the http response cache name to a ptr in the action rule */ |
| 1041 | list_for_each_entry(hresrule, &curproxy->http_res_rules, list) { |
| 1042 | if (hresrule->action != ACT_CUSTOM || |
| 1043 | hresrule->action_ptr != http_action_store_cache) |
| 1044 | continue; |
| 1045 | |
| 1046 | cache_ptr = hresrule->arg.act.p[0]; |
| 1047 | |
| 1048 | list_for_each_entry(cache, &caches, list) { |
| 1049 | if (!strcmp(cache->id, cache_ptr)) { |
| 1050 | /* don't free there, it's still used in the filter conf */ |
| 1051 | cache_ptr = cache; |
Christopher Faulet | 1f672c5 | 2018-12-03 14:30:41 +0100 | [diff] [blame] | 1052 | cache->flags |= ((curproxy->options2 & PR_O2_USE_HTX) |
| 1053 | ? CACHE_F_HTX |
| 1054 | : CACHE_F_LEGACY_HTTP); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1055 | break; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | if (cache_ptr == hresrule->arg.act.p[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1060 | ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n", |
| 1061 | curproxy->id, (char *)hresrule->arg.act.p[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1062 | err++; |
| 1063 | } |
| 1064 | |
| 1065 | hresrule->arg.act.p[0] = cache_ptr; |
| 1066 | } |
| 1067 | |
| 1068 | /* resolve the http request cache name to a ptr in the action rule */ |
| 1069 | list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) { |
| 1070 | if (hrqrule->action != ACT_CUSTOM || |
| 1071 | hrqrule->action_ptr != http_action_req_cache_use) |
| 1072 | continue; |
| 1073 | |
| 1074 | cache_ptr = hrqrule->arg.act.p[0]; |
| 1075 | |
| 1076 | list_for_each_entry(cache, &caches, list) { |
| 1077 | if (!strcmp(cache->id, cache_ptr)) { |
| 1078 | free(cache_ptr); |
| 1079 | cache_ptr = cache; |
Christopher Faulet | 1f672c5 | 2018-12-03 14:30:41 +0100 | [diff] [blame] | 1080 | cache->flags |= ((curproxy->options2 & PR_O2_USE_HTX) |
| 1081 | ? CACHE_F_HTX |
| 1082 | : CACHE_F_LEGACY_HTTP); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1083 | break; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | if (cache_ptr == hrqrule->arg.act.p[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1088 | ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n", |
| 1089 | curproxy->id, (char *)hrqrule->arg.act.p[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1090 | err++; |
| 1091 | } |
| 1092 | |
| 1093 | hrqrule->arg.act.p[0] = cache_ptr; |
| 1094 | } |
| 1095 | |
| 1096 | /* resolve the cache name to a ptr in the filter config */ |
| 1097 | list_for_each_entry(fconf, &curproxy->filter_configs, list) { |
| 1098 | |
William Lallemand | 9c54c53 | 2017-11-02 16:38:42 +0100 | [diff] [blame] | 1099 | if (fconf->id != cache_store_flt_id) |
| 1100 | continue; |
| 1101 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1102 | cache_ptr = fconf->conf; |
| 1103 | |
| 1104 | list_for_each_entry(cache, &caches, list) { |
| 1105 | if (!strcmp(cache->id, cache_ptr)) { |
| 1106 | /* there can be only one filter per cache, so we free it there */ |
| 1107 | free(cache_ptr); |
| 1108 | cache_ptr = cache; |
Christopher Faulet | 1f672c5 | 2018-12-03 14:30:41 +0100 | [diff] [blame] | 1109 | cache->flags |= ((curproxy->options2 & PR_O2_USE_HTX) |
| 1110 | ? CACHE_F_HTX |
| 1111 | : CACHE_F_LEGACY_HTTP); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1112 | break; |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | if (cache_ptr == fconf->conf) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1117 | ha_alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n", |
| 1118 | curproxy->id, (char *)fconf->conf); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1119 | err++; |
| 1120 | } |
| 1121 | fconf->conf = cache_ptr; |
| 1122 | } |
| 1123 | } |
Christopher Faulet | 1f672c5 | 2018-12-03 14:30:41 +0100 | [diff] [blame] | 1124 | |
| 1125 | /* Check if the cache is used by HTX and legacy HTTP proxies in same |
| 1126 | * time |
| 1127 | */ |
| 1128 | list_for_each_entry(cache, &caches, list) { |
| 1129 | if ((cache->flags & (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) == (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) { |
| 1130 | ha_alert("Cache '%s': cannot be used by HTX and legacy HTTP proxies in same time.\n", |
| 1131 | cache->id); |
| 1132 | err++; |
| 1133 | } |
| 1134 | } |
| 1135 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1136 | return err; |
| 1137 | } |
| 1138 | |
| 1139 | |
| 1140 | struct flt_ops cache_ops = { |
| 1141 | .init = cache_store_init, |
| 1142 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1143 | /* Handle channels activity */ |
| 1144 | .channel_start_analyze = cache_store_chn_start_analyze, |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 1145 | .channel_end_analyze = cache_store_chn_end_analyze, |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1146 | |
| 1147 | /* Filter HTTP requests and responses */ |
| 1148 | .http_headers = cache_store_http_headers, |
| 1149 | .http_end = cache_store_http_end, |
| 1150 | |
| 1151 | .http_forward_data = cache_store_http_forward_data, |
| 1152 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1153 | }; |
| 1154 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 1155 | 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] | 1156 | { |
| 1157 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1158 | return 1; |
| 1159 | |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
| 1163 | static int cli_io_handler_show_cache(struct appctx *appctx) |
| 1164 | { |
| 1165 | struct cache* cache = appctx->ctx.cli.p0; |
| 1166 | struct stream_interface *si = appctx->owner; |
| 1167 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1168 | if (cache == NULL) { |
| 1169 | cache = LIST_ELEM((caches).n, typeof(struct cache *), list); |
| 1170 | } |
| 1171 | |
| 1172 | list_for_each_entry_from(cache, &caches, list) { |
| 1173 | struct eb32_node *node = NULL; |
| 1174 | unsigned int next_key; |
| 1175 | struct cache_entry *entry; |
| 1176 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1177 | next_key = appctx->ctx.cli.i0; |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1178 | if (!next_key) { |
| 1179 | chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav); |
| 1180 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1181 | si_rx_room_blk(si); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1182 | return 0; |
| 1183 | } |
| 1184 | } |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1185 | |
| 1186 | appctx->ctx.cli.p0 = cache; |
| 1187 | |
| 1188 | while (1) { |
| 1189 | |
| 1190 | shctx_lock(shctx_ptr(cache)); |
| 1191 | node = eb32_lookup_ge(&cache->entries, next_key); |
| 1192 | if (!node) { |
| 1193 | shctx_unlock(shctx_ptr(cache)); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1194 | appctx->ctx.cli.i0 = 0; |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1195 | break; |
| 1196 | } |
| 1197 | |
| 1198 | entry = container_of(node, struct cache_entry, eb); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1199 | 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] | 1200 | |
| 1201 | next_key = node->key + 1; |
| 1202 | appctx->ctx.cli.i0 = next_key; |
| 1203 | |
| 1204 | shctx_unlock(shctx_ptr(cache)); |
| 1205 | |
| 1206 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1207 | si_rx_room_blk(si); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1208 | return 0; |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | } |
| 1213 | |
| 1214 | return 1; |
| 1215 | |
| 1216 | } |
| 1217 | |
| 1218 | static struct cli_kw_list cli_kws = {{},{ |
William Lallemand | e899af8 | 2017-11-22 16:41:26 +0100 | [diff] [blame] | 1219 | { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL }, |
| 1220 | {{},} |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1221 | }}; |
| 1222 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1223 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1224 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1225 | static struct action_kw_list http_res_actions = { |
| 1226 | .kw = { |
| 1227 | { "cache-store", parse_cache_store }, |
| 1228 | { NULL, NULL } |
| 1229 | } |
| 1230 | }; |
| 1231 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1232 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions); |
| 1233 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1234 | static struct action_kw_list http_req_actions = { |
| 1235 | .kw = { |
| 1236 | { "cache-use", parse_cache_use }, |
| 1237 | { NULL, NULL } |
| 1238 | } |
| 1239 | }; |
| 1240 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1241 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 1242 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1243 | struct applet http_cache_applet = { |
| 1244 | .obj_type = OBJ_TYPE_APPLET, |
| 1245 | .name = "<CACHE>", /* used for logging */ |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1246 | .fct = http_cache_io_handler, |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 1247 | .release = http_cache_applet_release, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1248 | }; |
| 1249 | |
Willy Tarreau | e655251 | 2018-11-26 11:33:13 +0100 | [diff] [blame] | 1250 | /* config parsers for this section */ |
| 1251 | REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache); |
| 1252 | REGISTER_CONFIG_POSTPARSER("cache", cfg_cache_postparser); |