blob: 4a0051f00a507da0a622fc892310046884a0b214 [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>
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010025#include <proto/http_htx.h>
William Lallemand41db4602017-10-30 11:15:51 +010026#include <proto/filters.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020027#include <proto/http_rules.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020028#include <proto/http_ana.h>
William Lallemand41db4602017-10-30 11:15:51 +010029#include <proto/log.h>
30#include <proto/stream.h>
31#include <proto/stream_interface.h>
32#include <proto/shctx.h>
33
William Lallemand41db4602017-10-30 11:15:51 +010034
35#include <common/cfgparse.h>
36#include <common/hash.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010037#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010038#include <common/initcall.h>
Willy Tarreau8b507582020-02-25 09:35:07 +010039#include <common/net_helper.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);
William Lallemandd1d1e222019-08-28 15:22:49 +020089static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +010090static struct cache *tmp_cache_config = NULL;
91
Willy Tarreau8ceae722018-11-26 11:58:30 +010092DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
93
William Lallemandf528fff2017-11-23 19:43:17 +010094struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010095{
96 struct eb32_node *node;
97 struct cache_entry *entry;
98
Willy Tarreau8b507582020-02-25 09:35:07 +010099 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100100 if (!node)
101 return NULL;
102
103 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100104
105 /* if that's not the right node */
106 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
107 return NULL;
108
William Lallemand08727662017-11-21 20:01:27 +0100109 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100110 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100111 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100112 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100113 entry->eb.key = 0;
114 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100115 return NULL;
116
117}
118
119static inline struct shared_context *shctx_ptr(struct cache *cache)
120{
121 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
122}
123
William Lallemand77c11972017-10-31 20:43:01 +0100124static inline struct shared_block *block_ptr(struct cache_entry *entry)
125{
126 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
127}
128
129
130
William Lallemand41db4602017-10-30 11:15:51 +0100131static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100132cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100133{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100134 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100135 return 0;
136}
137
Christopher Faulet95220e22018-12-07 17:34:39 +0100138static void
139cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
140{
141 struct cache_flt_conf *cconf = fconf->conf;
142
143 free(cconf);
144}
145
William Lallemand4da3f8a2017-10-31 14:33:34 +0100146static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100147cache_store_check(struct proxy *px, struct flt_conf *fconf)
148{
149 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100150 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100151 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100152 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100153
William Lallemandd1d1e222019-08-28 15:22:49 +0200154 /* Find the cache corresponding to the name in the filter config. The
155 * cache will not be referenced now in the filter config because it is
156 * not fully allocated. This step will be performed during the cache
157 * post_check.
158 */
159 list_for_each_entry(cache, &caches_config, list) {
160 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100161 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100162 }
163
164 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
165 proxy_type_str(px), px->id, (char *)cconf->c.name);
166 return 1;
167
168 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100169 /* Here <cache> points on the cache the filter must use and <cconf>
170 * points on the cache filter configuration. */
171
172 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100173 * enabled and if it is after the cache. When the compression is before
174 * the cache, an error is returned. Also check if the cache filter must
175 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100176 list_for_each_entry(f, &px->filter_configs, list) {
177 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100178 /* The compression filter must be evaluated after the cache. */
179 if (comp) {
180 ha_alert("config: %s '%s': unable to enable the compression filter before "
181 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
182 return 1;
183 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100184 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200185 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100186 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200187 else if (f->id == fcgi_flt_id)
188 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100189 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
190 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200191 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100192 * declaration is required. */
193 ha_alert("config: %s '%s': require an explicit filter declaration "
194 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
195 return 1;
196 }
197
Christopher Fauletafd819c2018-12-11 08:57:45 +0100198 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100199 return 0;
200}
201
202static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100203cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
204{
205 if (!(chn->flags & CF_ISRESP))
206 return 1;
207
208 if (filter->ctx == NULL) {
209 struct cache_st *st;
210
Willy Tarreaubafbe012017-11-24 17:34:44 +0100211 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100212 if (st == NULL)
213 return -1;
214
William Lallemand4da3f8a2017-10-31 14:33:34 +0100215 st->first_block = NULL;
216 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100217
218 /* Register post-analyzer on AN_RES_WAIT_HTTP */
219 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100220 }
221
William Lallemand4da3f8a2017-10-31 14:33:34 +0100222 return 1;
223}
224
225static int
William Lallemand49dc0482017-11-24 14:33:54 +0100226cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
227{
228 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100229 struct cache_flt_conf *cconf = FLT_CONF(filter);
230 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100231 struct shared_context *shctx = shctx_ptr(cache);
232
233 if (!(chn->flags & CF_ISRESP))
234 return 1;
235
236 /* Everything should be released in the http_end filter, but we need to do it
237 * there too, in case of errors */
238
239 if (st && st->first_block) {
240
241 shctx_lock(shctx);
242 shctx_row_dec_hot(shctx, st->first_block);
243 shctx_unlock(shctx);
244
245 }
246 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100247 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100248 filter->ctx = NULL;
249 }
250
251 return 1;
252}
253
Christopher Faulet839791a2019-01-07 16:12:07 +0100254static int
255cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
256 unsigned an_bit)
257{
258 struct http_txn *txn = s->txn;
259 struct http_msg *msg = &txn->rsp;
260 struct cache_st *st = filter->ctx;
261
262 if (an_bit != AN_RES_WAIT_HTTP)
263 goto end;
264
265 /* Here we need to check if any compression filter precedes the cache
266 * filter. This is only possible when the compression is configured in
267 * the frontend while the cache filter is configured on the
268 * backend. This case cannot be detected during HAProxy startup. So in
269 * such cases, the cache is disabled.
270 */
271 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
272 pool_free(pool_head_cache_st, st);
273 filter->ctx = NULL;
274 }
275
276 end:
277 return 1;
278}
William Lallemand49dc0482017-11-24 14:33:54 +0100279
280static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100281cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
282{
283 struct cache_st *st = filter->ctx;
284
William Lallemand4da3f8a2017-10-31 14:33:34 +0100285 if (!(msg->chn->flags & CF_ISRESP) || !st)
286 return 1;
287
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200288 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100289 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100290 return 1;
291}
292
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200293static inline void disable_cache_entry(struct cache_st *st,
294 struct filter *filter, struct shared_context *shctx)
295{
296 struct cache_entry *object;
297
298 object = (struct cache_entry *)st->first_block->data;
299 filter->ctx = NULL; /* disable cache */
300 shctx_lock(shctx);
301 shctx_row_dec_hot(shctx, st->first_block);
302 object->eb.key = 0;
303 shctx_unlock(shctx);
304 pool_free(pool_head_cache_st, st);
305}
306
William Lallemand4da3f8a2017-10-31 14:33:34 +0100307static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100308cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
309 unsigned int offset, unsigned int len)
310{
Christopher Faulet95220e22018-12-07 17:34:39 +0100311 struct cache_flt_conf *cconf = FLT_CONF(filter);
312 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100313 struct cache_st *st = filter->ctx;
314 struct htx *htx = htxbuf(&msg->chn->buf);
315 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200316 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100317 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200318 unsigned int orig_len, to_forward;
319 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100320
321 if (!len)
322 return len;
323
324 if (!st->first_block) {
325 unregister_data_filter(s, msg->chn, filter);
326 return len;
327 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100328
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200329 chunk_reset(&trash);
330 orig_len = len;
331 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100332
333 htxret = htx_find_offset(htx, offset);
334 blk = htxret.blk;
335 offset = htxret.ret;
336 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100337 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200338 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100339 struct ist v;
340
341 switch (type) {
342 case HTX_BLK_UNUSED:
343 break;
344
345 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100346 v = htx_get_blk_value(htx, blk);
347 v.ptr += offset;
348 v.len -= offset;
349 if (v.len > len)
350 v.len = len;
351
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200352 info = (type << 28) + v.len;
353 chunk_memcat(&trash, (char *)&info, sizeof(info));
354 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100355 to_forward += v.len;
356 len -= v.len;
357 break;
358
359 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200360 /* Here offset must always be 0 because only
361 * DATA blocks can be partially transferred. */
362 if (offset)
363 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100364 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200365 goto end;
366
367 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
368 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100369 to_forward += sz;
370 len -= sz;
371 break;
372 }
373
374 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100375 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200376
377 end:
378 shctx_lock(shctx);
379 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
380 if (!fb) {
381 shctx_unlock(shctx);
382 goto no_cache;
383 }
384 shctx_unlock(shctx);
385
386 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
387 (unsigned char *)b_head(&trash), b_data(&trash));
388 if (ret < 0)
389 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100390
391 return to_forward;
392
393 no_cache:
394 disable_cache_entry(st, filter, shctx);
395 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200396 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100397}
398
399static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100400cache_store_http_end(struct stream *s, struct filter *filter,
401 struct http_msg *msg)
402{
403 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100404 struct cache_flt_conf *cconf = FLT_CONF(filter);
405 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100406 struct shared_context *shctx = shctx_ptr(cache);
407 struct cache_entry *object;
408
409 if (!(msg->chn->flags & CF_ISRESP))
410 return 1;
411
412 if (st && st->first_block) {
413
414 object = (struct cache_entry *)st->first_block->data;
415
416 /* does not need to test if the insertion worked, if it
417 * doesn't, the blocks will be reused anyway */
418
419 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100420 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
421 object->eb.key = 0;
422 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100423 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100424 shctx_row_dec_hot(shctx, st->first_block);
425 shctx_unlock(shctx);
426
427 }
428 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100429 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100430 filter->ctx = NULL;
431 }
432
433 return 1;
434}
435
436 /*
437 * This intends to be used when checking HTTP headers for some
438 * word=value directive. Return a pointer to the first character of value, if
439 * the word was not found or if there wasn't any value assigned ot it return NULL
440 */
441char *directive_value(const char *sample, int slen, const char *word, int wlen)
442{
443 int st = 0;
444
445 if (slen < wlen)
446 return 0;
447
448 while (wlen) {
449 char c = *sample ^ *word;
450 if (c && c != ('A' ^ 'a'))
451 return NULL;
452 sample++;
453 word++;
454 slen--;
455 wlen--;
456 }
457
458 while (slen) {
459 if (st == 0) {
460 if (*sample != '=')
461 return NULL;
462 sample++;
463 slen--;
464 st = 1;
465 continue;
466 } else {
467 return (char *)sample;
468 }
469 }
470
471 return NULL;
472}
473
474/*
475 * Return the maxage in seconds of an HTTP response.
476 * Compute the maxage using either:
477 * - the assigned max-age of the cache
478 * - the s-maxage directive
479 * - the max-age directive
480 * - (Expires - Data) headers
481 * - the default-max-age of the cache
482 *
483 */
William Lallemand49b44532017-11-24 18:53:43 +0100484int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100485{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200486 struct htx *htx = htxbuf(&s->res.buf);
487 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100488 int smaxage = -1;
489 int maxage = -1;
490
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200491 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
492 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100493
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200494 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
495 if (value) {
496 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100497
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200498 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
499 chunk_strncat(chk, "", 1);
500 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100501 }
502
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200503 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
504 if (value) {
505 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200506
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200507 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
508 chunk_strncat(chk, "", 1);
509 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100510 }
511 }
512
513 /* TODO: Expires - Data */
514
515
516 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100517 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100518
519 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100520 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100521
William Lallemand49b44532017-11-24 18:53:43 +0100522 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100523
524}
525
526
William Lallemanda400a3a2017-11-20 19:13:12 +0100527static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
528{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200529 struct cache_entry *object = (struct cache_entry *)block->data;
530
531 if (first == block && object->eb.key)
532 eb32_delete(&object->eb);
533 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100534}
535
William Lallemand41db4602017-10-30 11:15:51 +0100536/*
537 * This fonction will store the headers of the response in a buffer and then
538 * register a filter to store the data
539 */
540enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200541 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100542{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200543 unsigned int age;
544 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100545 struct http_txn *txn = s->txn;
546 struct http_msg *msg = &txn->rsp;
547 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100548 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100549 struct cache_flt_conf *cconf = rule->arg.act.p[0];
550 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100551 struct cache_st *cache_ctx = NULL;
552 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100553 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200554 struct htx *htx;
555 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200556 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200557 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100558
William Lallemand4da3f8a2017-10-31 14:33:34 +0100559 /* Don't cache if the response came from a cache */
560 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
561 s->target == &http_cache_applet.obj_type) {
562 goto out;
563 }
564
565 /* cache only HTTP/1.1 */
566 if (!(txn->req.flags & HTTP_MSGF_VER_11))
567 goto out;
568
Willy Tarreau6905d182019-10-01 17:59:17 +0200569 /* cache only GET method */
570 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100571 goto out;
572
Willy Tarreauc9036c02019-01-11 19:38:25 +0100573 /* cache key was not computed */
574 if (!key)
575 goto out;
576
William Lallemand4da3f8a2017-10-31 14:33:34 +0100577 /* cache only 200 status code */
578 if (txn->status != 200)
579 goto out;
580
Christopher Faulet839791a2019-01-07 16:12:07 +0100581 /* Find the corresponding filter instance for the current stream */
582 list_for_each_entry(filter, &s->strm_flt.filters, list) {
583 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
584 /* No filter ctx, don't cache anything */
585 if (!filter->ctx)
586 goto out;
587 cache_ctx = filter->ctx;
588 break;
589 }
590 }
591
592 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200593 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100594
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200595 /* Do not cache too big objects. */
596 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
597 htx->data + htx->extra > shctx->max_obj_size)
598 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100599
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200600 /* Does not manage Vary at the moment. We will need a secondary key later for that */
601 ctx.blk = NULL;
602 if (http_find_header(htx, ist("Vary"), &ctx, 0))
603 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100604
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200605 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100606
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200607 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
608 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100609
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200610 age = 0;
611 ctx.blk = NULL;
612 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
613 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
614 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
615 hdr_age = CACHE_ENTRY_MAX_AGE;
616 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100617 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200618 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100619 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100620
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200621 chunk_reset(&trash);
622 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
623 struct htx_blk *blk = htx_get_blk(htx, pos);
624 enum htx_blk_type type = htx_get_blk_type(blk);
625 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100626
Christopher Fauletb0667472019-09-03 22:22:12 +0200627 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200628 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
629 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
630 if (type == HTX_BLK_EOH)
631 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200632 }
633
Christopher Fauletb0667472019-09-03 22:22:12 +0200634 /* Do not cache objects if the headers are too big. */
635 if (hdrs_len > htx->size - global.tune.maxrewrite)
636 goto out;
637
William Lallemand4da3f8a2017-10-31 14:33:34 +0100638 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200639 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100640 if (!first) {
641 shctx_unlock(shctx);
642 goto out;
643 }
644 shctx_unlock(shctx);
645
Willy Tarreau1093a452018-04-06 19:02:25 +0200646 /* the received memory is not initialized, we need at least to mark
647 * the object as not indexed yet.
648 */
649 object = (struct cache_entry *)first->data;
650 object->eb.node.leaf_p = NULL;
651 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200652 object->age = age;
Willy Tarreau1093a452018-04-06 19:02:25 +0200653
William Lallemand4da3f8a2017-10-31 14:33:34 +0100654 /* reserve space for the cache_entry structure */
655 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200656 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100657 /* cache the headers in a http action because it allows to chose what
658 * to cache, for example you might want to cache a response before
659 * modifying some HTTP headers, or on the contrary after modifying
660 * those headers.
661 */
662
663 /* does not need to be locked because it's in the "hot" list,
664 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200665 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
666 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100667
668 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100669 if (cache_ctx) {
670 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100671
Willy Tarreauc9036c02019-01-11 19:38:25 +0100672 object->eb.key = key;
673
Christopher Faulet839791a2019-01-07 16:12:07 +0100674 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
675 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100676
Christopher Faulet839791a2019-01-07 16:12:07 +0100677 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100678
Christopher Faulet839791a2019-01-07 16:12:07 +0100679 old = entry_exist(cconf->c.cache, txn->cache_hash);
680 if (old) {
681 eb32_delete(&old->eb);
682 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100683 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100684 shctx_unlock(shctx);
685
686 /* store latest value and expiration time */
687 object->latest_validation = now.tv_sec;
688 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
689 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100690 }
691
692out:
693 /* if does not cache */
694 if (first) {
695 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100696 first->len = 0;
697 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100698 shctx_row_dec_hot(shctx, first);
699 shctx_unlock(shctx);
700 }
701
William Lallemand41db4602017-10-30 11:15:51 +0100702 return ACT_RET_CONT;
703}
704
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100705#define HTX_CACHE_INIT 0 /* Initial state. */
706#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
707#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200708#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
709#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100710
William Lallemandecb73b12017-11-24 14:33:55 +0100711static void http_cache_applet_release(struct appctx *appctx)
712{
Christopher Faulet95220e22018-12-07 17:34:39 +0100713 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100714 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100715 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100716 struct shared_block *first = block_ptr(cache_ptr);
717
718 shctx_lock(shctx_ptr(cache));
719 shctx_row_dec_hot(shctx_ptr(cache), first);
720 shctx_unlock(shctx_ptr(cache));
721}
722
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200723
724static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
725 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100726{
Christopher Faulet95220e22018-12-07 17:34:39 +0100727 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
728 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200729 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200730 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200731 unsigned int max, total;
732 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100733
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200734 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
735 if (!max)
736 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200737 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200738 ? (info & 0xff) + ((info >> 8) & 0xfffff)
739 : info & 0xfffffff);
740 if (blksz > max)
741 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100742
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200743 blk = htx_add_blk(htx, type, blksz);
744 if (!blk)
745 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100746
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200747 blk->info = info;
748 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200749 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200750 while (blksz) {
751 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200752 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200753 offset += max;
754 blksz -= max;
755 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200756 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200757 if (blksz || offset == shctx->block_size) {
758 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
759 offset = 0;
760 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100761 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200762 appctx->ctx.cache.offset = offset;
763 appctx->ctx.cache.next = shblk;
764 appctx->ctx.cache.sent += total;
765 return total;
766}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100767
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200768static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
769 uint32_t info, struct shared_block *shblk, unsigned int offset)
770{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100771
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200772 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
773 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
774 unsigned int max, total, rem_data;
775 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100776
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200777 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
778 if (!max)
779 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100780
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200781 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200782 if (appctx->ctx.cache.rem_data) {
783 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200784 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200785 }
786 else {
787 blksz = (info & 0xfffffff);
788 total = 4;
789 }
790 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200791 rem_data = blksz - max;
792 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100793 }
794
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200795 while (blksz) {
796 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100797
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200798 max = MIN(blksz, shctx->block_size - offset);
799 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
800 offset += sz;
801 blksz -= sz;
802 total += sz;
803 if (sz < max)
804 break;
805 if (blksz || offset == shctx->block_size) {
806 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
807 offset = 0;
808 }
809 }
810
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200811 appctx->ctx.cache.offset = offset;
812 appctx->ctx.cache.next = shblk;
813 appctx->ctx.cache.sent += total;
814 appctx->ctx.cache.rem_data = rem_data + blksz;
815 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100816}
817
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200818static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
819 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100820{
Christopher Faulet95220e22018-12-07 17:34:39 +0100821 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
822 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200823 struct shared_block *shblk;
824 unsigned int offset, sz;
825 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100826
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200827 while (len) {
828 enum htx_blk_type type;
829 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100830
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200831 shblk = appctx->ctx.cache.next;
832 offset = appctx->ctx.cache.offset;
833 if (appctx->ctx.cache.rem_data) {
834 type = HTX_BLK_DATA;
835 info = 0;
836 goto add_data_blk;
837 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100838
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200839 /* Get info of the next HTX block. May be splitted on 2 shblk */
840 sz = MIN(4, shctx->block_size - offset);
841 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
842 offset += sz;
843 if (sz < 4) {
844 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
845 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
846 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100847 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200848
849 /* Get payload of the next HTX block and insert it. */
850 type = (info >> 28);
851 if (type != HTX_BLK_DATA)
852 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
853 else {
854 add_data_blk:
855 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100856 }
857
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200858 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100859 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200860 total += ret;
861 len -= ret;
862
863 if (appctx->ctx.cache.rem_data || type == mark)
864 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100865 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100866
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100867 return total;
868}
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200869
870static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
871{
872 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
873 unsigned int age;
874 char *end;
875
876 chunk_reset(&trash);
877 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
878 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
879 age = CACHE_ENTRY_MAX_AGE;
880 end = ultoa_o(age, b_head(&trash), b_size(&trash));
881 b_set_data(&trash, end - b_head(&trash));
882 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
883 return 0;
884 return 1;
885}
886
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200887static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100888{
889 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
890 struct shared_block *first = block_ptr(cache_ptr);
891 struct stream_interface *si = appctx->owner;
892 struct channel *req = si_oc(si);
893 struct channel *res = si_ic(si);
894 struct htx *req_htx, *res_htx;
895 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200896 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100897 size_t ret, total = 0;
898
899 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200900 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100901
902 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
903 goto out;
904
905 /* Check if the input buffer is avalaible. */
906 if (!b_size(&res->buf)) {
907 si_rx_room_blk(si);
908 goto out;
909 }
910
Willy Tarreauefef3232018-12-16 00:37:45 +0100911 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100912 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100913
914 if (appctx->st0 == HTX_CACHE_INIT) {
915 appctx->ctx.cache.next = block_ptr(cache_ptr);
916 appctx->ctx.cache.offset = sizeof(*cache_ptr);
917 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200918 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100919 appctx->st0 = HTX_CACHE_HEADER;
920 }
921
922 if (appctx->st0 == HTX_CACHE_HEADER) {
923 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200924 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
925 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
926 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
927 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100928 goto error;
929
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200930 /* Skip response body for HEAD requests */
931 if (si_strm(si)->txn->meth == HTTP_METH_HEAD)
Christopher Fauletf0dd0372019-02-25 11:08:34 +0100932 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100933 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200934 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100935 }
936
937 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200938 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
939 if (len) {
940 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
941 if (ret < len) {
942 si_rx_room_blk(si);
943 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100944 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100945 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200946 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100947 }
948
949 if (appctx->st0 == HTX_CACHE_EOM) {
950 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
951 si_rx_room_blk(si);
952 goto out;
953 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100954 appctx->st0 = HTX_CACHE_END;
955 }
956
957 end:
Christopher Fauletadb36312019-02-25 11:40:49 +0100958 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100959 res->flags |= CF_READ_NULL;
960 si_shutr(si);
961 }
962
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100963 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200964 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +0100965 if (total)
966 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100967 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +0100968
969 /* eat the whole request */
970 if (co_data(req)) {
971 req_htx = htx_from_buf(&req->buf);
972 co_htx_skip(req, req_htx, co_data(req));
973 htx_to_buf(req_htx, &req->buf);
974 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100975 return;
976
977 error:
978 /* Sent and HTTP error 500 */
979 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +0200980 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100981 res->buf.data = b_data(errmsg);
982 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
983 res_htx = htx_from_buf(&res->buf);
984
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200985 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100986 appctx->st0 = HTX_CACHE_END;
987 goto end;
988}
989
990
Christopher Faulet95220e22018-12-07 17:34:39 +0100991static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +0100992{
993 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +0100994 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +0100995
Christopher Faulet95220e22018-12-07 17:34:39 +0100996 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +0100997 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +0100998 goto err;
William Lallemand41db4602017-10-30 11:15:51 +0100999 }
1000
1001 /* check if a cache filter was already registered with this cache
1002 * name, if that's the case, must use it. */
1003 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001004 if (fconf->id == cache_store_flt_id) {
1005 cconf = fconf->conf;
1006 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1007 rule->arg.act.p[0] = cconf;
1008 return 1;
1009 }
William Lallemand41db4602017-10-30 11:15:51 +01001010 }
1011 }
1012
Christopher Faulet95220e22018-12-07 17:34:39 +01001013 /* Create the filter cache config */
1014 cconf = calloc(1, sizeof(*cconf));
1015 if (!cconf) {
1016 memprintf(err, "out of memory\n");
1017 goto err;
1018 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001019 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001020 cconf->c.name = strdup(name);
1021 if (!cconf->c.name) {
1022 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001023 goto err;
1024 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001025
William Lallemand41db4602017-10-30 11:15:51 +01001026 /* register a filter to fill the cache buffer */
1027 fconf = calloc(1, sizeof(*fconf));
1028 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001029 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001030 goto err;
1031 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001032 fconf->id = cache_store_flt_id;
1033 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001034 fconf->ops = &cache_ops;
1035 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1036
Christopher Faulet95220e22018-12-07 17:34:39 +01001037 rule->arg.act.p[0] = cconf;
1038 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001039
Christopher Faulet95220e22018-12-07 17:34:39 +01001040 err:
1041 free(cconf);
1042 return 0;
1043}
1044
1045enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1046 struct act_rule *rule, char **err)
1047{
1048 rule->action = ACT_CUSTOM;
1049 rule->action_ptr = http_action_store_cache;
1050
1051 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1052 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001053
Christopher Faulet95220e22018-12-07 17:34:39 +01001054 (*orig_arg)++;
1055 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001056}
1057
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001058/* This produces a sha1 hash of the concatenation of the HTTP method,
1059 * the first occurrence of the Host header followed by the path component
1060 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001061int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001062{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001063 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001064 struct htx *htx = htxbuf(&s->req.buf);
1065 struct htx_sl *sl;
1066 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001067 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001068 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001069 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001070
William Lallemandf528fff2017-11-23 19:43:17 +01001071 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001072 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001073
1074 switch (txn->meth) {
1075 case HTTP_METH_HEAD:
1076 case HTTP_METH_GET:
1077 chunk_memcat(trash, "GET", 3);
1078 break;
1079 default:
1080 return 0;
1081 }
1082
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001083 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001084 uri = htx_sl_req_uri(sl); // whole uri
1085 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001086 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001087
1088 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1089 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1090 * URIs are almost always sent in absolute form with their scheme. In
1091 * this case, the scheme is almost always "https". In order to support
1092 * sharing of cache objects between H1 and H2, we'll hash the absolute
1093 * URI whenever known, or prepend "https://" + the Host header for
1094 * relative URIs. The difference will only appear on absolute HTTP/1
1095 * requests sent to an origin server, which practically is never met in
1096 * the real world so we don't care about the ability to share the same
1097 * key here.URIs are normalized from the absolute URI to an origin form as
1098 * well.
1099 */
1100 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001101 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001102 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1103 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001104 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001105 }
1106
1107 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001108
1109 /* hash everything */
1110 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001111 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001112 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1113
1114 return 1;
1115}
1116
William Lallemand41db4602017-10-30 11:15:51 +01001117enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1118 struct session *sess, struct stream *s, int flags)
1119{
William Lallemand77c11972017-10-31 20:43:01 +01001120
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001121 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001122 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001123 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1124 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001125
Willy Tarreau6905d182019-10-01 17:59:17 +02001126 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1127 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001128 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001129 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001130 txn->flags |= TX_CACHE_IGNORE;
1131
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001132 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001133
Willy Tarreau504455c2017-12-22 17:47:35 +01001134 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1135 return ACT_RET_CONT;
1136
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001137 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001138 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001139
Willy Tarreau504455c2017-12-22 17:47:35 +01001140 if (s->txn->flags & TX_CACHE_IGNORE)
1141 return ACT_RET_CONT;
1142
Willy Tarreaua1214a52018-12-14 14:00:25 +01001143 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001144 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001145 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001146 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001147
William Lallemanda400a3a2017-11-20 19:13:12 +01001148 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001149 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001150 if (res) {
1151 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001152 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1153 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001154 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001155 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001156 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001157 appctx->rule = rule;
1158 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001159 appctx->ctx.cache.next = NULL;
1160 appctx->ctx.cache.sent = 0;
Willy Tarreaua1214a52018-12-14 14:00:25 +01001161
1162 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001163 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001164 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001165 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001166 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001167 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001168 shctx_lock(shctx_ptr(cache));
1169 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1170 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001171 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001172 }
1173 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001174 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001175 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001176}
1177
1178
1179enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1180 struct act_rule *rule, char **err)
1181{
William Lallemand41db4602017-10-30 11:15:51 +01001182 rule->action = ACT_CUSTOM;
1183 rule->action_ptr = http_action_req_cache_use;
1184
Christopher Faulet95220e22018-12-07 17:34:39 +01001185 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001186 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001187
1188 (*orig_arg)++;
1189 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001190}
1191
1192int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1193{
1194 int err_code = 0;
1195
1196 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1197
1198 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001199 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1200 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001201 err_code |= ERR_ALERT | ERR_ABORT;
1202 goto out;
1203 }
1204
1205 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1206 err_code |= ERR_ABORT;
1207 goto out;
1208 }
1209
1210 if (tmp_cache_config == NULL) {
1211 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1212 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001213 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001214 err_code |= ERR_ALERT | ERR_ABORT;
1215 goto out;
1216 }
1217
1218 strlcpy2(tmp_cache_config->id, args[1], 33);
1219 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001220 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1221 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001222 err_code |= ERR_WARN;
1223 }
William Lallemand49b44532017-11-24 18:53:43 +01001224 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001225 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001226 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001227 }
1228 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001229 unsigned long int maxsize;
1230 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001231
1232 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1233 err_code |= ERR_ABORT;
1234 goto out;
1235 }
1236
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001237 maxsize = strtoul(args[1], &err, 10);
1238 if (err == args[1] || *err != '\0') {
1239 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1240 file, linenum, args[1]);
1241 err_code |= ERR_ABORT;
1242 goto out;
1243 }
1244
1245 if (maxsize > (UINT_MAX >> 20)) {
1246 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1247 file, linenum, args[1], UINT_MAX >> 20);
1248 err_code |= ERR_ABORT;
1249 goto out;
1250 }
1251
William Lallemand41db4602017-10-30 11:15:51 +01001252 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001253 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001254 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001255 } else if (strcmp(args[0], "max-age") == 0) {
1256 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1257 err_code |= ERR_ABORT;
1258 goto out;
1259 }
1260
1261 if (!*args[1]) {
1262 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1263 file, linenum, args[0]);
1264 err_code |= ERR_WARN;
1265 }
1266
1267 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001268 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001269 unsigned int maxobjsz;
1270 char *err;
1271
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001272 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1273 err_code |= ERR_ABORT;
1274 goto out;
1275 }
1276
1277 if (!*args[1]) {
1278 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1279 file, linenum, args[0]);
1280 err_code |= ERR_WARN;
1281 }
1282
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001283 maxobjsz = strtoul(args[1], &err, 10);
1284 if (err == args[1] || *err != '\0') {
1285 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1286 file, linenum, args[1]);
1287 err_code |= ERR_ABORT;
1288 goto out;
1289 }
1290 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001291 }
1292 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001293 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001294 err_code |= ERR_ALERT | ERR_FATAL;
1295 goto out;
1296 }
1297out:
1298 return err_code;
1299}
1300
1301/* once the cache section is parsed */
1302
1303int cfg_post_parse_section_cache()
1304{
William Lallemand41db4602017-10-30 11:15:51 +01001305 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001306
1307 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001308
1309 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001310 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001311 err_code |= ERR_FATAL | ERR_ALERT;
1312 goto out;
1313 }
1314
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001315 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001316 /* Default max. file size is a 256th of the cache size. */
1317 tmp_cache_config->maxobjsz =
1318 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001319 }
1320 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1321 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1322 err_code |= ERR_FATAL | ERR_ALERT;
1323 goto out;
1324 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001325
William Lallemandd1d1e222019-08-28 15:22:49 +02001326 /* add to the list of cache to init and reinit tmp_cache_config
1327 * for next cache section, if any.
1328 */
1329 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1330 tmp_cache_config = NULL;
1331 return err_code;
1332 }
1333out:
1334 free(tmp_cache_config);
1335 tmp_cache_config = NULL;
1336 return err_code;
1337
1338}
1339
1340int post_check_cache()
1341{
1342 struct proxy *px;
1343 struct cache *back, *cache_config, *cache;
1344 struct shared_context *shctx;
1345 int ret_shctx;
1346 int err_code = 0;
1347
1348 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1349
1350 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1351 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001352
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001353 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001354 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001355 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001356 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001357 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001358
1359 err_code |= ERR_FATAL | ERR_ALERT;
1360 goto out;
1361 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001362 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001363 /* the cache structure is stored in the shctx and added to the
1364 * caches list, we can remove the entry from the caches_config
1365 * list */
1366 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001367 cache = (struct cache *)shctx->data;
1368 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001369 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001370 LIST_DEL(&cache_config->list);
1371 free(cache_config);
1372
1373 /* Find all references for this cache in the existing filters
1374 * (over all proxies) and reference it in matching filters.
1375 */
1376 for (px = proxies_list; px; px = px->next) {
1377 struct flt_conf *fconf;
1378 struct cache_flt_conf *cconf;
1379
1380 list_for_each_entry(fconf, &px->filter_configs, list) {
1381 if (fconf->id != cache_store_flt_id)
1382 continue;
1383
1384 cconf = fconf->conf;
1385 if (!strcmp(cache->id, cconf->c.name)) {
1386 free(cconf->c.name);
1387 cconf->c.cache = cache;
1388 break;
1389 }
1390 }
1391 }
William Lallemand41db4602017-10-30 11:15:51 +01001392 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001393
William Lallemand41db4602017-10-30 11:15:51 +01001394out:
William Lallemand41db4602017-10-30 11:15:51 +01001395 return err_code;
1396
William Lallemand41db4602017-10-30 11:15:51 +01001397}
1398
William Lallemand41db4602017-10-30 11:15:51 +01001399struct flt_ops cache_ops = {
1400 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001401 .check = cache_store_check,
1402 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001403
William Lallemand4da3f8a2017-10-31 14:33:34 +01001404 /* Handle channels activity */
1405 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001406 .channel_end_analyze = cache_store_chn_end_analyze,
Christopher Faulet839791a2019-01-07 16:12:07 +01001407 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001408
1409 /* Filter HTTP requests and responses */
1410 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001411 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001412 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001413};
1414
Christopher Faulet99a17a22018-12-11 09:18:27 +01001415
1416
1417static int
1418parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1419 struct flt_conf *fconf, char **err, void *private)
1420{
1421 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001422 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001423 char *name = NULL;
1424 int pos = *cur_arg;
1425
1426 /* Get the cache filter name*/
1427 if (!strcmp(args[pos], "cache")) {
1428 if (!*args[pos + 1]) {
1429 memprintf(err, "%s : expects an <id> argument", args[pos]);
1430 goto error;
1431 }
1432 name = strdup(args[pos + 1]);
1433 if (!name) {
1434 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1435 goto error;
1436 }
1437 pos += 2;
1438 }
1439
1440 /* Check if an implicit filter with the same name already exists. If so,
1441 * we remove the implicit filter to use the explicit one. */
1442 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1443 if (f->id != cache_store_flt_id)
1444 continue;
1445
1446 cconf = f->conf;
1447 if (strcmp(name, cconf->c.name)) {
1448 cconf = NULL;
1449 continue;
1450 }
1451
1452 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1453 cconf = NULL;
1454 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1455 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01001456 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001457 }
1458
1459 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1460 LIST_DEL(&f->list);
1461 free(f);
1462 free(name);
1463 break;
1464 }
1465
1466 /* No implicit cache filter found, create configuration for the explicit one */
1467 if (!cconf) {
1468 cconf = calloc(1, sizeof(*cconf));
1469 if (!cconf) {
1470 memprintf(err, "%s: out of memory", args[*cur_arg]);
1471 goto error;
1472 }
1473 cconf->c.name = name;
1474 }
1475
1476 cconf->flags = 0;
1477 fconf->id = cache_store_flt_id;
1478 fconf->conf = cconf;
1479 fconf->ops = &cache_ops;
1480
1481 *cur_arg = pos;
1482 return 0;
1483
1484 error:
1485 free(name);
1486 free(cconf);
1487 return -1;
1488}
1489
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001490static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001491{
1492 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1493 return 1;
1494
1495 return 0;
1496}
1497
1498static int cli_io_handler_show_cache(struct appctx *appctx)
1499{
1500 struct cache* cache = appctx->ctx.cli.p0;
1501 struct stream_interface *si = appctx->owner;
1502
William Lallemand1f49a362017-11-21 20:01:26 +01001503 if (cache == NULL) {
1504 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1505 }
1506
1507 list_for_each_entry_from(cache, &caches, list) {
1508 struct eb32_node *node = NULL;
1509 unsigned int next_key;
1510 struct cache_entry *entry;
1511
William Lallemand1f49a362017-11-21 20:01:26 +01001512 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001513 if (!next_key) {
1514 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1515 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001516 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001517 return 0;
1518 }
1519 }
William Lallemand1f49a362017-11-21 20:01:26 +01001520
1521 appctx->ctx.cli.p0 = cache;
1522
1523 while (1) {
1524
1525 shctx_lock(shctx_ptr(cache));
1526 node = eb32_lookup_ge(&cache->entries, next_key);
1527 if (!node) {
1528 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001529 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001530 break;
1531 }
1532
1533 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01001534 chunk_printf(&trash, "%p hash:%u size:%u (%u blocks), refcount:%u, expire:%d\n", entry, read_u32(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 +01001535
1536 next_key = node->key + 1;
1537 appctx->ctx.cli.i0 = next_key;
1538
1539 shctx_unlock(shctx_ptr(cache));
1540
1541 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001542 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001543 return 0;
1544 }
1545 }
1546
1547 }
1548
1549 return 1;
1550
1551}
1552
Christopher Faulet99a17a22018-12-11 09:18:27 +01001553/* Declare the filter parser for "cache" keyword */
1554static struct flt_kw_list filter_kws = { "CACHE", { }, {
1555 { "cache", parse_cache_flt, NULL },
1556 { NULL, NULL, NULL },
1557 }
1558};
1559
1560INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1561
William Lallemand1f49a362017-11-21 20:01:26 +01001562static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001563 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1564 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001565}};
1566
Willy Tarreau0108d902018-11-25 19:14:37 +01001567INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001568
William Lallemand41db4602017-10-30 11:15:51 +01001569static struct action_kw_list http_res_actions = {
1570 .kw = {
1571 { "cache-store", parse_cache_store },
1572 { NULL, NULL }
1573 }
1574};
1575
Willy Tarreau0108d902018-11-25 19:14:37 +01001576INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1577
William Lallemand41db4602017-10-30 11:15:51 +01001578static struct action_kw_list http_req_actions = {
1579 .kw = {
1580 { "cache-use", parse_cache_use },
1581 { NULL, NULL }
1582 }
1583};
1584
Willy Tarreau0108d902018-11-25 19:14:37 +01001585INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1586
Willy Tarreau2231b632019-03-29 18:26:52 +01001587struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001588 .obj_type = OBJ_TYPE_APPLET,
1589 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001590 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001591 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001592};
1593
Willy Tarreaue6552512018-11-26 11:33:13 +01001594/* config parsers for this section */
1595REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02001596REGISTER_POST_CHECK(post_check_cache);