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