blob: 162ec18f3d64580a635cdad36a104804a2296783 [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
Christopher Faulet27d93c32018-12-15 22:32:02 +010041#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010042 * the filter keyword) */
Christopher Fauletafd819c2018-12-11 08:57:45 +010043
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010044const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010045
Willy Tarreau2231b632019-03-29 18:26:52 +010046extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010047
48struct flt_ops cache_ops;
49
50struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010051 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010052 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010053 unsigned int maxage; /* max-age */
54 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020055 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010056 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010057};
58
Christopher Faulet95220e22018-12-07 17:34:39 +010059/* cache config for filters */
60struct cache_flt_conf {
61 union {
62 struct cache *cache; /* cache used by the filter */
63 char *name; /* cache name used during conf parsing */
64 } c;
65 unsigned int flags; /* CACHE_FLT_F_* */
66};
67
William Lallemand41db4602017-10-30 11:15:51 +010068/*
69 * cache ctx for filters
70 */
71struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +010072 struct shared_block *first_block;
73};
74
75struct cache_entry {
76 unsigned int latest_validation; /* latest validation date */
77 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020078 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010079
William Lallemand41db4602017-10-30 11:15:51 +010080 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010081 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010082 unsigned char data[0];
83};
84
85#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010086#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010087
88static struct list caches = LIST_HEAD_INIT(caches);
89static struct cache *tmp_cache_config = NULL;
90
Willy Tarreau8ceae722018-11-26 11:58:30 +010091DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
92
William Lallemandf528fff2017-11-23 19:43:17 +010093struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010094{
95 struct eb32_node *node;
96 struct cache_entry *entry;
97
William Lallemandf528fff2017-11-23 19:43:17 +010098 node = eb32_lookup(&cache->entries, (*(unsigned int *)hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010099 if (!node)
100 return NULL;
101
102 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100103
104 /* if that's not the right node */
105 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
106 return NULL;
107
William Lallemand08727662017-11-21 20:01:27 +0100108 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100109 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100110 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100111 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100112 entry->eb.key = 0;
113 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100114 return NULL;
115
116}
117
118static inline struct shared_context *shctx_ptr(struct cache *cache)
119{
120 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
121}
122
William Lallemand77c11972017-10-31 20:43:01 +0100123static inline struct shared_block *block_ptr(struct cache_entry *entry)
124{
125 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
126}
127
128
129
William Lallemand41db4602017-10-30 11:15:51 +0100130static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100131cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100132{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100133 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100134 return 0;
135}
136
Christopher Faulet95220e22018-12-07 17:34:39 +0100137static void
138cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
139{
140 struct cache_flt_conf *cconf = fconf->conf;
141
142 free(cconf);
143}
144
William Lallemand4da3f8a2017-10-31 14:33:34 +0100145static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100146cache_store_check(struct proxy *px, struct flt_conf *fconf)
147{
148 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100149 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100150 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100151 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100152
153 /* resolve the cache name to a ptr in the filter config */
154 list_for_each_entry(cache, &caches, list) {
155 if (!strcmp(cache->id, cconf->c.name)) {
Christopher Faulet95220e22018-12-07 17:34:39 +0100156 free(cconf->c.name);
157 cconf->c.cache = cache;
158 goto found;
159 }
160 }
161
162 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
163 proxy_type_str(px), px->id, (char *)cconf->c.name);
164 return 1;
165
166 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100167 /* Here <cache> points on the cache the filter must use and <cconf>
168 * points on the cache filter configuration. */
169
170 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100171 * enabled and if it is after the cache. When the compression is before
172 * the cache, an error is returned. Also check if the cache filter must
173 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100174 list_for_each_entry(f, &px->filter_configs, list) {
175 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100176 /* The compression filter must be evaluated after the cache. */
177 if (comp) {
178 ha_alert("config: %s '%s': unable to enable the compression filter before "
179 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
180 return 1;
181 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100182 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200183 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100184 comp = 1;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100185 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
186 /* Implicit declaration is only allowed with the
187 * compression. For other filters, an implicit
188 * declaration is required. */
189 ha_alert("config: %s '%s': require an explicit filter declaration "
190 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
191 return 1;
192 }
193
Christopher Fauletafd819c2018-12-11 08:57:45 +0100194 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100195 return 0;
196}
197
198static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100199cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
200{
201 if (!(chn->flags & CF_ISRESP))
202 return 1;
203
204 if (filter->ctx == NULL) {
205 struct cache_st *st;
206
Willy Tarreaubafbe012017-11-24 17:34:44 +0100207 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100208 if (st == NULL)
209 return -1;
210
William Lallemand4da3f8a2017-10-31 14:33:34 +0100211 st->first_block = NULL;
212 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100213
214 /* Register post-analyzer on AN_RES_WAIT_HTTP */
215 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100216 }
217
William Lallemand4da3f8a2017-10-31 14:33:34 +0100218 return 1;
219}
220
221static int
William Lallemand49dc0482017-11-24 14:33:54 +0100222cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
223{
224 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100225 struct cache_flt_conf *cconf = FLT_CONF(filter);
226 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100227 struct shared_context *shctx = shctx_ptr(cache);
228
229 if (!(chn->flags & CF_ISRESP))
230 return 1;
231
232 /* Everything should be released in the http_end filter, but we need to do it
233 * there too, in case of errors */
234
235 if (st && st->first_block) {
236
237 shctx_lock(shctx);
238 shctx_row_dec_hot(shctx, st->first_block);
239 shctx_unlock(shctx);
240
241 }
242 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100243 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100244 filter->ctx = NULL;
245 }
246
247 return 1;
248}
249
Christopher Faulet839791a2019-01-07 16:12:07 +0100250static int
251cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
252 unsigned an_bit)
253{
254 struct http_txn *txn = s->txn;
255 struct http_msg *msg = &txn->rsp;
256 struct cache_st *st = filter->ctx;
257
258 if (an_bit != AN_RES_WAIT_HTTP)
259 goto end;
260
261 /* Here we need to check if any compression filter precedes the cache
262 * filter. This is only possible when the compression is configured in
263 * the frontend while the cache filter is configured on the
264 * backend. This case cannot be detected during HAProxy startup. So in
265 * such cases, the cache is disabled.
266 */
267 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
268 pool_free(pool_head_cache_st, st);
269 filter->ctx = NULL;
270 }
271
272 end:
273 return 1;
274}
William Lallemand49dc0482017-11-24 14:33:54 +0100275
276static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100277cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
278{
279 struct cache_st *st = filter->ctx;
280
William Lallemand4da3f8a2017-10-31 14:33:34 +0100281 if (!(msg->chn->flags & CF_ISRESP) || !st)
282 return 1;
283
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200284 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100285 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100286 return 1;
287}
288
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200289static inline void disable_cache_entry(struct cache_st *st,
290 struct filter *filter, struct shared_context *shctx)
291{
292 struct cache_entry *object;
293
294 object = (struct cache_entry *)st->first_block->data;
295 filter->ctx = NULL; /* disable cache */
296 shctx_lock(shctx);
297 shctx_row_dec_hot(shctx, st->first_block);
298 object->eb.key = 0;
299 shctx_unlock(shctx);
300 pool_free(pool_head_cache_st, st);
301}
302
William Lallemand4da3f8a2017-10-31 14:33:34 +0100303static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100304cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
305 unsigned int offset, unsigned int len)
306{
Christopher Faulet95220e22018-12-07 17:34:39 +0100307 struct cache_flt_conf *cconf = FLT_CONF(filter);
308 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100309 struct cache_st *st = filter->ctx;
310 struct htx *htx = htxbuf(&msg->chn->buf);
311 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200312 struct shared_block *fb;
313 unsigned int orig_len, to_forward;
314 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100315
316 if (!len)
317 return len;
318
319 if (!st->first_block) {
320 unregister_data_filter(s, msg->chn, filter);
321 return len;
322 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100323
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200324 chunk_reset(&trash);
325 orig_len = len;
326 to_forward = 0;
Christopher Fauletee847d42019-05-23 11:55:33 +0200327 for (blk = htx_get_first_blk(htx); blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100328 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200329 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100330 struct ist v;
331
Christopher Fauletee847d42019-05-23 11:55:33 +0200332 if (offset >= sz) {
333 offset -= sz;
334 continue;
335 }
336
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100337 switch (type) {
338 case HTX_BLK_UNUSED:
339 break;
340
341 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100342 v = htx_get_blk_value(htx, blk);
343 v.ptr += offset;
344 v.len -= offset;
345 if (v.len > len)
346 v.len = len;
347
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200348 info = (type << 28) + v.len;
349 chunk_memcat(&trash, (char *)&info, sizeof(info));
350 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100351 to_forward += v.len;
352 len -= v.len;
353 break;
354
355 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200356 /* Here offset must always be 0 because only
357 * DATA blocks can be partially transferred. */
358 if (offset)
359 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100360 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200361 goto end;
362
363 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
364 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100365 to_forward += sz;
366 len -= sz;
367 break;
368 }
369
370 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100371 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200372
373 end:
374 shctx_lock(shctx);
375 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
376 if (!fb) {
377 shctx_unlock(shctx);
378 goto no_cache;
379 }
380 shctx_unlock(shctx);
381
382 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
383 (unsigned char *)b_head(&trash), b_data(&trash));
384 if (ret < 0)
385 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100386
387 return to_forward;
388
389 no_cache:
390 disable_cache_entry(st, filter, shctx);
391 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200392 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100393}
394
395static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100396cache_store_http_end(struct stream *s, struct filter *filter,
397 struct http_msg *msg)
398{
399 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100400 struct cache_flt_conf *cconf = FLT_CONF(filter);
401 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100402 struct shared_context *shctx = shctx_ptr(cache);
403 struct cache_entry *object;
404
405 if (!(msg->chn->flags & CF_ISRESP))
406 return 1;
407
408 if (st && st->first_block) {
409
410 object = (struct cache_entry *)st->first_block->data;
411
412 /* does not need to test if the insertion worked, if it
413 * doesn't, the blocks will be reused anyway */
414
415 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100416 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
417 object->eb.key = 0;
418 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100419 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100420 shctx_row_dec_hot(shctx, st->first_block);
421 shctx_unlock(shctx);
422
423 }
424 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100425 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100426 filter->ctx = NULL;
427 }
428
429 return 1;
430}
431
432 /*
433 * This intends to be used when checking HTTP headers for some
434 * word=value directive. Return a pointer to the first character of value, if
435 * the word was not found or if there wasn't any value assigned ot it return NULL
436 */
437char *directive_value(const char *sample, int slen, const char *word, int wlen)
438{
439 int st = 0;
440
441 if (slen < wlen)
442 return 0;
443
444 while (wlen) {
445 char c = *sample ^ *word;
446 if (c && c != ('A' ^ 'a'))
447 return NULL;
448 sample++;
449 word++;
450 slen--;
451 wlen--;
452 }
453
454 while (slen) {
455 if (st == 0) {
456 if (*sample != '=')
457 return NULL;
458 sample++;
459 slen--;
460 st = 1;
461 continue;
462 } else {
463 return (char *)sample;
464 }
465 }
466
467 return NULL;
468}
469
470/*
471 * Return the maxage in seconds of an HTTP response.
472 * Compute the maxage using either:
473 * - the assigned max-age of the cache
474 * - the s-maxage directive
475 * - the max-age directive
476 * - (Expires - Data) headers
477 * - the default-max-age of the cache
478 *
479 */
William Lallemand49b44532017-11-24 18:53:43 +0100480int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100481{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200482 struct htx *htx = htxbuf(&s->res.buf);
483 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100484 int smaxage = -1;
485 int maxage = -1;
486
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200487 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
488 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100489
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200490 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
491 if (value) {
492 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100493
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200494 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
495 chunk_strncat(chk, "", 1);
496 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100497 }
498
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200499 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
500 if (value) {
501 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200502
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200503 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
504 chunk_strncat(chk, "", 1);
505 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100506 }
507 }
508
509 /* TODO: Expires - Data */
510
511
512 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100513 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100514
515 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100516 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100517
William Lallemand49b44532017-11-24 18:53:43 +0100518 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100519
520}
521
522
William Lallemanda400a3a2017-11-20 19:13:12 +0100523static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
524{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200525 struct cache_entry *object = (struct cache_entry *)block->data;
526
527 if (first == block && object->eb.key)
528 eb32_delete(&object->eb);
529 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100530}
531
William Lallemand41db4602017-10-30 11:15:51 +0100532/*
533 * This fonction will store the headers of the response in a buffer and then
534 * register a filter to store the data
535 */
536enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200537 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100538{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200539 unsigned int age;
540 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100541 struct http_txn *txn = s->txn;
542 struct http_msg *msg = &txn->rsp;
543 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100544 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100545 struct cache_flt_conf *cconf = rule->arg.act.p[0];
546 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100547 struct cache_st *cache_ctx = NULL;
548 struct cache_entry *object, *old;
Willy Tarreauc9036c02019-01-11 19:38:25 +0100549 unsigned int key = *(unsigned int *)txn->cache_hash;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200550 struct htx *htx;
551 struct http_hdr_ctx ctx;
552 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100553
William Lallemand4da3f8a2017-10-31 14:33:34 +0100554 /* Don't cache if the response came from a cache */
555 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
556 s->target == &http_cache_applet.obj_type) {
557 goto out;
558 }
559
560 /* cache only HTTP/1.1 */
561 if (!(txn->req.flags & HTTP_MSGF_VER_11))
562 goto out;
563
564 /* cache only GET method */
565 if (txn->meth != HTTP_METH_GET)
566 goto out;
567
Willy Tarreauc9036c02019-01-11 19:38:25 +0100568 /* cache key was not computed */
569 if (!key)
570 goto out;
571
William Lallemand4da3f8a2017-10-31 14:33:34 +0100572 /* cache only 200 status code */
573 if (txn->status != 200)
574 goto out;
575
Christopher Faulet839791a2019-01-07 16:12:07 +0100576 /* Find the corresponding filter instance for the current stream */
577 list_for_each_entry(filter, &s->strm_flt.filters, list) {
578 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
579 /* No filter ctx, don't cache anything */
580 if (!filter->ctx)
581 goto out;
582 cache_ctx = filter->ctx;
583 break;
584 }
585 }
586
587 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200588 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100589
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200590 /* Do not cache too big objects. */
591 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
592 htx->data + htx->extra > shctx->max_obj_size)
593 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100594
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200595 /* Does not manage Vary at the moment. We will need a secondary key later for that */
596 ctx.blk = NULL;
597 if (http_find_header(htx, ist("Vary"), &ctx, 0))
598 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100599
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200600 htx_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100601
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200602 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
603 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100604
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200605 age = 0;
606 ctx.blk = NULL;
607 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
608 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
609 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
610 hdr_age = CACHE_ENTRY_MAX_AGE;
611 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100612 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200613 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100614 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100615
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200616 chunk_reset(&trash);
617 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
618 struct htx_blk *blk = htx_get_blk(htx, pos);
619 enum htx_blk_type type = htx_get_blk_type(blk);
620 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100621
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200622 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
623 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
624 if (type == HTX_BLK_EOH)
625 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200626 }
627
William Lallemand4da3f8a2017-10-31 14:33:34 +0100628 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200629 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100630 if (!first) {
631 shctx_unlock(shctx);
632 goto out;
633 }
634 shctx_unlock(shctx);
635
Willy Tarreau1093a452018-04-06 19:02:25 +0200636 /* the received memory is not initialized, we need at least to mark
637 * the object as not indexed yet.
638 */
639 object = (struct cache_entry *)first->data;
640 object->eb.node.leaf_p = NULL;
641 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200642 object->age = age;
Willy Tarreau1093a452018-04-06 19:02:25 +0200643
William Lallemand4da3f8a2017-10-31 14:33:34 +0100644 /* reserve space for the cache_entry structure */
645 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200646 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100647 /* cache the headers in a http action because it allows to chose what
648 * to cache, for example you might want to cache a response before
649 * modifying some HTTP headers, or on the contrary after modifying
650 * those headers.
651 */
652
653 /* does not need to be locked because it's in the "hot" list,
654 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200655 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
656 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100657
658 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100659 if (cache_ctx) {
660 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100661
Willy Tarreauc9036c02019-01-11 19:38:25 +0100662 object->eb.key = key;
663
Christopher Faulet839791a2019-01-07 16:12:07 +0100664 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
665 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100666
Christopher Faulet839791a2019-01-07 16:12:07 +0100667 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100668
Christopher Faulet839791a2019-01-07 16:12:07 +0100669 old = entry_exist(cconf->c.cache, txn->cache_hash);
670 if (old) {
671 eb32_delete(&old->eb);
672 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100673 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100674 shctx_unlock(shctx);
675
676 /* store latest value and expiration time */
677 object->latest_validation = now.tv_sec;
678 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
679 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100680 }
681
682out:
683 /* if does not cache */
684 if (first) {
685 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100686 first->len = 0;
687 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100688 shctx_row_dec_hot(shctx, first);
689 shctx_unlock(shctx);
690 }
691
William Lallemand41db4602017-10-30 11:15:51 +0100692 return ACT_RET_CONT;
693}
694
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100695#define HTX_CACHE_INIT 0 /* Initial state. */
696#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
697#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200698#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
699#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100700
William Lallemandecb73b12017-11-24 14:33:55 +0100701static void http_cache_applet_release(struct appctx *appctx)
702{
Christopher Faulet95220e22018-12-07 17:34:39 +0100703 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100704 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100705 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100706 struct shared_block *first = block_ptr(cache_ptr);
707
708 shctx_lock(shctx_ptr(cache));
709 shctx_row_dec_hot(shctx_ptr(cache), first);
710 shctx_unlock(shctx_ptr(cache));
711}
712
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200713
714static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
715 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100716{
Christopher Faulet95220e22018-12-07 17:34:39 +0100717 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
718 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200719 struct htx_blk *blk;
720 unsigned int max, total;
721 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100722
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200723 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
724 if (!max)
725 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200726 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200727 ? (info & 0xff) + ((info >> 8) & 0xfffff)
728 : info & 0xfffffff);
729 if (blksz > max)
730 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100731
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200732 blk = htx_add_blk(htx, type, blksz);
733 if (!blk)
734 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100735
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200736 blk->info = info;
737 total = 4;
738 while (blksz) {
739 max = MIN(blksz, shctx->block_size - offset);
740 memcpy(htx_get_blk_ptr(htx, blk), (const char *)shblk->data + offset, max);
741 offset += max;
742 blksz -= max;
743 total += max;
744 if (blksz || offset == shctx->block_size) {
745 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
746 offset = 0;
747 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100748 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200749 appctx->ctx.cache.offset = offset;
750 appctx->ctx.cache.next = shblk;
751 appctx->ctx.cache.sent += total;
752 return total;
753}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100754
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200755static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
756 uint32_t info, struct shared_block *shblk, unsigned int offset)
757{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100758
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200759 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
760 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
761 unsigned int max, total, rem_data;
762 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100763
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200764 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
765 if (!max)
766 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100767
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200768 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200769 if (appctx->ctx.cache.rem_data) {
770 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200771 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200772 }
773 else {
774 blksz = (info & 0xfffffff);
775 total = 4;
776 }
777 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200778 rem_data = blksz - max;
779 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100780 }
781
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200782 while (blksz) {
783 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100784
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200785 max = MIN(blksz, shctx->block_size - offset);
786 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
787 offset += sz;
788 blksz -= sz;
789 total += sz;
790 if (sz < max)
791 break;
792 if (blksz || offset == shctx->block_size) {
793 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
794 offset = 0;
795 }
796 }
797
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200798 appctx->ctx.cache.offset = offset;
799 appctx->ctx.cache.next = shblk;
800 appctx->ctx.cache.sent += total;
801 appctx->ctx.cache.rem_data = rem_data + blksz;
802 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100803}
804
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200805static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
806 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100807{
Christopher Faulet95220e22018-12-07 17:34:39 +0100808 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
809 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200810 struct shared_block *shblk;
811 unsigned int offset, sz;
812 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100813
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200814 while (len) {
815 enum htx_blk_type type;
816 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100817
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200818 shblk = appctx->ctx.cache.next;
819 offset = appctx->ctx.cache.offset;
820 if (appctx->ctx.cache.rem_data) {
821 type = HTX_BLK_DATA;
822 info = 0;
823 goto add_data_blk;
824 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100825
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200826 /* Get info of the next HTX block. May be splitted on 2 shblk */
827 sz = MIN(4, shctx->block_size - offset);
828 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
829 offset += sz;
830 if (sz < 4) {
831 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
832 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
833 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100834 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200835
836 /* Get payload of the next HTX block and insert it. */
837 type = (info >> 28);
838 if (type != HTX_BLK_DATA)
839 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
840 else {
841 add_data_blk:
842 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100843 }
844
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200845 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100846 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200847 total += ret;
848 len -= ret;
849
850 if (appctx->ctx.cache.rem_data || type == mark)
851 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100852 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100853
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100854 return total;
855}
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200856
857static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
858{
859 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
860 unsigned int age;
861 char *end;
862
863 chunk_reset(&trash);
864 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
865 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
866 age = CACHE_ENTRY_MAX_AGE;
867 end = ultoa_o(age, b_head(&trash), b_size(&trash));
868 b_set_data(&trash, end - b_head(&trash));
869 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
870 return 0;
871 return 1;
872}
873
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200874static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100875{
876 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
877 struct shared_block *first = block_ptr(cache_ptr);
878 struct stream_interface *si = appctx->owner;
879 struct channel *req = si_oc(si);
880 struct channel *res = si_ic(si);
881 struct htx *req_htx, *res_htx;
882 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200883 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100884 size_t ret, total = 0;
885
886 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200887 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100888
889 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
890 goto out;
891
892 /* Check if the input buffer is avalaible. */
893 if (!b_size(&res->buf)) {
894 si_rx_room_blk(si);
895 goto out;
896 }
897
Willy Tarreauefef3232018-12-16 00:37:45 +0100898 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100899 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100900
901 if (appctx->st0 == HTX_CACHE_INIT) {
902 appctx->ctx.cache.next = block_ptr(cache_ptr);
903 appctx->ctx.cache.offset = sizeof(*cache_ptr);
904 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200905 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100906 appctx->st0 = HTX_CACHE_HEADER;
907 }
908
909 if (appctx->st0 == HTX_CACHE_HEADER) {
910 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200911 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
912 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
913 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
914 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100915 goto error;
916
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200917 /* Skip response body for HEAD requests */
918 if (si_strm(si)->txn->meth == HTTP_METH_HEAD)
Christopher Fauletf0dd0372019-02-25 11:08:34 +0100919 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100920 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200921 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100922 }
923
924 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200925 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
926 if (len) {
927 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
928 if (ret < len) {
929 si_rx_room_blk(si);
930 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100931 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100932 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200933 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100934 }
935
936 if (appctx->st0 == HTX_CACHE_EOM) {
937 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
938 si_rx_room_blk(si);
939 goto out;
940 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100941 appctx->st0 = HTX_CACHE_END;
942 }
943
944 end:
Christopher Fauletadb36312019-02-25 11:40:49 +0100945 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100946 res->flags |= CF_READ_NULL;
947 si_shutr(si);
948 }
949
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100950 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200951 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +0100952 if (total)
953 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100954 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +0100955
956 /* eat the whole request */
957 if (co_data(req)) {
958 req_htx = htx_from_buf(&req->buf);
959 co_htx_skip(req, req_htx, co_data(req));
960 htx_to_buf(req_htx, &req->buf);
961 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100962 return;
963
964 error:
965 /* Sent and HTTP error 500 */
966 b_reset(&res->buf);
967 errmsg = &htx_err_chunks[HTTP_ERR_500];
968 res->buf.data = b_data(errmsg);
969 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
970 res_htx = htx_from_buf(&res->buf);
971
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200972 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100973 appctx->st0 = HTX_CACHE_END;
974 goto end;
975}
976
977
Christopher Faulet95220e22018-12-07 17:34:39 +0100978static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +0100979{
980 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +0100981 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +0100982
Christopher Faulet95220e22018-12-07 17:34:39 +0100983 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +0100984 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +0100985 goto err;
William Lallemand41db4602017-10-30 11:15:51 +0100986 }
987
988 /* check if a cache filter was already registered with this cache
989 * name, if that's the case, must use it. */
990 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +0100991 if (fconf->id == cache_store_flt_id) {
992 cconf = fconf->conf;
993 if (cconf && !strcmp((char *)cconf->c.name, name)) {
994 rule->arg.act.p[0] = cconf;
995 return 1;
996 }
William Lallemand41db4602017-10-30 11:15:51 +0100997 }
998 }
999
Christopher Faulet95220e22018-12-07 17:34:39 +01001000 /* Create the filter cache config */
1001 cconf = calloc(1, sizeof(*cconf));
1002 if (!cconf) {
1003 memprintf(err, "out of memory\n");
1004 goto err;
1005 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001006 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001007 cconf->c.name = strdup(name);
1008 if (!cconf->c.name) {
1009 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001010 goto err;
1011 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001012
William Lallemand41db4602017-10-30 11:15:51 +01001013 /* register a filter to fill the cache buffer */
1014 fconf = calloc(1, sizeof(*fconf));
1015 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001016 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001017 goto err;
1018 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001019 fconf->id = cache_store_flt_id;
1020 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001021 fconf->ops = &cache_ops;
1022 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1023
Christopher Faulet95220e22018-12-07 17:34:39 +01001024 rule->arg.act.p[0] = cconf;
1025 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001026
Christopher Faulet95220e22018-12-07 17:34:39 +01001027 err:
1028 free(cconf);
1029 return 0;
1030}
1031
1032enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1033 struct act_rule *rule, char **err)
1034{
1035 rule->action = ACT_CUSTOM;
1036 rule->action_ptr = http_action_store_cache;
1037
1038 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1039 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001040
Christopher Faulet95220e22018-12-07 17:34:39 +01001041 (*orig_arg)++;
1042 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001043}
1044
William Lallemandf528fff2017-11-23 19:43:17 +01001045/* This produces a sha1 hash of the concatenation of the first
1046 * occurrence of the Host header followed by the path component if it
1047 * begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001048int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001049{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001050 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001051 struct htx *htx = htxbuf(&s->req.buf);
1052 struct htx_sl *sl;
1053 struct http_hdr_ctx ctx;
1054 struct ist path;
William Lallemandf528fff2017-11-23 19:43:17 +01001055 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001056 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001057
William Lallemandf528fff2017-11-23 19:43:17 +01001058 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001059 ctx.blk = NULL;
1060 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1061 return 0;
1062 chunk_memcat(trash, ctx.value.ptr, ctx.value.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001063
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001064 sl = http_get_stline(htx);
1065 path = http_get_path(htx_sl_req_uri(sl));
1066 if (!path.ptr)
1067 return 0;
1068 chunk_memcat(trash, path.ptr, path.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001069
1070 /* hash everything */
1071 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001072 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001073 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1074
1075 return 1;
1076}
1077
William Lallemand41db4602017-10-30 11:15:51 +01001078enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1079 struct session *sess, struct stream *s, int flags)
1080{
William Lallemand77c11972017-10-31 20:43:01 +01001081
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001082 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001083 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001084 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1085 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001086
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001087 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1088 * and HEAD */
1089 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
1090 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
1091 txn->flags |= TX_CACHE_IGNORE;
1092
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001093 htx_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001094
Willy Tarreau504455c2017-12-22 17:47:35 +01001095 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1096 return ACT_RET_CONT;
1097
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001098 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001099 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001100
Willy Tarreau504455c2017-12-22 17:47:35 +01001101 if (s->txn->flags & TX_CACHE_IGNORE)
1102 return ACT_RET_CONT;
1103
Willy Tarreaua1214a52018-12-14 14:00:25 +01001104 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001105 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001106 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001107 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001108
William Lallemanda400a3a2017-11-20 19:13:12 +01001109 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001110 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001111 if (res) {
1112 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001113 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1114 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001115 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001116 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001117 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001118 appctx->rule = rule;
1119 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001120 appctx->ctx.cache.next = NULL;
1121 appctx->ctx.cache.sent = 0;
Willy Tarreaua1214a52018-12-14 14:00:25 +01001122
1123 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001124 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001125 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001126 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001127 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001128 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001129 shctx_lock(shctx_ptr(cache));
1130 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1131 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001132 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001133 }
1134 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001135 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001136 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001137}
1138
1139
1140enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1141 struct act_rule *rule, char **err)
1142{
William Lallemand41db4602017-10-30 11:15:51 +01001143 rule->action = ACT_CUSTOM;
1144 rule->action_ptr = http_action_req_cache_use;
1145
Christopher Faulet95220e22018-12-07 17:34:39 +01001146 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001147 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001148
1149 (*orig_arg)++;
1150 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001151}
1152
1153int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1154{
1155 int err_code = 0;
1156
1157 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1158
1159 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001160 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1161 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001162 err_code |= ERR_ALERT | ERR_ABORT;
1163 goto out;
1164 }
1165
1166 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1167 err_code |= ERR_ABORT;
1168 goto out;
1169 }
1170
1171 if (tmp_cache_config == NULL) {
1172 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1173 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001174 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001175 err_code |= ERR_ALERT | ERR_ABORT;
1176 goto out;
1177 }
1178
1179 strlcpy2(tmp_cache_config->id, args[1], 33);
1180 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001181 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1182 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001183 err_code |= ERR_WARN;
1184 }
William Lallemand49b44532017-11-24 18:53:43 +01001185 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001186 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001187 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001188 }
1189 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001190 unsigned long int maxsize;
1191 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001192
1193 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1194 err_code |= ERR_ABORT;
1195 goto out;
1196 }
1197
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001198 maxsize = strtoul(args[1], &err, 10);
1199 if (err == args[1] || *err != '\0') {
1200 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1201 file, linenum, args[1]);
1202 err_code |= ERR_ABORT;
1203 goto out;
1204 }
1205
1206 if (maxsize > (UINT_MAX >> 20)) {
1207 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1208 file, linenum, args[1], UINT_MAX >> 20);
1209 err_code |= ERR_ABORT;
1210 goto out;
1211 }
1212
William Lallemand41db4602017-10-30 11:15:51 +01001213 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001214 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001215 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001216 } else if (strcmp(args[0], "max-age") == 0) {
1217 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1218 err_code |= ERR_ABORT;
1219 goto out;
1220 }
1221
1222 if (!*args[1]) {
1223 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1224 file, linenum, args[0]);
1225 err_code |= ERR_WARN;
1226 }
1227
1228 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001229 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001230 unsigned int maxobjsz;
1231 char *err;
1232
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001233 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1234 err_code |= ERR_ABORT;
1235 goto out;
1236 }
1237
1238 if (!*args[1]) {
1239 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1240 file, linenum, args[0]);
1241 err_code |= ERR_WARN;
1242 }
1243
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001244 maxobjsz = strtoul(args[1], &err, 10);
1245 if (err == args[1] || *err != '\0') {
1246 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1247 file, linenum, args[1]);
1248 err_code |= ERR_ABORT;
1249 goto out;
1250 }
1251 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001252 }
1253 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001254 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001255 err_code |= ERR_ALERT | ERR_FATAL;
1256 goto out;
1257 }
1258out:
1259 return err_code;
1260}
1261
1262/* once the cache section is parsed */
1263
1264int cfg_post_parse_section_cache()
1265{
1266 struct shared_context *shctx;
1267 int err_code = 0;
1268 int ret_shctx;
1269
1270 if (tmp_cache_config) {
1271 struct cache *cache;
1272
1273 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001274 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001275 err_code |= ERR_FATAL | ERR_ALERT;
1276 goto out;
1277 }
1278
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001279 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001280 /* Default max. file size is a 256th of the cache size. */
1281 tmp_cache_config->maxobjsz =
1282 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001283 }
1284 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1285 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1286 err_code |= ERR_FATAL | ERR_ALERT;
1287 goto out;
1288 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001289
1290 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE,
1291 tmp_cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001292
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001293 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001294 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001295 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001296 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001297 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001298
1299 err_code |= ERR_FATAL | ERR_ALERT;
1300 goto out;
1301 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001302 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +01001303 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
1304 cache = (struct cache *)shctx->data;
1305 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001306 LIST_ADDQ(&caches, &cache->list);
1307 }
1308out:
1309 free(tmp_cache_config);
1310 tmp_cache_config = NULL;
1311 return err_code;
1312
William Lallemand41db4602017-10-30 11:15:51 +01001313}
1314
William Lallemand41db4602017-10-30 11:15:51 +01001315struct flt_ops cache_ops = {
1316 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001317 .check = cache_store_check,
1318 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001319
William Lallemand4da3f8a2017-10-31 14:33:34 +01001320 /* Handle channels activity */
1321 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001322 .channel_end_analyze = cache_store_chn_end_analyze,
Christopher Faulet839791a2019-01-07 16:12:07 +01001323 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001324
1325 /* Filter HTTP requests and responses */
1326 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001327 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001328 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001329};
1330
Christopher Faulet99a17a22018-12-11 09:18:27 +01001331
1332
1333static int
1334parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1335 struct flt_conf *fconf, char **err, void *private)
1336{
1337 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001338 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001339 char *name = NULL;
1340 int pos = *cur_arg;
1341
1342 /* Get the cache filter name*/
1343 if (!strcmp(args[pos], "cache")) {
1344 if (!*args[pos + 1]) {
1345 memprintf(err, "%s : expects an <id> argument", args[pos]);
1346 goto error;
1347 }
1348 name = strdup(args[pos + 1]);
1349 if (!name) {
1350 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1351 goto error;
1352 }
1353 pos += 2;
1354 }
1355
1356 /* Check if an implicit filter with the same name already exists. If so,
1357 * we remove the implicit filter to use the explicit one. */
1358 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1359 if (f->id != cache_store_flt_id)
1360 continue;
1361
1362 cconf = f->conf;
1363 if (strcmp(name, cconf->c.name)) {
1364 cconf = NULL;
1365 continue;
1366 }
1367
1368 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1369 cconf = NULL;
1370 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1371 px->id, name);
1372 return -1;
1373 }
1374
1375 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1376 LIST_DEL(&f->list);
1377 free(f);
1378 free(name);
1379 break;
1380 }
1381
1382 /* No implicit cache filter found, create configuration for the explicit one */
1383 if (!cconf) {
1384 cconf = calloc(1, sizeof(*cconf));
1385 if (!cconf) {
1386 memprintf(err, "%s: out of memory", args[*cur_arg]);
1387 goto error;
1388 }
1389 cconf->c.name = name;
1390 }
1391
1392 cconf->flags = 0;
1393 fconf->id = cache_store_flt_id;
1394 fconf->conf = cconf;
1395 fconf->ops = &cache_ops;
1396
1397 *cur_arg = pos;
1398 return 0;
1399
1400 error:
1401 free(name);
1402 free(cconf);
1403 return -1;
1404}
1405
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001406static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001407{
1408 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1409 return 1;
1410
1411 return 0;
1412}
1413
1414static int cli_io_handler_show_cache(struct appctx *appctx)
1415{
1416 struct cache* cache = appctx->ctx.cli.p0;
1417 struct stream_interface *si = appctx->owner;
1418
William Lallemand1f49a362017-11-21 20:01:26 +01001419 if (cache == NULL) {
1420 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1421 }
1422
1423 list_for_each_entry_from(cache, &caches, list) {
1424 struct eb32_node *node = NULL;
1425 unsigned int next_key;
1426 struct cache_entry *entry;
1427
William Lallemand1f49a362017-11-21 20:01:26 +01001428 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001429 if (!next_key) {
1430 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1431 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001432 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001433 return 0;
1434 }
1435 }
William Lallemand1f49a362017-11-21 20:01:26 +01001436
1437 appctx->ctx.cli.p0 = cache;
1438
1439 while (1) {
1440
1441 shctx_lock(shctx_ptr(cache));
1442 node = eb32_lookup_ge(&cache->entries, next_key);
1443 if (!node) {
1444 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001445 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001446 break;
1447 }
1448
1449 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001450 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 +01001451
1452 next_key = node->key + 1;
1453 appctx->ctx.cli.i0 = next_key;
1454
1455 shctx_unlock(shctx_ptr(cache));
1456
1457 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001458 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001459 return 0;
1460 }
1461 }
1462
1463 }
1464
1465 return 1;
1466
1467}
1468
Christopher Faulet99a17a22018-12-11 09:18:27 +01001469/* Declare the filter parser for "cache" keyword */
1470static struct flt_kw_list filter_kws = { "CACHE", { }, {
1471 { "cache", parse_cache_flt, NULL },
1472 { NULL, NULL, NULL },
1473 }
1474};
1475
1476INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1477
William Lallemand1f49a362017-11-21 20:01:26 +01001478static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001479 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1480 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001481}};
1482
Willy Tarreau0108d902018-11-25 19:14:37 +01001483INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001484
William Lallemand41db4602017-10-30 11:15:51 +01001485static struct action_kw_list http_res_actions = {
1486 .kw = {
1487 { "cache-store", parse_cache_store },
1488 { NULL, NULL }
1489 }
1490};
1491
Willy Tarreau0108d902018-11-25 19:14:37 +01001492INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1493
William Lallemand41db4602017-10-30 11:15:51 +01001494static struct action_kw_list http_req_actions = {
1495 .kw = {
1496 { "cache-use", parse_cache_use },
1497 { NULL, NULL }
1498 }
1499};
1500
Willy Tarreau0108d902018-11-25 19:14:37 +01001501INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1502
Willy Tarreau2231b632019-03-29 18:26:52 +01001503struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001504 .obj_type = OBJ_TYPE_APPLET,
1505 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001506 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001507 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001508};
1509
Willy Tarreaue6552512018-11-26 11:33:13 +01001510/* config parsers for this section */
1511REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);