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