blob: d23fe35529cc7c780c41391d83d284f57595484c [file] [log] [blame]
William Lallemand41db4602017-10-30 11:15:51 +01001/*
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 Lallemand41db4602017-10-30 11:15:51 +010013#include <eb32tree.h>
William Lallemandf528fff2017-11-23 19:43:17 +010014#include <import/sha1.h>
William Lallemand41db4602017-10-30 11:15:51 +010015
William Lallemand75d93292017-11-21 20:01:24 +010016#include <types/action.h>
William Lallemand1f49a362017-11-21 20:01:26 +010017#include <types/cli.h>
William Lallemand75d93292017-11-21 20:01:24 +010018#include <types/filters.h>
19#include <types/proxy.h>
20#include <types/shctx.h>
21
William Lallemand41db4602017-10-30 11:15:51 +010022#include <proto/channel.h>
William Lallemand1f49a362017-11-21 20:01:26 +010023#include <proto/cli.h>
William Lallemand41db4602017-10-30 11:15:51 +010024#include <proto/proxy.h>
25#include <proto/hdr_idx.h>
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010026#include <proto/http_htx.h>
William Lallemand41db4602017-10-30 11:15:51 +010027#include <proto/filters.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020028#include <proto/http_rules.h>
William Lallemand41db4602017-10-30 11:15:51 +010029#include <proto/proto_http.h>
30#include <proto/log.h>
31#include <proto/stream.h>
32#include <proto/stream_interface.h>
33#include <proto/shctx.h>
34
William Lallemand41db4602017-10-30 11:15:51 +010035
36#include <common/cfgparse.h>
37#include <common/hash.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010038#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010039#include <common/initcall.h>
William Lallemand41db4602017-10-30 11:15:51 +010040
41/* flt_cache_store */
Christopher Faulet1f672c52018-12-03 14:30:41 +010042#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 Lallemand41db4602017-10-30 11:15:51 +010045
Christopher Faulet27d93c32018-12-15 22:32:02 +010046#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010047 * the filter keyword) */
Christopher Fauletafd819c2018-12-11 08:57:45 +010048
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010049const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010050
Willy Tarreau2231b632019-03-29 18:26:52 +010051extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010052
53struct flt_ops cache_ops;
54
55struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010056 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010057 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010058 unsigned int maxage; /* max-age */
59 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020060 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010061 char id[33]; /* cache name */
Christopher Faulet1f672c52018-12-03 14:30:41 +010062 unsigned int flags; /* CACHE_F_* */
William Lallemand41db4602017-10-30 11:15:51 +010063};
64
Christopher Faulet95220e22018-12-07 17:34:39 +010065/* cache config for filters */
66struct cache_flt_conf {
67 union {
68 struct cache *cache; /* cache used by the filter */
69 char *name; /* cache name used during conf parsing */
70 } c;
71 unsigned int flags; /* CACHE_FLT_F_* */
72};
73
William Lallemand41db4602017-10-30 11:15:51 +010074/*
75 * cache ctx for filters
76 */
77struct cache_st {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010078 int hdrs_len; // field used in legacy mode only
William Lallemand41db4602017-10-30 11:15:51 +010079 struct shared_block *first_block;
80};
81
82struct cache_entry {
83 unsigned int latest_validation; /* latest validation date */
84 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020085 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010086 unsigned int eoh; /* Origin server end of headers offset. */ // field used in legacy mode only
87
William Lallemand41db4602017-10-30 11:15:51 +010088 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010089 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010090 unsigned char data[0];
91};
92
93#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010094#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010095
96static struct list caches = LIST_HEAD_INIT(caches);
97static struct cache *tmp_cache_config = NULL;
98
Willy Tarreau8ceae722018-11-26 11:58:30 +010099DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
100
William Lallemandf528fff2017-11-23 19:43:17 +0100101struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100102{
103 struct eb32_node *node;
104 struct cache_entry *entry;
105
William Lallemandf528fff2017-11-23 19:43:17 +0100106 node = eb32_lookup(&cache->entries, (*(unsigned int *)hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100107 if (!node)
108 return NULL;
109
110 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100111
112 /* if that's not the right node */
113 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
114 return NULL;
115
William Lallemand08727662017-11-21 20:01:27 +0100116 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100117 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100118 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100119 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100120 entry->eb.key = 0;
121 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100122 return NULL;
123
124}
125
126static 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 Lallemand77c11972017-10-31 20:43:01 +0100131static 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 Lallemand41db4602017-10-30 11:15:51 +0100138static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100139cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100140{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100141 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100142 return 0;
143}
144
Christopher Faulet95220e22018-12-07 17:34:39 +0100145static void
146cache_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 Lallemand4da3f8a2017-10-31 14:33:34 +0100153static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100154cache_store_check(struct proxy *px, struct flt_conf *fconf)
155{
156 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100157 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100158 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100159 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100160
161 /* resolve the cache name to a ptr in the filter config */
162 list_for_each_entry(cache, &caches, list) {
163 if (!strcmp(cache->id, cconf->c.name)) {
164 /* there can be only one filter per cache, so we free it there */
165 cache->flags |= ((px->options2 & PR_O2_USE_HTX)
166 ? CACHE_F_HTX
167 : CACHE_F_LEGACY_HTTP);
168
169 free(cconf->c.name);
170 cconf->c.cache = cache;
171 goto found;
172 }
173 }
174
175 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
176 proxy_type_str(px), px->id, (char *)cconf->c.name);
177 return 1;
178
179 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100180 /* Here <cache> points on the cache the filter must use and <cconf>
181 * points on the cache filter configuration. */
182
183 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100184 * enabled and if it is after the cache. When the compression is before
185 * the cache, an error is returned. Also check if the cache filter must
186 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100187 list_for_each_entry(f, &px->filter_configs, list) {
188 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100189 /* The compression filter must be evaluated after the cache. */
190 if (comp) {
191 ha_alert("config: %s '%s': unable to enable the compression filter before "
192 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
193 return 1;
194 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100195 }
Christopher Faulet27d93c32018-12-15 22:32:02 +0100196 else if (f->id == http_comp_flt_id) {
Christopher Fauletafd819c2018-12-11 08:57:45 +0100197 if (!(px->options2 & PR_O2_USE_HTX)) {
198 ha_alert("config: %s '%s' : compression and cache filters cannot be "
199 "both enabled on non HTX proxy.\n",
200 proxy_type_str(px), px->id);
201 return 1;
202 }
Christopher Faulet27d93c32018-12-15 22:32:02 +0100203 comp = 1;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100204 }
Christopher Faulet27d93c32018-12-15 22:32:02 +0100205 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
206 /* Implicit declaration is only allowed with the
207 * compression. For other filters, an implicit
208 * declaration is required. */
209 ha_alert("config: %s '%s': require an explicit filter declaration "
210 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
211 return 1;
212 }
213
Christopher Fauletafd819c2018-12-11 08:57:45 +0100214 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100215 return 0;
216}
217
218static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100219cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
220{
221 if (!(chn->flags & CF_ISRESP))
222 return 1;
223
224 if (filter->ctx == NULL) {
225 struct cache_st *st;
226
Willy Tarreaubafbe012017-11-24 17:34:44 +0100227 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100228 if (st == NULL)
229 return -1;
230
231 st->hdrs_len = 0;
232 st->first_block = NULL;
233 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100234
235 /* Register post-analyzer on AN_RES_WAIT_HTTP */
236 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100237 }
238
William Lallemand4da3f8a2017-10-31 14:33:34 +0100239 return 1;
240}
241
242static int
William Lallemand49dc0482017-11-24 14:33:54 +0100243cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
244{
245 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100246 struct cache_flt_conf *cconf = FLT_CONF(filter);
247 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100248 struct shared_context *shctx = shctx_ptr(cache);
249
250 if (!(chn->flags & CF_ISRESP))
251 return 1;
252
253 /* Everything should be released in the http_end filter, but we need to do it
254 * there too, in case of errors */
255
256 if (st && st->first_block) {
257
258 shctx_lock(shctx);
259 shctx_row_dec_hot(shctx, st->first_block);
260 shctx_unlock(shctx);
261
262 }
263 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100264 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100265 filter->ctx = NULL;
266 }
267
268 return 1;
269}
270
Christopher Faulet839791a2019-01-07 16:12:07 +0100271static int
272cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
273 unsigned an_bit)
274{
275 struct http_txn *txn = s->txn;
276 struct http_msg *msg = &txn->rsp;
277 struct cache_st *st = filter->ctx;
278
279 if (an_bit != AN_RES_WAIT_HTTP)
280 goto end;
281
282 /* Here we need to check if any compression filter precedes the cache
283 * filter. This is only possible when the compression is configured in
284 * the frontend while the cache filter is configured on the
285 * backend. This case cannot be detected during HAProxy startup. So in
286 * such cases, the cache is disabled.
287 */
288 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
289 pool_free(pool_head_cache_st, st);
290 filter->ctx = NULL;
291 }
292
293 end:
294 return 1;
295}
William Lallemand49dc0482017-11-24 14:33:54 +0100296
297static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100298cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
299{
300 struct cache_st *st = filter->ctx;
301
William Lallemand4da3f8a2017-10-31 14:33:34 +0100302 if (!(msg->chn->flags & CF_ISRESP) || !st)
303 return 1;
304
Christopher Faulet67658c92018-12-06 21:59:39 +0100305 if (st->first_block) {
306 register_data_filter(s, msg->chn, filter);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100307 if (!IS_HTX_STRM(s))
308 st->hdrs_len = msg->sov;
Christopher Faulet67658c92018-12-06 21:59:39 +0100309 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100310 return 1;
311}
312
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200313static inline void disable_cache_entry(struct cache_st *st,
314 struct filter *filter, struct shared_context *shctx)
315{
316 struct cache_entry *object;
317
318 object = (struct cache_entry *)st->first_block->data;
319 filter->ctx = NULL; /* disable cache */
320 shctx_lock(shctx);
321 shctx_row_dec_hot(shctx, st->first_block);
322 object->eb.key = 0;
323 shctx_unlock(shctx);
324 pool_free(pool_head_cache_st, st);
325}
326
William Lallemand4da3f8a2017-10-31 14:33:34 +0100327static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100328cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
329 unsigned int offset, unsigned int len)
330{
Christopher Faulet95220e22018-12-07 17:34:39 +0100331 struct cache_flt_conf *cconf = FLT_CONF(filter);
332 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100333 struct cache_st *st = filter->ctx;
334 struct htx *htx = htxbuf(&msg->chn->buf);
335 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200336 struct shared_block *fb;
337 unsigned int orig_len, to_forward;
338 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100339
340 if (!len)
341 return len;
342
343 if (!st->first_block) {
344 unregister_data_filter(s, msg->chn, filter);
345 return len;
346 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100347
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200348 chunk_reset(&trash);
349 orig_len = len;
350 to_forward = 0;
Christopher Fauletee847d42019-05-23 11:55:33 +0200351 for (blk = htx_get_first_blk(htx); blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100352 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200353 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100354 struct ist v;
355
Christopher Fauletee847d42019-05-23 11:55:33 +0200356 if (offset >= sz) {
357 offset -= sz;
358 continue;
359 }
360
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100361 switch (type) {
362 case HTX_BLK_UNUSED:
363 break;
364
365 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100366 v = htx_get_blk_value(htx, blk);
367 v.ptr += offset;
368 v.len -= offset;
369 if (v.len > len)
370 v.len = len;
371
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200372 info = (type << 28) + v.len;
373 chunk_memcat(&trash, (char *)&info, sizeof(info));
374 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100375 to_forward += v.len;
376 len -= v.len;
377 break;
378
379 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200380 /* Here offset must always be 0 because only
381 * DATA blocks can be partially transferred. */
382 if (offset)
383 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100384 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200385 goto end;
386
387 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
388 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100389 to_forward += sz;
390 len -= sz;
391 break;
392 }
393
394 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100395 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200396
397 end:
398 shctx_lock(shctx);
399 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
400 if (!fb) {
401 shctx_unlock(shctx);
402 goto no_cache;
403 }
404 shctx_unlock(shctx);
405
406 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
407 (unsigned char *)b_head(&trash), b_data(&trash));
408 if (ret < 0)
409 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100410
411 return to_forward;
412
413 no_cache:
414 disable_cache_entry(st, filter, shctx);
415 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200416 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100417}
418
419static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100420cache_store_http_forward_data(struct stream *s, struct filter *filter,
421 struct http_msg *msg, unsigned int len)
422{
423 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100424 struct cache_flt_conf *cconf = FLT_CONF(filter);
425 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100426 int ret;
427
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200428 ret = 0;
429
William Lallemand4da3f8a2017-10-31 14:33:34 +0100430 /*
431 * We need to skip the HTTP headers first, because we saved them in the
432 * http-response action.
433 */
Christopher Faulet67658c92018-12-06 21:59:39 +0100434 if (!(msg->chn->flags & CF_ISRESP) || !st) {
435 /* should never happen */
436 unregister_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100437 return len;
Christopher Faulet67658c92018-12-06 21:59:39 +0100438 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100439
440 if (!len) {
Joseph Herlant8dae5b32018-11-15 14:07:53 -0800441 /* Nothing to forward */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100442 ret = len;
443 }
William Lallemand10935bc2017-11-14 14:39:23 +0100444 else if (st->hdrs_len >= len) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100445 /* Forward part of headers */
446 ret = len;
447 st->hdrs_len -= len;
448 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100449 else {
William Lallemand10935bc2017-11-14 14:39:23 +0100450 /* Forward data */
Christopher Faulet67658c92018-12-06 21:59:39 +0100451 if (st->first_block) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200452 int to_append, append;
453 struct shared_block *fb;
454
455 to_append = MIN(ci_contig_data(msg->chn), len - st->hdrs_len);
456
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200457 shctx_lock(shctx);
458 fb = shctx_row_reserve_hot(shctx, st->first_block, to_append);
459 if (!fb) {
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100460 shctx_unlock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200461 disable_cache_entry(st, filter, shctx);
Christopher Faulet67658c92018-12-06 21:59:39 +0100462 unregister_data_filter(s, msg->chn, filter);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200463 return len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100464 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200465 shctx_unlock(shctx);
466
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200467 /* Skip remaining headers to fill the cache */
468 c_adv(msg->chn, st->hdrs_len);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200469 append = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
470 (unsigned char *)ci_head(msg->chn), to_append);
471 ret = st->hdrs_len + to_append - append;
472 /* Rewind the buffer to forward all data */
473 c_rew(msg->chn, st->hdrs_len);
474 st->hdrs_len = 0;
Christopher Faulet67658c92018-12-06 21:59:39 +0100475 if (ret < 0) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200476 disable_cache_entry(st, filter, shctx);
Christopher Faulet67658c92018-12-06 21:59:39 +0100477 unregister_data_filter(s, msg->chn, filter);
478 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100479 }
Christopher Faulet67658c92018-12-06 21:59:39 +0100480 else {
481 /* should never happen */
482 unregister_data_filter(s, msg->chn, filter);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200483 ret = len;
Christopher Faulet67658c92018-12-06 21:59:39 +0100484 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100485 }
486
487 if ((ret != len) ||
488 (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret))
489 task_wakeup(s->task, TASK_WOKEN_MSG);
490
491 return ret;
492}
493
494static int
495cache_store_http_end(struct stream *s, struct filter *filter,
496 struct http_msg *msg)
497{
498 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100499 struct cache_flt_conf *cconf = FLT_CONF(filter);
500 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100501 struct shared_context *shctx = shctx_ptr(cache);
502 struct cache_entry *object;
503
504 if (!(msg->chn->flags & CF_ISRESP))
505 return 1;
506
507 if (st && st->first_block) {
508
509 object = (struct cache_entry *)st->first_block->data;
510
511 /* does not need to test if the insertion worked, if it
512 * doesn't, the blocks will be reused anyway */
513
514 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100515 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
516 object->eb.key = 0;
517 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100518 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100519 shctx_row_dec_hot(shctx, st->first_block);
520 shctx_unlock(shctx);
521
522 }
523 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100524 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100525 filter->ctx = NULL;
526 }
527
528 return 1;
529}
530
531 /*
532 * This intends to be used when checking HTTP headers for some
533 * word=value directive. Return a pointer to the first character of value, if
534 * the word was not found or if there wasn't any value assigned ot it return NULL
535 */
536char *directive_value(const char *sample, int slen, const char *word, int wlen)
537{
538 int st = 0;
539
540 if (slen < wlen)
541 return 0;
542
543 while (wlen) {
544 char c = *sample ^ *word;
545 if (c && c != ('A' ^ 'a'))
546 return NULL;
547 sample++;
548 word++;
549 slen--;
550 wlen--;
551 }
552
553 while (slen) {
554 if (st == 0) {
555 if (*sample != '=')
556 return NULL;
557 sample++;
558 slen--;
559 st = 1;
560 continue;
561 } else {
562 return (char *)sample;
563 }
564 }
565
566 return NULL;
567}
568
569/*
570 * Return the maxage in seconds of an HTTP response.
571 * Compute the maxage using either:
572 * - the assigned max-age of the cache
573 * - the s-maxage directive
574 * - the max-age directive
575 * - (Expires - Data) headers
576 * - the default-max-age of the cache
577 *
578 */
William Lallemand49b44532017-11-24 18:53:43 +0100579int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100580{
William Lallemand4da3f8a2017-10-31 14:33:34 +0100581 int smaxage = -1;
582 int maxage = -1;
583
Christopher Fauleta63ae1e2019-07-15 20:49:46 +0200584
585 if (IS_HTX_STRM(s)) {
586 /* HTX mode */
587 struct htx *htx = htxbuf(&s->res.buf);
588 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100589
Christopher Fauleta63ae1e2019-07-15 20:49:46 +0200590 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
591 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100592
Christopher Fauleta63ae1e2019-07-15 20:49:46 +0200593 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
594 if (value) {
595 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100596
Christopher Fauleta63ae1e2019-07-15 20:49:46 +0200597 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
598 chunk_strncat(chk, "", 1);
599 maxage = atoi(chk->area);
600 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100601
Christopher Fauleta63ae1e2019-07-15 20:49:46 +0200602 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
603 if (value) {
604 struct buffer *chk = get_trash_chunk();
605
606 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
607 chunk_strncat(chk, "", 1);
608 smaxage = atoi(chk->area);
609 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100610 }
Christopher Fauleta63ae1e2019-07-15 20:49:46 +0200611 }
612 else {
613 /* Legacy mode */
614 struct http_txn *txn = s->txn;
615 struct hdr_ctx ctx;
616
617 ctx.idx = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100618
Christopher Fauleta63ae1e2019-07-15 20:49:46 +0200619 /* loop on the Cache-Control values */
620 while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) {
621 char *directive = ctx.line + ctx.val;
622 char *value;
623
624 value = directive_value(directive, ctx.vlen, "s-maxage", 8);
625 if (value) {
626 struct buffer *chk = get_trash_chunk();
627
628 chunk_strncat(chk, value, ctx.vlen - 8 + 1);
629 chunk_strncat(chk, "", 1);
630 maxage = atoi(chk->area);
631 }
632
633 value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
634 if (value) {
635 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100636
Christopher Fauleta63ae1e2019-07-15 20:49:46 +0200637 chunk_strncat(chk, value, ctx.vlen - 7 + 1);
638 chunk_strncat(chk, "", 1);
639 smaxage = atoi(chk->area);
640 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100641 }
642 }
643
644 /* TODO: Expires - Data */
645
646
647 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100648 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100649
650 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100651 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100652
William Lallemand49b44532017-11-24 18:53:43 +0100653 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100654
655}
656
657
William Lallemanda400a3a2017-11-20 19:13:12 +0100658static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
659{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200660 struct cache_entry *object = (struct cache_entry *)block->data;
661
662 if (first == block && object->eb.key)
663 eb32_delete(&object->eb);
664 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100665}
666
William Lallemand41db4602017-10-30 11:15:51 +0100667/*
668 * This fonction will store the headers of the response in a buffer and then
669 * register a filter to store the data
670 */
671enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200672 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100673{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200674 unsigned int age;
675 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100676 struct http_txn *txn = s->txn;
677 struct http_msg *msg = &txn->rsp;
678 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100679 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100680 struct cache_flt_conf *cconf = rule->arg.act.p[0];
681 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100682 struct cache_st *cache_ctx = NULL;
683 struct cache_entry *object, *old;
Willy Tarreauc9036c02019-01-11 19:38:25 +0100684 unsigned int key = *(unsigned int *)txn->cache_hash;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100685
William Lallemand4da3f8a2017-10-31 14:33:34 +0100686 /* Don't cache if the response came from a cache */
687 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
688 s->target == &http_cache_applet.obj_type) {
689 goto out;
690 }
691
692 /* cache only HTTP/1.1 */
693 if (!(txn->req.flags & HTTP_MSGF_VER_11))
694 goto out;
695
696 /* cache only GET method */
697 if (txn->meth != HTTP_METH_GET)
698 goto out;
699
Willy Tarreauc9036c02019-01-11 19:38:25 +0100700 /* cache key was not computed */
701 if (!key)
702 goto out;
703
William Lallemand4da3f8a2017-10-31 14:33:34 +0100704 /* cache only 200 status code */
705 if (txn->status != 200)
706 goto out;
707
Christopher Faulet839791a2019-01-07 16:12:07 +0100708 /* Find the corresponding filter instance for the current stream */
709 list_for_each_entry(filter, &s->strm_flt.filters, list) {
710 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
711 /* No filter ctx, don't cache anything */
712 if (!filter->ctx)
713 goto out;
714 cache_ctx = filter->ctx;
715 break;
716 }
717 }
718
719 /* from there, cache_ctx is always defined */
720
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100721 if (IS_HTX_STRM(s)) {
722 struct htx *htx = htxbuf(&s->res.buf);
723 struct http_hdr_ctx ctx;
724 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100725
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100726 /* Do not cache too big objects. */
727 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
728 htx->data + htx->extra > shctx->max_obj_size)
729 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100730
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100731 /* Does not manage Vary at the moment. We will need a secondary key later for that */
732 ctx.blk = NULL;
733 if (http_find_header(htx, ist("Vary"), &ctx, 0))
734 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100735
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100736 htx_check_response_for_cacheability(s, &s->res);
737
738 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
739 goto out;
740
741 age = 0;
742 ctx.blk = NULL;
743 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
744 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
745 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
746 hdr_age = CACHE_ENTRY_MAX_AGE;
747 age = hdr_age;
748 }
749 http_remove_header(htx, &ctx);
750 }
751
752 chunk_reset(&trash);
Christopher Fauleta3f15502019-05-13 15:27:23 +0200753 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100754 struct htx_blk *blk = htx_get_blk(htx, pos);
Christopher Fauletafd819c2018-12-11 08:57:45 +0100755 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100756 uint32_t sz = htx_get_blksz(blk);
757
758 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200759 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Fauletafd819c2018-12-11 08:57:45 +0100760 if (type == HTX_BLK_EOH)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100761 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100762 }
763 }
764 else {
765 struct hdr_ctx ctx;
766
767 /* Do not cache too big objects. */
768 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
769 msg->sov + msg->body_len > shctx->max_obj_size)
770 goto out;
771
772 /* Does not manage Vary at the moment. We will need a secondary key later for that */
773 ctx.idx = 0;
774 if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx))
775 goto out;
776
777 check_response_for_cacheability(s, &s->res);
778
779 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
780 goto out;
781
782 age = 0;
783 ctx.idx = 0;
784 if (http_find_header2("Age", 3, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
785 if (!strl2llrc(ctx.line + ctx.val, ctx.vlen, &hdr_age) && hdr_age > 0) {
786 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
787 hdr_age = CACHE_ENTRY_MAX_AGE;
788 age = hdr_age;
789 }
790 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200791 }
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200792 }
793
William Lallemand4da3f8a2017-10-31 14:33:34 +0100794 shctx_lock(shctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100795 if (IS_HTX_STRM(s))
796 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
797 else
798 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + msg->sov);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100799 if (!first) {
800 shctx_unlock(shctx);
801 goto out;
802 }
803 shctx_unlock(shctx);
804
Willy Tarreau1093a452018-04-06 19:02:25 +0200805 /* the received memory is not initialized, we need at least to mark
806 * the object as not indexed yet.
807 */
808 object = (struct cache_entry *)first->data;
809 object->eb.node.leaf_p = NULL;
810 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200811 object->age = age;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200812 if (!IS_HTX_STRM(s))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100813 object->eoh = msg->eoh;
Willy Tarreau1093a452018-04-06 19:02:25 +0200814
William Lallemand4da3f8a2017-10-31 14:33:34 +0100815 /* reserve space for the cache_entry structure */
816 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200817 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100818 /* cache the headers in a http action because it allows to chose what
819 * to cache, for example you might want to cache a response before
820 * modifying some HTTP headers, or on the contrary after modifying
821 * those headers.
822 */
823
824 /* does not need to be locked because it's in the "hot" list,
825 * copy the headers */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100826 if (IS_HTX_STRM(s)) {
827 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
828 goto out;
829 }
830 else {
831 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)ci_head(&s->res), msg->sov) < 0)
832 goto out;
833 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100834
835 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100836 if (cache_ctx) {
837 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100838
Willy Tarreauc9036c02019-01-11 19:38:25 +0100839 object->eb.key = key;
840
Christopher Faulet839791a2019-01-07 16:12:07 +0100841 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
842 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100843
Christopher Faulet839791a2019-01-07 16:12:07 +0100844 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100845
Christopher Faulet839791a2019-01-07 16:12:07 +0100846 old = entry_exist(cconf->c.cache, txn->cache_hash);
847 if (old) {
848 eb32_delete(&old->eb);
849 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100850 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100851 shctx_unlock(shctx);
852
853 /* store latest value and expiration time */
854 object->latest_validation = now.tv_sec;
855 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
856 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100857 }
858
859out:
860 /* if does not cache */
861 if (first) {
862 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100863 first->len = 0;
864 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100865 shctx_row_dec_hot(shctx, first);
866 shctx_unlock(shctx);
867 }
868
William Lallemand41db4602017-10-30 11:15:51 +0100869 return ACT_RET_CONT;
870}
871
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200872#define HTTP_CACHE_INIT 0 /* Initial state. */
873#define HTTP_CACHE_HEADER 1 /* Cache entry headers forwarded. */
874#define HTTP_CACHE_FWD 2 /* Cache entry completely forwarded. */
875#define HTTP_CACHE_END 3 /* Cache entry treatment terminated. */
William Lallemand77c11972017-10-31 20:43:01 +0100876
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100877#define HTX_CACHE_INIT 0 /* Initial state. */
878#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
879#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200880#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
881#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100882
William Lallemandecb73b12017-11-24 14:33:55 +0100883static void http_cache_applet_release(struct appctx *appctx)
884{
Christopher Faulet95220e22018-12-07 17:34:39 +0100885 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100886 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100887 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100888 struct shared_block *first = block_ptr(cache_ptr);
889
890 shctx_lock(shctx_ptr(cache));
891 shctx_row_dec_hot(shctx_ptr(cache), first);
892 shctx_unlock(shctx_ptr(cache));
893}
894
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200895
896static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
897 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100898{
Christopher Faulet95220e22018-12-07 17:34:39 +0100899 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
900 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200901 struct htx_blk *blk;
Christopher Faulet554b3bc2019-09-03 22:11:52 +0200902 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200903 unsigned int max, total;
904 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100905
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200906 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
907 if (!max)
908 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200909 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200910 ? (info & 0xff) + ((info >> 8) & 0xfffff)
911 : info & 0xfffffff);
912 if (blksz > max)
913 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100914
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200915 blk = htx_add_blk(htx, type, blksz);
916 if (!blk)
917 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100918
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200919 blk->info = info;
920 total = 4;
Christopher Faulet554b3bc2019-09-03 22:11:52 +0200921 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200922 while (blksz) {
923 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet554b3bc2019-09-03 22:11:52 +0200924 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200925 offset += max;
926 blksz -= max;
927 total += max;
Christopher Faulet554b3bc2019-09-03 22:11:52 +0200928 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200929 if (blksz || offset == shctx->block_size) {
930 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
931 offset = 0;
932 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100933 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200934 appctx->ctx.cache.offset = offset;
935 appctx->ctx.cache.next = shblk;
936 appctx->ctx.cache.sent += total;
937 return total;
938}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100939
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200940static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
941 uint32_t info, struct shared_block *shblk, unsigned int offset)
942{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100943
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200944 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
945 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
946 unsigned int max, total, rem_data;
947 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100948
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200949 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
950 if (!max)
951 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100952
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200953 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200954 if (appctx->ctx.cache.rem_data) {
955 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200956 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200957 }
958 else {
959 blksz = (info & 0xfffffff);
960 total = 4;
961 }
962 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200963 rem_data = blksz - max;
964 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100965 }
966
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200967 while (blksz) {
968 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100969
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200970 max = MIN(blksz, shctx->block_size - offset);
971 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
972 offset += sz;
973 blksz -= sz;
974 total += sz;
975 if (sz < max)
976 break;
977 if (blksz || offset == shctx->block_size) {
978 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
979 offset = 0;
980 }
981 }
982
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200983 appctx->ctx.cache.offset = offset;
984 appctx->ctx.cache.next = shblk;
985 appctx->ctx.cache.sent += total;
986 appctx->ctx.cache.rem_data = rem_data + blksz;
987 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100988}
989
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200990static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
991 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100992{
Christopher Faulet95220e22018-12-07 17:34:39 +0100993 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
994 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200995 struct shared_block *shblk;
996 unsigned int offset, sz;
997 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100998
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200999 while (len) {
1000 enum htx_blk_type type;
1001 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001002
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001003 shblk = appctx->ctx.cache.next;
1004 offset = appctx->ctx.cache.offset;
1005 if (appctx->ctx.cache.rem_data) {
1006 type = HTX_BLK_DATA;
1007 info = 0;
1008 goto add_data_blk;
1009 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001010
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001011 /* Get info of the next HTX block. May be splitted on 2 shblk */
1012 sz = MIN(4, shctx->block_size - offset);
1013 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
1014 offset += sz;
1015 if (sz < 4) {
1016 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1017 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
1018 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001019 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001020
1021 /* Get payload of the next HTX block and insert it. */
1022 type = (info >> 28);
1023 if (type != HTX_BLK_DATA)
1024 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
1025 else {
1026 add_data_blk:
1027 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001028 }
1029
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001030 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001031 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001032 total += ret;
1033 len -= ret;
1034
1035 if (appctx->ctx.cache.rem_data || type == mark)
1036 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001037 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001038
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001039 return total;
1040}
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001041
1042static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
1043{
1044 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1045 unsigned int age;
1046 char *end;
1047
1048 chunk_reset(&trash);
1049 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
1050 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1051 age = CACHE_ENTRY_MAX_AGE;
1052 end = ultoa_o(age, b_head(&trash), b_size(&trash));
1053 b_set_data(&trash, end - b_head(&trash));
1054 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
1055 return 0;
1056 return 1;
1057}
1058
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001059static void htx_cache_io_handler(struct appctx *appctx)
1060{
1061 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1062 struct shared_block *first = block_ptr(cache_ptr);
1063 struct stream_interface *si = appctx->owner;
1064 struct channel *req = si_oc(si);
1065 struct channel *res = si_ic(si);
1066 struct htx *req_htx, *res_htx;
1067 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001068 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001069 size_t ret, total = 0;
1070
1071 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001072 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001073
1074 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1075 goto out;
1076
1077 /* Check if the input buffer is avalaible. */
1078 if (!b_size(&res->buf)) {
1079 si_rx_room_blk(si);
1080 goto out;
1081 }
1082
Willy Tarreauefef3232018-12-16 00:37:45 +01001083 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +01001084 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001085
1086 if (appctx->st0 == HTX_CACHE_INIT) {
1087 appctx->ctx.cache.next = block_ptr(cache_ptr);
1088 appctx->ctx.cache.offset = sizeof(*cache_ptr);
1089 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001090 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001091 appctx->st0 = HTX_CACHE_HEADER;
1092 }
1093
1094 if (appctx->st0 == HTX_CACHE_HEADER) {
1095 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001096 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1097 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
1098 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
1099 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001100 goto error;
1101
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001102 /* Skip response body for HEAD requests */
1103 if (si_strm(si)->txn->meth == HTTP_METH_HEAD)
Christopher Fauletf0dd0372019-02-25 11:08:34 +01001104 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001105 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001106 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001107 }
1108
1109 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001110 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1111 if (len) {
1112 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
1113 if (ret < len) {
1114 si_rx_room_blk(si);
1115 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001116 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001117 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001118 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001119 }
1120
1121 if (appctx->st0 == HTX_CACHE_EOM) {
1122 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1123 si_rx_room_blk(si);
1124 goto out;
1125 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001126 appctx->st0 = HTX_CACHE_END;
1127 }
1128
1129 end:
Christopher Fauletadb36312019-02-25 11:40:49 +01001130 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001131 res->flags |= CF_READ_NULL;
1132 si_shutr(si);
1133 }
1134
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001135 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001136 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +01001137 if (total)
1138 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001139 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +01001140
1141 /* eat the whole request */
1142 if (co_data(req)) {
1143 req_htx = htx_from_buf(&req->buf);
1144 co_htx_skip(req, req_htx, co_data(req));
1145 htx_to_buf(req_htx, &req->buf);
1146 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001147 return;
1148
1149 error:
1150 /* Sent and HTTP error 500 */
1151 b_reset(&res->buf);
1152 errmsg = &htx_err_chunks[HTTP_ERR_500];
1153 res->buf.data = b_data(errmsg);
1154 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1155 res_htx = htx_from_buf(&res->buf);
1156
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001157 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001158 appctx->st0 = HTX_CACHE_END;
1159 goto end;
1160}
1161
1162
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001163/*
1164 * Append an "Age" header into <chn> channel for this <ce> cache entry.
Joseph Herlant8dae5b32018-11-15 14:07:53 -08001165 * This is the responsibility of the caller to insure there is enough
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001166 * data in the channel.
1167 *
1168 * Returns the number of bytes inserted if succeeded, 0 if failed.
1169 */
1170static int cache_channel_append_age_header(struct cache_entry *ce, struct channel *chn)
1171{
1172 unsigned int age;
1173
1174 age = MAX(0, (int)(now.tv_sec - ce->latest_validation)) + ce->age;
1175 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1176 age = CACHE_ENTRY_MAX_AGE;
1177
1178 chunk_reset(&trash);
1179 chunk_printf(&trash, "Age: %u", age);
1180
1181 return ci_insert_line2(chn, ce->eoh, trash.area, trash.data);
1182}
1183
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001184static int cache_channel_row_data_get(struct appctx *appctx, int len)
William Lallemand77c11972017-10-31 20:43:01 +01001185{
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001186 int ret, total;
William Lallemand77c11972017-10-31 20:43:01 +01001187 struct stream_interface *si = appctx->owner;
1188 struct channel *res = si_ic(si);
Christopher Faulet95220e22018-12-07 17:34:39 +01001189 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1190 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001191 struct shared_context *shctx = shctx_ptr(cache);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001192 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1193 struct shared_block *blk, *next = appctx->ctx.cache.next;
1194 int offset;
1195
1196 total = 0;
1197 offset = 0;
1198
1199 if (!next) {
1200 offset = sizeof(struct cache_entry);
1201 next = block_ptr(cache_ptr);
1202 }
1203
1204 blk = next;
1205 list_for_each_entry_from(blk, &shctx->hot, list) {
1206 int sz;
1207
1208 if (len <= 0)
1209 break;
1210
1211 sz = MIN(len, shctx->block_size - offset);
1212
1213 ret = ci_putblk(res, (const char *)blk->data + offset, sz);
1214 if (unlikely(offset))
1215 offset = 0;
1216 if (ret <= 0) {
1217 if (ret == -3 || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001218 si_rx_room_blk(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001219 break;
1220 }
1221 return -1;
1222 }
1223
1224 total += sz;
1225 len -= sz;
1226 }
1227 appctx->ctx.cache.next = blk;
1228
1229 return total;
1230}
1231
1232static void http_cache_io_handler(struct appctx *appctx)
1233{
1234 struct stream_interface *si = appctx->owner;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001235 struct stream *s = si_strm(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001236 struct channel *res = si_ic(si);
1237 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
William Lallemand77c11972017-10-31 20:43:01 +01001238 struct shared_block *first = block_ptr(cache_ptr);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001239 unsigned int *sent = &appctx->ctx.cache.sent;
1240
1241 if (IS_HTX_STRM(s))
1242 return htx_cache_io_handler(appctx);
William Lallemand77c11972017-10-31 20:43:01 +01001243
1244 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1245 goto out;
1246
Joseph Herlant8dae5b32018-11-15 14:07:53 -08001247 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001248 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01001249 /* buf.size==0 means we failed to get a buffer and were
1250 * already subscribed to a wait list to get a buffer.
1251 */
William Lallemand77c11972017-10-31 20:43:01 +01001252 goto out;
1253 }
1254
Willy Tarreauefef3232018-12-16 00:37:45 +01001255 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
William Lallemand77c11972017-10-31 20:43:01 +01001256 appctx->st0 = HTTP_CACHE_END;
1257
1258 /* buffer are aligned there, should be fine */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001259 if (appctx->st0 == HTTP_CACHE_HEADER || appctx->st0 == HTTP_CACHE_INIT) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001260 int len = first->len - *sent - sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001261 if (len > 0) {
1262 int ret;
1263
1264 ret = cache_channel_row_data_get(appctx, len);
1265 if (ret == -1)
1266 appctx->st0 = HTTP_CACHE_END;
1267 else
1268 *sent += ret;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001269 if (appctx->st0 == HTTP_CACHE_INIT && *sent > cache_ptr->eoh &&
1270 cache_channel_append_age_header(cache_ptr, res))
1271 appctx->st0 = HTTP_CACHE_HEADER;
Christopher Faulet61123912019-01-02 14:10:01 +01001272 else if (ret == len) {
1273 *sent = 0;
1274 appctx->st0 = HTTP_CACHE_FWD;
1275 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001276 }
1277 else {
1278 *sent = 0;
1279 appctx->st0 = HTTP_CACHE_FWD;
William Lallemand77c11972017-10-31 20:43:01 +01001280 }
William Lallemand77c11972017-10-31 20:43:01 +01001281 }
1282
Christopher Fauletadb36312019-02-25 11:40:49 +01001283 if (appctx->st0 == HTTP_CACHE_FWD)
1284 appctx->st0 = HTTP_CACHE_END;
1285
1286 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTTP_CACHE_END) {
William Lallemand77c11972017-10-31 20:43:01 +01001287 res->flags |= CF_READ_NULL;
1288 si_shutr(si);
1289 }
William Lallemand77c11972017-10-31 20:43:01 +01001290out:
Christopher Fauletadb36312019-02-25 11:40:49 +01001291 /* eat the whole request */
1292 if (co_data(si_oc(si)))
1293 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand77c11972017-10-31 20:43:01 +01001294}
1295
Christopher Faulet95220e22018-12-07 17:34:39 +01001296static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001297{
1298 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001299 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001300
Christopher Faulet95220e22018-12-07 17:34:39 +01001301 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001302 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001303 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001304 }
1305
1306 /* check if a cache filter was already registered with this cache
1307 * name, if that's the case, must use it. */
1308 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001309 if (fconf->id == cache_store_flt_id) {
1310 cconf = fconf->conf;
1311 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1312 rule->arg.act.p[0] = cconf;
1313 return 1;
1314 }
William Lallemand41db4602017-10-30 11:15:51 +01001315 }
1316 }
1317
Christopher Faulet95220e22018-12-07 17:34:39 +01001318 /* Create the filter cache config */
1319 cconf = calloc(1, sizeof(*cconf));
1320 if (!cconf) {
1321 memprintf(err, "out of memory\n");
1322 goto err;
1323 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001324 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001325 cconf->c.name = strdup(name);
1326 if (!cconf->c.name) {
1327 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001328 goto err;
1329 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001330
William Lallemand41db4602017-10-30 11:15:51 +01001331 /* register a filter to fill the cache buffer */
1332 fconf = calloc(1, sizeof(*fconf));
1333 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001334 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001335 goto err;
1336 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001337 fconf->id = cache_store_flt_id;
1338 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001339 fconf->ops = &cache_ops;
1340 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1341
Christopher Faulet95220e22018-12-07 17:34:39 +01001342 rule->arg.act.p[0] = cconf;
1343 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001344
Christopher Faulet95220e22018-12-07 17:34:39 +01001345 err:
1346 free(cconf);
1347 return 0;
1348}
1349
1350enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1351 struct act_rule *rule, char **err)
1352{
1353 rule->action = ACT_CUSTOM;
1354 rule->action_ptr = http_action_store_cache;
1355
1356 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1357 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001358
Christopher Faulet95220e22018-12-07 17:34:39 +01001359 (*orig_arg)++;
1360 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001361}
1362
William Lallemandf528fff2017-11-23 19:43:17 +01001363/* This produces a sha1 hash of the concatenation of the first
1364 * occurrence of the Host header followed by the path component if it
1365 * begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001366int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001367{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001368 struct http_txn *txn = s->txn;
William Lallemandf528fff2017-11-23 19:43:17 +01001369 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001370 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001371
William Lallemandf528fff2017-11-23 19:43:17 +01001372 trash = get_trash_chunk();
1373
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001374 if (IS_HTX_STRM(s)) {
1375 struct htx *htx = htxbuf(&s->req.buf);
1376 struct htx_sl *sl;
1377 struct http_hdr_ctx ctx;
1378 struct ist path;
William Lallemandf528fff2017-11-23 19:43:17 +01001379
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001380 ctx.blk = NULL;
1381 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1382 return 0;
1383 chunk_memcat(trash, ctx.value.ptr, ctx.value.len);
1384
Christopher Faulet297fbb42019-05-13 14:41:27 +02001385 sl = http_get_stline(htx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001386 path = http_get_path(htx_sl_req_uri(sl));
1387 if (!path.ptr)
1388 return 0;
1389 chunk_memcat(trash, path.ptr, path.len);
1390 }
1391 else {
1392 struct hdr_ctx ctx;
1393 char *path;
1394 char *end;
1395
1396 /* retrive the host */
1397 ctx.idx = 0;
1398 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx))
1399 return 0;
1400 chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen);
1401
1402 /* now retrieve the path */
1403 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1404 path = http_txn_get_path(txn);
1405 if (!path)
1406 return 0;
1407 chunk_strncat(trash, path, end - path);
1408 }
William Lallemandf528fff2017-11-23 19:43:17 +01001409
1410 /* hash everything */
1411 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001412 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001413 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1414
1415 return 1;
1416}
1417
William Lallemand41db4602017-10-30 11:15:51 +01001418enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1419 struct session *sess, struct stream *s, int flags)
1420{
William Lallemand77c11972017-10-31 20:43:01 +01001421
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001422 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001423 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001424 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1425 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001426
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001427 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1428 * and HEAD */
1429 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
1430 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
1431 txn->flags |= TX_CACHE_IGNORE;
1432
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001433 if (IS_HTX_STRM(s))
1434 htx_check_request_for_cacheability(s, &s->req);
1435 else
1436 check_request_for_cacheability(s, &s->req);
1437
Willy Tarreau504455c2017-12-22 17:47:35 +01001438 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1439 return ACT_RET_CONT;
1440
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001441 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001442 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001443
Willy Tarreau504455c2017-12-22 17:47:35 +01001444 if (s->txn->flags & TX_CACHE_IGNORE)
1445 return ACT_RET_CONT;
1446
Willy Tarreaua1214a52018-12-14 14:00:25 +01001447 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001448 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001449 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001450 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001451
William Lallemanda400a3a2017-11-20 19:13:12 +01001452 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001453 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001454 if (res) {
1455 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001456 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1457 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001458 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001459 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
William Lallemand77c11972017-10-31 20:43:01 +01001460 appctx->st0 = HTTP_CACHE_INIT;
1461 appctx->rule = rule;
1462 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001463 appctx->ctx.cache.next = NULL;
1464 appctx->ctx.cache.sent = 0;
Willy Tarreaua1214a52018-12-14 14:00:25 +01001465
1466 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001467 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001468 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001469 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001470 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001471 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001472 shctx_lock(shctx_ptr(cache));
1473 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1474 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001475 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001476 }
1477 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001478 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001479 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001480}
1481
1482
1483enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1484 struct act_rule *rule, char **err)
1485{
William Lallemand41db4602017-10-30 11:15:51 +01001486 rule->action = ACT_CUSTOM;
1487 rule->action_ptr = http_action_req_cache_use;
1488
Christopher Faulet95220e22018-12-07 17:34:39 +01001489 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001490 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001491
1492 (*orig_arg)++;
1493 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001494}
1495
1496int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1497{
1498 int err_code = 0;
1499
1500 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1501
1502 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001503 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1504 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001505 err_code |= ERR_ALERT | ERR_ABORT;
1506 goto out;
1507 }
1508
1509 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1510 err_code |= ERR_ABORT;
1511 goto out;
1512 }
1513
1514 if (tmp_cache_config == NULL) {
1515 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1516 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001517 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001518 err_code |= ERR_ALERT | ERR_ABORT;
1519 goto out;
1520 }
1521
1522 strlcpy2(tmp_cache_config->id, args[1], 33);
1523 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001524 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1525 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001526 err_code |= ERR_WARN;
1527 }
William Lallemand49b44532017-11-24 18:53:43 +01001528 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001529 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001530 tmp_cache_config->maxobjsz = 0;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001531 tmp_cache_config->flags = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001532 }
1533 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001534 unsigned long int maxsize;
1535 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001536
1537 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1538 err_code |= ERR_ABORT;
1539 goto out;
1540 }
1541
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001542 maxsize = strtoul(args[1], &err, 10);
1543 if (err == args[1] || *err != '\0') {
1544 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1545 file, linenum, args[1]);
1546 err_code |= ERR_ABORT;
1547 goto out;
1548 }
1549
1550 if (maxsize > (UINT_MAX >> 20)) {
1551 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1552 file, linenum, args[1], UINT_MAX >> 20);
1553 err_code |= ERR_ABORT;
1554 goto out;
1555 }
1556
William Lallemand41db4602017-10-30 11:15:51 +01001557 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001558 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001559 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001560 } else if (strcmp(args[0], "max-age") == 0) {
1561 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1562 err_code |= ERR_ABORT;
1563 goto out;
1564 }
1565
1566 if (!*args[1]) {
1567 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1568 file, linenum, args[0]);
1569 err_code |= ERR_WARN;
1570 }
1571
1572 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001573 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001574 unsigned int maxobjsz;
1575 char *err;
1576
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001577 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1578 err_code |= ERR_ABORT;
1579 goto out;
1580 }
1581
1582 if (!*args[1]) {
1583 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1584 file, linenum, args[0]);
1585 err_code |= ERR_WARN;
1586 }
1587
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001588 maxobjsz = strtoul(args[1], &err, 10);
1589 if (err == args[1] || *err != '\0') {
1590 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1591 file, linenum, args[1]);
1592 err_code |= ERR_ABORT;
1593 goto out;
1594 }
1595 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001596 }
1597 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001598 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001599 err_code |= ERR_ALERT | ERR_FATAL;
1600 goto out;
1601 }
1602out:
1603 return err_code;
1604}
1605
1606/* once the cache section is parsed */
1607
1608int cfg_post_parse_section_cache()
1609{
1610 struct shared_context *shctx;
1611 int err_code = 0;
1612 int ret_shctx;
1613
1614 if (tmp_cache_config) {
1615 struct cache *cache;
1616
1617 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001618 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001619 err_code |= ERR_FATAL | ERR_ALERT;
1620 goto out;
1621 }
1622
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001623 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001624 /* Default max. file size is a 256th of the cache size. */
1625 tmp_cache_config->maxobjsz =
1626 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001627 }
1628 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1629 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1630 err_code |= ERR_FATAL | ERR_ALERT;
1631 goto out;
1632 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001633
1634 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE,
1635 tmp_cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001636
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001637 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001638 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001639 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001640 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001641 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001642
1643 err_code |= ERR_FATAL | ERR_ALERT;
1644 goto out;
1645 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001646 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +01001647 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
1648 cache = (struct cache *)shctx->data;
1649 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001650 LIST_ADDQ(&caches, &cache->list);
1651 }
1652out:
1653 free(tmp_cache_config);
1654 tmp_cache_config = NULL;
1655 return err_code;
1656
1657}
1658
1659/*
1660 * Resolve the cache name to a pointer once the file is completely read.
1661 */
1662int cfg_cache_postparser()
1663{
William Lallemand41db4602017-10-30 11:15:51 +01001664 struct cache *cache;
William Lallemand41db4602017-10-30 11:15:51 +01001665 int err = 0;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001666
1667 /* Check if the cache is used by HTX and legacy HTTP proxies in same
1668 * time
1669 */
1670 list_for_each_entry(cache, &caches, list) {
1671 if ((cache->flags & (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) == (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) {
1672 ha_alert("Cache '%s': cannot be used by HTX and legacy HTTP proxies in same time.\n",
1673 cache->id);
1674 err++;
1675 }
1676 }
1677
William Lallemand41db4602017-10-30 11:15:51 +01001678 return err;
1679}
1680
1681
1682struct flt_ops cache_ops = {
1683 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001684 .check = cache_store_check,
1685 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001686
William Lallemand4da3f8a2017-10-31 14:33:34 +01001687 /* Handle channels activity */
1688 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001689 .channel_end_analyze = cache_store_chn_end_analyze,
Christopher Faulet839791a2019-01-07 16:12:07 +01001690 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001691
1692 /* Filter HTTP requests and responses */
1693 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001694 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001695 .http_end = cache_store_http_end,
1696
1697 .http_forward_data = cache_store_http_forward_data,
1698
William Lallemand41db4602017-10-30 11:15:51 +01001699};
1700
Christopher Faulet99a17a22018-12-11 09:18:27 +01001701
1702
1703static int
1704parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1705 struct flt_conf *fconf, char **err, void *private)
1706{
1707 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001708 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001709 char *name = NULL;
1710 int pos = *cur_arg;
1711
1712 /* Get the cache filter name*/
1713 if (!strcmp(args[pos], "cache")) {
1714 if (!*args[pos + 1]) {
1715 memprintf(err, "%s : expects an <id> argument", args[pos]);
1716 goto error;
1717 }
1718 name = strdup(args[pos + 1]);
1719 if (!name) {
1720 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1721 goto error;
1722 }
1723 pos += 2;
1724 }
1725
1726 /* Check if an implicit filter with the same name already exists. If so,
1727 * we remove the implicit filter to use the explicit one. */
1728 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1729 if (f->id != cache_store_flt_id)
1730 continue;
1731
1732 cconf = f->conf;
1733 if (strcmp(name, cconf->c.name)) {
1734 cconf = NULL;
1735 continue;
1736 }
1737
1738 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1739 cconf = NULL;
1740 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1741 px->id, name);
1742 return -1;
1743 }
1744
1745 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1746 LIST_DEL(&f->list);
1747 free(f);
1748 free(name);
1749 break;
1750 }
1751
1752 /* No implicit cache filter found, create configuration for the explicit one */
1753 if (!cconf) {
1754 cconf = calloc(1, sizeof(*cconf));
1755 if (!cconf) {
1756 memprintf(err, "%s: out of memory", args[*cur_arg]);
1757 goto error;
1758 }
1759 cconf->c.name = name;
1760 }
1761
1762 cconf->flags = 0;
1763 fconf->id = cache_store_flt_id;
1764 fconf->conf = cconf;
1765 fconf->ops = &cache_ops;
1766
1767 *cur_arg = pos;
1768 return 0;
1769
1770 error:
1771 free(name);
1772 free(cconf);
1773 return -1;
1774}
1775
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001776static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001777{
1778 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1779 return 1;
1780
1781 return 0;
1782}
1783
1784static int cli_io_handler_show_cache(struct appctx *appctx)
1785{
1786 struct cache* cache = appctx->ctx.cli.p0;
1787 struct stream_interface *si = appctx->owner;
1788
William Lallemand1f49a362017-11-21 20:01:26 +01001789 if (cache == NULL) {
1790 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1791 }
1792
1793 list_for_each_entry_from(cache, &caches, list) {
1794 struct eb32_node *node = NULL;
1795 unsigned int next_key;
1796 struct cache_entry *entry;
1797
William Lallemand1f49a362017-11-21 20:01:26 +01001798 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001799 if (!next_key) {
1800 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1801 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001802 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001803 return 0;
1804 }
1805 }
William Lallemand1f49a362017-11-21 20:01:26 +01001806
1807 appctx->ctx.cli.p0 = cache;
1808
1809 while (1) {
1810
1811 shctx_lock(shctx_ptr(cache));
1812 node = eb32_lookup_ge(&cache->entries, next_key);
1813 if (!node) {
1814 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001815 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001816 break;
1817 }
1818
1819 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001820 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 Lallemand1f49a362017-11-21 20:01:26 +01001821
1822 next_key = node->key + 1;
1823 appctx->ctx.cli.i0 = next_key;
1824
1825 shctx_unlock(shctx_ptr(cache));
1826
1827 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001828 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001829 return 0;
1830 }
1831 }
1832
1833 }
1834
1835 return 1;
1836
1837}
1838
Christopher Faulet99a17a22018-12-11 09:18:27 +01001839/* Declare the filter parser for "cache" keyword */
1840static struct flt_kw_list filter_kws = { "CACHE", { }, {
1841 { "cache", parse_cache_flt, NULL },
1842 { NULL, NULL, NULL },
1843 }
1844};
1845
1846INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1847
William Lallemand1f49a362017-11-21 20:01:26 +01001848static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001849 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1850 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001851}};
1852
Willy Tarreau0108d902018-11-25 19:14:37 +01001853INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001854
William Lallemand41db4602017-10-30 11:15:51 +01001855static struct action_kw_list http_res_actions = {
1856 .kw = {
1857 { "cache-store", parse_cache_store },
1858 { NULL, NULL }
1859 }
1860};
1861
Willy Tarreau0108d902018-11-25 19:14:37 +01001862INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1863
William Lallemand41db4602017-10-30 11:15:51 +01001864static struct action_kw_list http_req_actions = {
1865 .kw = {
1866 { "cache-use", parse_cache_use },
1867 { NULL, NULL }
1868 }
1869};
1870
Willy Tarreau0108d902018-11-25 19:14:37 +01001871INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1872
Willy Tarreau2231b632019-03-29 18:26:52 +01001873struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001874 .obj_type = OBJ_TYPE_APPLET,
1875 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001876 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001877 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001878};
1879
Willy Tarreaue6552512018-11-26 11:33:13 +01001880/* config parsers for this section */
1881REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
1882REGISTER_CONFIG_POSTPARSER("cache", cfg_cache_postparser);