blob: e95493ab203ba75b369df251ad38177fa561cd42 [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
Willy Tarreau122eba92020-06-04 10:15:32 +020013#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020015#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020016#include <haproxy/cli.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020017#include <haproxy/filters.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020018#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020019#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020020#include <haproxy/http_rules.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020021#include <haproxy/log.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020022#include <haproxy/proxy.h>
Willy Tarreau334099c2020-06-03 18:38:48 +020023#include <haproxy/shctx.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020024#include <haproxy/stream_interface.h>
Willy Tarreau8d2b7772020-05-27 10:58:19 +020025#include <import/eb32tree.h>
William Lallemandf528fff2017-11-23 19:43:17 +010026#include <import/sha1.h>
William Lallemand41db4602017-10-30 11:15:51 +010027
William Lallemand41db4602017-10-30 11:15:51 +010028#include <proto/stream.h>
William Lallemand41db4602017-10-30 11:15:51 +010029
William Lallemand41db4602017-10-30 11:15:51 +010030
31#include <common/cfgparse.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020032#include <haproxy/hash.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020033#include <haproxy/htx.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020034#include <haproxy/net_helper.h>
William Lallemand41db4602017-10-30 11:15:51 +010035
Christopher Faulet27d93c32018-12-15 22:32:02 +010036#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010037 * the filter keyword) */
Christopher Fauletafd819c2018-12-11 08:57:45 +010038
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010039const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010040
Willy Tarreau2231b632019-03-29 18:26:52 +010041extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010042
43struct flt_ops cache_ops;
44
45struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010046 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010047 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010048 unsigned int maxage; /* max-age */
49 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020050 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010051 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010052};
53
Christopher Faulet95220e22018-12-07 17:34:39 +010054/* cache config for filters */
55struct cache_flt_conf {
56 union {
57 struct cache *cache; /* cache used by the filter */
58 char *name; /* cache name used during conf parsing */
59 } c;
60 unsigned int flags; /* CACHE_FLT_F_* */
61};
62
William Lallemand41db4602017-10-30 11:15:51 +010063/*
64 * cache ctx for filters
65 */
66struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +010067 struct shared_block *first_block;
68};
69
70struct cache_entry {
71 unsigned int latest_validation; /* latest validation date */
72 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020073 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010074
William Lallemand41db4602017-10-30 11:15:51 +010075 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010076 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010077 unsigned char data[0];
78};
79
80#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010081#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010082
83static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +020084static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +010085static struct cache *tmp_cache_config = NULL;
86
Willy Tarreau8ceae722018-11-26 11:58:30 +010087DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
88
William Lallemandf528fff2017-11-23 19:43:17 +010089struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010090{
91 struct eb32_node *node;
92 struct cache_entry *entry;
93
Willy Tarreau8b507582020-02-25 09:35:07 +010094 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010095 if (!node)
96 return NULL;
97
98 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +010099
100 /* if that's not the right node */
101 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
102 return NULL;
103
William Lallemand08727662017-11-21 20:01:27 +0100104 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100105 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100106 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100107 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100108 entry->eb.key = 0;
109 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100110 return NULL;
111
112}
113
114static inline struct shared_context *shctx_ptr(struct cache *cache)
115{
116 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
117}
118
William Lallemand77c11972017-10-31 20:43:01 +0100119static inline struct shared_block *block_ptr(struct cache_entry *entry)
120{
121 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
122}
123
124
125
William Lallemand41db4602017-10-30 11:15:51 +0100126static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100127cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100128{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100129 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100130 return 0;
131}
132
Christopher Faulet95220e22018-12-07 17:34:39 +0100133static void
134cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
135{
136 struct cache_flt_conf *cconf = fconf->conf;
137
138 free(cconf);
139}
140
William Lallemand4da3f8a2017-10-31 14:33:34 +0100141static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100142cache_store_check(struct proxy *px, struct flt_conf *fconf)
143{
144 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100145 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100146 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100147 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100148
William Lallemandd1d1e222019-08-28 15:22:49 +0200149 /* Find the cache corresponding to the name in the filter config. The
150 * cache will not be referenced now in the filter config because it is
151 * not fully allocated. This step will be performed during the cache
152 * post_check.
153 */
154 list_for_each_entry(cache, &caches_config, list) {
155 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100156 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100157 }
158
159 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
160 proxy_type_str(px), px->id, (char *)cconf->c.name);
161 return 1;
162
163 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100164 /* Here <cache> points on the cache the filter must use and <cconf>
165 * points on the cache filter configuration. */
166
167 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100168 * enabled and if it is after the cache. When the compression is before
169 * the cache, an error is returned. Also check if the cache filter must
170 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100171 list_for_each_entry(f, &px->filter_configs, list) {
172 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100173 /* The compression filter must be evaluated after the cache. */
174 if (comp) {
175 ha_alert("config: %s '%s': unable to enable the compression filter before "
176 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
177 return 1;
178 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100179 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200180 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100181 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200182 else if (f->id == fcgi_flt_id)
183 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100184 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
185 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200186 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100187 * declaration is required. */
188 ha_alert("config: %s '%s': require an explicit filter declaration "
189 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
190 return 1;
191 }
192
Christopher Fauletafd819c2018-12-11 08:57:45 +0100193 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100194 return 0;
195}
196
197static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100198cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100199{
Christopher Faulet65554e12020-03-06 14:52:06 +0100200 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100201
Christopher Faulet65554e12020-03-06 14:52:06 +0100202 st = pool_alloc_dirty(pool_head_cache_st);
203 if (st == NULL)
204 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100205
Christopher Faulet65554e12020-03-06 14:52:06 +0100206 st->first_block = NULL;
207 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100208
Christopher Faulet65554e12020-03-06 14:52:06 +0100209 /* Register post-analyzer on AN_RES_WAIT_HTTP */
210 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100211 return 1;
212}
213
Christopher Faulet65554e12020-03-06 14:52:06 +0100214static void
215cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100216{
217 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100218 struct cache_flt_conf *cconf = FLT_CONF(filter);
219 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100220 struct shared_context *shctx = shctx_ptr(cache);
221
William Lallemand49dc0482017-11-24 14:33:54 +0100222 /* Everything should be released in the http_end filter, but we need to do it
223 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100224 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100225 shctx_lock(shctx);
226 shctx_row_dec_hot(shctx, st->first_block);
227 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100228 }
229 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100230 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100231 filter->ctx = NULL;
232 }
William Lallemand49dc0482017-11-24 14:33:54 +0100233}
234
Christopher Faulet839791a2019-01-07 16:12:07 +0100235static int
236cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
237 unsigned an_bit)
238{
239 struct http_txn *txn = s->txn;
240 struct http_msg *msg = &txn->rsp;
241 struct cache_st *st = filter->ctx;
242
243 if (an_bit != AN_RES_WAIT_HTTP)
244 goto end;
245
246 /* Here we need to check if any compression filter precedes the cache
247 * filter. This is only possible when the compression is configured in
248 * the frontend while the cache filter is configured on the
249 * backend. This case cannot be detected during HAProxy startup. So in
250 * such cases, the cache is disabled.
251 */
252 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
253 pool_free(pool_head_cache_st, st);
254 filter->ctx = NULL;
255 }
256
257 end:
258 return 1;
259}
William Lallemand49dc0482017-11-24 14:33:54 +0100260
261static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100262cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
263{
264 struct cache_st *st = filter->ctx;
265
William Lallemand4da3f8a2017-10-31 14:33:34 +0100266 if (!(msg->chn->flags & CF_ISRESP) || !st)
267 return 1;
268
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200269 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100270 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100271 return 1;
272}
273
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200274static inline void disable_cache_entry(struct cache_st *st,
275 struct filter *filter, struct shared_context *shctx)
276{
277 struct cache_entry *object;
278
279 object = (struct cache_entry *)st->first_block->data;
280 filter->ctx = NULL; /* disable cache */
281 shctx_lock(shctx);
282 shctx_row_dec_hot(shctx, st->first_block);
283 object->eb.key = 0;
284 shctx_unlock(shctx);
285 pool_free(pool_head_cache_st, st);
286}
287
William Lallemand4da3f8a2017-10-31 14:33:34 +0100288static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100289cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
290 unsigned int offset, unsigned int len)
291{
Christopher Faulet95220e22018-12-07 17:34:39 +0100292 struct cache_flt_conf *cconf = FLT_CONF(filter);
293 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100294 struct cache_st *st = filter->ctx;
295 struct htx *htx = htxbuf(&msg->chn->buf);
296 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200297 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100298 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200299 unsigned int orig_len, to_forward;
300 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100301
302 if (!len)
303 return len;
304
305 if (!st->first_block) {
306 unregister_data_filter(s, msg->chn, filter);
307 return len;
308 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100309
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200310 chunk_reset(&trash);
311 orig_len = len;
312 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100313
314 htxret = htx_find_offset(htx, offset);
315 blk = htxret.blk;
316 offset = htxret.ret;
317 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100318 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200319 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100320 struct ist v;
321
322 switch (type) {
323 case HTX_BLK_UNUSED:
324 break;
325
326 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100327 v = htx_get_blk_value(htx, blk);
328 v.ptr += offset;
329 v.len -= offset;
330 if (v.len > len)
331 v.len = len;
332
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200333 info = (type << 28) + v.len;
334 chunk_memcat(&trash, (char *)&info, sizeof(info));
335 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100336 to_forward += v.len;
337 len -= v.len;
338 break;
339
340 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200341 /* Here offset must always be 0 because only
342 * DATA blocks can be partially transferred. */
343 if (offset)
344 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100345 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200346 goto end;
347
348 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
349 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100350 to_forward += sz;
351 len -= sz;
352 break;
353 }
354
355 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100356 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200357
358 end:
359 shctx_lock(shctx);
360 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
361 if (!fb) {
362 shctx_unlock(shctx);
363 goto no_cache;
364 }
365 shctx_unlock(shctx);
366
367 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
368 (unsigned char *)b_head(&trash), b_data(&trash));
369 if (ret < 0)
370 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100371
372 return to_forward;
373
374 no_cache:
375 disable_cache_entry(st, filter, shctx);
376 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200377 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100378}
379
380static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100381cache_store_http_end(struct stream *s, struct filter *filter,
382 struct http_msg *msg)
383{
384 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100385 struct cache_flt_conf *cconf = FLT_CONF(filter);
386 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100387 struct shared_context *shctx = shctx_ptr(cache);
388 struct cache_entry *object;
389
390 if (!(msg->chn->flags & CF_ISRESP))
391 return 1;
392
393 if (st && st->first_block) {
394
395 object = (struct cache_entry *)st->first_block->data;
396
397 /* does not need to test if the insertion worked, if it
398 * doesn't, the blocks will be reused anyway */
399
400 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100401 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
402 object->eb.key = 0;
403 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100404 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100405 shctx_row_dec_hot(shctx, st->first_block);
406 shctx_unlock(shctx);
407
408 }
409 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100410 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100411 filter->ctx = NULL;
412 }
413
414 return 1;
415}
416
417 /*
418 * This intends to be used when checking HTTP headers for some
419 * word=value directive. Return a pointer to the first character of value, if
420 * the word was not found or if there wasn't any value assigned ot it return NULL
421 */
422char *directive_value(const char *sample, int slen, const char *word, int wlen)
423{
424 int st = 0;
425
426 if (slen < wlen)
427 return 0;
428
429 while (wlen) {
430 char c = *sample ^ *word;
431 if (c && c != ('A' ^ 'a'))
432 return NULL;
433 sample++;
434 word++;
435 slen--;
436 wlen--;
437 }
438
439 while (slen) {
440 if (st == 0) {
441 if (*sample != '=')
442 return NULL;
443 sample++;
444 slen--;
445 st = 1;
446 continue;
447 } else {
448 return (char *)sample;
449 }
450 }
451
452 return NULL;
453}
454
455/*
456 * Return the maxage in seconds of an HTTP response.
457 * Compute the maxage using either:
458 * - the assigned max-age of the cache
459 * - the s-maxage directive
460 * - the max-age directive
461 * - (Expires - Data) headers
462 * - the default-max-age of the cache
463 *
464 */
William Lallemand49b44532017-11-24 18:53:43 +0100465int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100466{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200467 struct htx *htx = htxbuf(&s->res.buf);
468 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100469 int smaxage = -1;
470 int maxage = -1;
471
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200472 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
473 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100474
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200475 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
476 if (value) {
477 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100478
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200479 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
480 chunk_strncat(chk, "", 1);
481 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100482 }
483
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200484 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
485 if (value) {
486 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200487
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200488 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
489 chunk_strncat(chk, "", 1);
490 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100491 }
492 }
493
494 /* TODO: Expires - Data */
495
496
497 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100498 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100499
500 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100501 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100502
William Lallemand49b44532017-11-24 18:53:43 +0100503 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100504
505}
506
507
William Lallemanda400a3a2017-11-20 19:13:12 +0100508static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
509{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200510 struct cache_entry *object = (struct cache_entry *)block->data;
511
512 if (first == block && object->eb.key)
513 eb32_delete(&object->eb);
514 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100515}
516
William Lallemand41db4602017-10-30 11:15:51 +0100517/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500518 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100519 * register a filter to store the data
520 */
521enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200522 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100523{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200524 unsigned int age;
525 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100526 struct http_txn *txn = s->txn;
527 struct http_msg *msg = &txn->rsp;
528 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100529 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100530 struct cache_flt_conf *cconf = rule->arg.act.p[0];
531 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100532 struct cache_st *cache_ctx = NULL;
533 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100534 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200535 struct htx *htx;
536 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200537 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200538 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100539
William Lallemand4da3f8a2017-10-31 14:33:34 +0100540 /* Don't cache if the response came from a cache */
541 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
542 s->target == &http_cache_applet.obj_type) {
543 goto out;
544 }
545
546 /* cache only HTTP/1.1 */
547 if (!(txn->req.flags & HTTP_MSGF_VER_11))
548 goto out;
549
Willy Tarreau6905d182019-10-01 17:59:17 +0200550 /* cache only GET method */
551 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100552 goto out;
553
Willy Tarreauc9036c02019-01-11 19:38:25 +0100554 /* cache key was not computed */
555 if (!key)
556 goto out;
557
William Lallemand4da3f8a2017-10-31 14:33:34 +0100558 /* cache only 200 status code */
559 if (txn->status != 200)
560 goto out;
561
Christopher Faulet839791a2019-01-07 16:12:07 +0100562 /* Find the corresponding filter instance for the current stream */
563 list_for_each_entry(filter, &s->strm_flt.filters, list) {
564 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
565 /* No filter ctx, don't cache anything */
566 if (!filter->ctx)
567 goto out;
568 cache_ctx = filter->ctx;
569 break;
570 }
571 }
572
573 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200574 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100575
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200576 /* Do not cache too big objects. */
577 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
578 htx->data + htx->extra > shctx->max_obj_size)
579 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100580
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200581 /* Does not manage Vary at the moment. We will need a secondary key later for that */
582 ctx.blk = NULL;
583 if (http_find_header(htx, ist("Vary"), &ctx, 0))
584 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100585
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200586 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100587
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200588 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
589 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100590
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200591 age = 0;
592 ctx.blk = NULL;
593 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
594 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
595 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
596 hdr_age = CACHE_ENTRY_MAX_AGE;
597 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100598 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200599 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100600 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100601
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200602 chunk_reset(&trash);
603 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
604 struct htx_blk *blk = htx_get_blk(htx, pos);
605 enum htx_blk_type type = htx_get_blk_type(blk);
606 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100607
Christopher Fauletb0667472019-09-03 22:22:12 +0200608 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200609 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
610 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
611 if (type == HTX_BLK_EOH)
612 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200613 }
614
Christopher Fauletb0667472019-09-03 22:22:12 +0200615 /* Do not cache objects if the headers are too big. */
616 if (hdrs_len > htx->size - global.tune.maxrewrite)
617 goto out;
618
William Lallemand4da3f8a2017-10-31 14:33:34 +0100619 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200620 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100621 if (!first) {
622 shctx_unlock(shctx);
623 goto out;
624 }
625 shctx_unlock(shctx);
626
Willy Tarreau1093a452018-04-06 19:02:25 +0200627 /* the received memory is not initialized, we need at least to mark
628 * the object as not indexed yet.
629 */
630 object = (struct cache_entry *)first->data;
631 object->eb.node.leaf_p = NULL;
632 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200633 object->age = age;
Willy Tarreau1093a452018-04-06 19:02:25 +0200634
William Lallemand4da3f8a2017-10-31 14:33:34 +0100635 /* reserve space for the cache_entry structure */
636 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200637 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100638 /* cache the headers in a http action because it allows to chose what
639 * to cache, for example you might want to cache a response before
640 * modifying some HTTP headers, or on the contrary after modifying
641 * those headers.
642 */
643
644 /* does not need to be locked because it's in the "hot" list,
645 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200646 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
647 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100648
649 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100650 if (cache_ctx) {
651 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100652
Willy Tarreauc9036c02019-01-11 19:38:25 +0100653 object->eb.key = key;
654
Christopher Faulet839791a2019-01-07 16:12:07 +0100655 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
656 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100657
Christopher Faulet839791a2019-01-07 16:12:07 +0100658 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100659
Christopher Faulet839791a2019-01-07 16:12:07 +0100660 old = entry_exist(cconf->c.cache, txn->cache_hash);
661 if (old) {
662 eb32_delete(&old->eb);
663 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100664 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100665 shctx_unlock(shctx);
666
667 /* store latest value and expiration time */
668 object->latest_validation = now.tv_sec;
669 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
670 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100671 }
672
673out:
674 /* if does not cache */
675 if (first) {
676 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100677 first->len = 0;
678 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100679 shctx_row_dec_hot(shctx, first);
680 shctx_unlock(shctx);
681 }
682
William Lallemand41db4602017-10-30 11:15:51 +0100683 return ACT_RET_CONT;
684}
685
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100686#define HTX_CACHE_INIT 0 /* Initial state. */
687#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
688#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200689#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
690#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100691
William Lallemandecb73b12017-11-24 14:33:55 +0100692static void http_cache_applet_release(struct appctx *appctx)
693{
Christopher Faulet95220e22018-12-07 17:34:39 +0100694 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100695 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100696 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100697 struct shared_block *first = block_ptr(cache_ptr);
698
699 shctx_lock(shctx_ptr(cache));
700 shctx_row_dec_hot(shctx_ptr(cache), first);
701 shctx_unlock(shctx_ptr(cache));
702}
703
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200704
705static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
706 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100707{
Christopher Faulet95220e22018-12-07 17:34:39 +0100708 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
709 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200710 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200711 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200712 unsigned int max, total;
713 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100714
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200715 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
716 if (!max)
717 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200718 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200719 ? (info & 0xff) + ((info >> 8) & 0xfffff)
720 : info & 0xfffffff);
721 if (blksz > max)
722 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100723
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200724 blk = htx_add_blk(htx, type, blksz);
725 if (!blk)
726 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100727
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200728 blk->info = info;
729 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200730 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200731 while (blksz) {
732 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200733 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200734 offset += max;
735 blksz -= max;
736 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200737 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200738 if (blksz || offset == shctx->block_size) {
739 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
740 offset = 0;
741 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100742 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200743 appctx->ctx.cache.offset = offset;
744 appctx->ctx.cache.next = shblk;
745 appctx->ctx.cache.sent += total;
746 return total;
747}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100748
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200749static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
750 uint32_t info, struct shared_block *shblk, unsigned int offset)
751{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100752
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200753 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
754 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
755 unsigned int max, total, rem_data;
756 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100757
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200758 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
759 if (!max)
760 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100761
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200762 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200763 if (appctx->ctx.cache.rem_data) {
764 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200765 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200766 }
767 else {
768 blksz = (info & 0xfffffff);
769 total = 4;
770 }
771 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200772 rem_data = blksz - max;
773 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100774 }
775
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200776 while (blksz) {
777 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100778
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200779 max = MIN(blksz, shctx->block_size - offset);
780 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
781 offset += sz;
782 blksz -= sz;
783 total += sz;
784 if (sz < max)
785 break;
786 if (blksz || offset == shctx->block_size) {
787 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
788 offset = 0;
789 }
790 }
791
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200792 appctx->ctx.cache.offset = offset;
793 appctx->ctx.cache.next = shblk;
794 appctx->ctx.cache.sent += total;
795 appctx->ctx.cache.rem_data = rem_data + blksz;
796 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100797}
798
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200799static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
800 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100801{
Christopher Faulet95220e22018-12-07 17:34:39 +0100802 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
803 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200804 struct shared_block *shblk;
805 unsigned int offset, sz;
806 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100807
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200808 while (len) {
809 enum htx_blk_type type;
810 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100811
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200812 shblk = appctx->ctx.cache.next;
813 offset = appctx->ctx.cache.offset;
814 if (appctx->ctx.cache.rem_data) {
815 type = HTX_BLK_DATA;
816 info = 0;
817 goto add_data_blk;
818 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100819
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500820 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200821 sz = MIN(4, shctx->block_size - offset);
822 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
823 offset += sz;
824 if (sz < 4) {
825 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
826 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
827 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100828 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200829
830 /* Get payload of the next HTX block and insert it. */
831 type = (info >> 28);
832 if (type != HTX_BLK_DATA)
833 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
834 else {
835 add_data_blk:
836 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100837 }
838
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200839 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100840 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200841 total += ret;
842 len -= ret;
843
844 if (appctx->ctx.cache.rem_data || type == mark)
845 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100846 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100847
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100848 return total;
849}
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200850
851static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
852{
853 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
854 unsigned int age;
855 char *end;
856
857 chunk_reset(&trash);
858 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
859 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
860 age = CACHE_ENTRY_MAX_AGE;
861 end = ultoa_o(age, b_head(&trash), b_size(&trash));
862 b_set_data(&trash, end - b_head(&trash));
863 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
864 return 0;
865 return 1;
866}
867
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200868static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100869{
870 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
871 struct shared_block *first = block_ptr(cache_ptr);
872 struct stream_interface *si = appctx->owner;
873 struct channel *req = si_oc(si);
874 struct channel *res = si_ic(si);
875 struct htx *req_htx, *res_htx;
876 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200877 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100878 size_t ret, total = 0;
879
880 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200881 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100882
883 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
884 goto out;
885
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500886 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100887 if (!b_size(&res->buf)) {
888 si_rx_room_blk(si);
889 goto out;
890 }
891
Willy Tarreauefef3232018-12-16 00:37:45 +0100892 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100893 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100894
895 if (appctx->st0 == HTX_CACHE_INIT) {
896 appctx->ctx.cache.next = block_ptr(cache_ptr);
897 appctx->ctx.cache.offset = sizeof(*cache_ptr);
898 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200899 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100900 appctx->st0 = HTX_CACHE_HEADER;
901 }
902
903 if (appctx->st0 == HTX_CACHE_HEADER) {
904 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200905 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
906 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
907 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
908 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100909 goto error;
910
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200911 /* Skip response body for HEAD requests */
912 if (si_strm(si)->txn->meth == HTTP_METH_HEAD)
Christopher Fauletf0dd0372019-02-25 11:08:34 +0100913 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100914 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200915 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100916 }
917
918 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200919 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
920 if (len) {
921 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
922 if (ret < len) {
923 si_rx_room_blk(si);
924 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100925 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100926 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200927 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100928 }
929
930 if (appctx->st0 == HTX_CACHE_EOM) {
931 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
932 si_rx_room_blk(si);
933 goto out;
934 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100935 appctx->st0 = HTX_CACHE_END;
936 }
937
938 end:
Christopher Fauletadb36312019-02-25 11:40:49 +0100939 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100940 res->flags |= CF_READ_NULL;
941 si_shutr(si);
942 }
943
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100944 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200945 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +0100946 if (total)
947 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100948 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +0100949
950 /* eat the whole request */
951 if (co_data(req)) {
952 req_htx = htx_from_buf(&req->buf);
953 co_htx_skip(req, req_htx, co_data(req));
954 htx_to_buf(req_htx, &req->buf);
955 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100956 return;
957
958 error:
959 /* Sent and HTTP error 500 */
960 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +0200961 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100962 res->buf.data = b_data(errmsg);
963 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
964 res_htx = htx_from_buf(&res->buf);
965
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200966 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100967 appctx->st0 = HTX_CACHE_END;
968 goto end;
969}
970
971
Christopher Faulet95220e22018-12-07 17:34:39 +0100972static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +0100973{
974 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +0100975 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +0100976
Christopher Faulet95220e22018-12-07 17:34:39 +0100977 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +0100978 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +0100979 goto err;
William Lallemand41db4602017-10-30 11:15:51 +0100980 }
981
982 /* check if a cache filter was already registered with this cache
983 * name, if that's the case, must use it. */
984 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +0100985 if (fconf->id == cache_store_flt_id) {
986 cconf = fconf->conf;
987 if (cconf && !strcmp((char *)cconf->c.name, name)) {
988 rule->arg.act.p[0] = cconf;
989 return 1;
990 }
William Lallemand41db4602017-10-30 11:15:51 +0100991 }
992 }
993
Christopher Faulet95220e22018-12-07 17:34:39 +0100994 /* Create the filter cache config */
995 cconf = calloc(1, sizeof(*cconf));
996 if (!cconf) {
997 memprintf(err, "out of memory\n");
998 goto err;
999 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001000 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001001 cconf->c.name = strdup(name);
1002 if (!cconf->c.name) {
1003 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001004 goto err;
1005 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001006
William Lallemand41db4602017-10-30 11:15:51 +01001007 /* register a filter to fill the cache buffer */
1008 fconf = calloc(1, sizeof(*fconf));
1009 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001010 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001011 goto err;
1012 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001013 fconf->id = cache_store_flt_id;
1014 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001015 fconf->ops = &cache_ops;
1016 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1017
Christopher Faulet95220e22018-12-07 17:34:39 +01001018 rule->arg.act.p[0] = cconf;
1019 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001020
Christopher Faulet95220e22018-12-07 17:34:39 +01001021 err:
1022 free(cconf);
1023 return 0;
1024}
1025
1026enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1027 struct act_rule *rule, char **err)
1028{
1029 rule->action = ACT_CUSTOM;
1030 rule->action_ptr = http_action_store_cache;
1031
1032 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1033 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001034
Christopher Faulet95220e22018-12-07 17:34:39 +01001035 (*orig_arg)++;
1036 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001037}
1038
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001039/* This produces a sha1 hash of the concatenation of the HTTP method,
1040 * the first occurrence of the Host header followed by the path component
1041 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001042int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001043{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001044 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001045 struct htx *htx = htxbuf(&s->req.buf);
1046 struct htx_sl *sl;
1047 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001048 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001049 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001050 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001051
William Lallemandf528fff2017-11-23 19:43:17 +01001052 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001053 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001054
1055 switch (txn->meth) {
1056 case HTTP_METH_HEAD:
1057 case HTTP_METH_GET:
1058 chunk_memcat(trash, "GET", 3);
1059 break;
1060 default:
1061 return 0;
1062 }
1063
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001064 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001065 uri = htx_sl_req_uri(sl); // whole uri
1066 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001067 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001068
1069 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1070 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1071 * URIs are almost always sent in absolute form with their scheme. In
1072 * this case, the scheme is almost always "https". In order to support
1073 * sharing of cache objects between H1 and H2, we'll hash the absolute
1074 * URI whenever known, or prepend "https://" + the Host header for
1075 * relative URIs. The difference will only appear on absolute HTTP/1
1076 * requests sent to an origin server, which practically is never met in
1077 * the real world so we don't care about the ability to share the same
1078 * key here.URIs are normalized from the absolute URI to an origin form as
1079 * well.
1080 */
1081 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001082 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001083 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1084 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001085 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001086 }
1087
1088 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001089
1090 /* hash everything */
1091 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001092 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001093 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1094
1095 return 1;
1096}
1097
William Lallemand41db4602017-10-30 11:15:51 +01001098enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1099 struct session *sess, struct stream *s, int flags)
1100{
William Lallemand77c11972017-10-31 20:43:01 +01001101
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001102 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001103 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001104 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1105 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001106
Willy Tarreau6905d182019-10-01 17:59:17 +02001107 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1108 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001109 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001110 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001111 txn->flags |= TX_CACHE_IGNORE;
1112
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001113 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001114
Willy Tarreau504455c2017-12-22 17:47:35 +01001115 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1116 return ACT_RET_CONT;
1117
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001118 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001119 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001120
Willy Tarreau504455c2017-12-22 17:47:35 +01001121 if (s->txn->flags & TX_CACHE_IGNORE)
1122 return ACT_RET_CONT;
1123
Willy Tarreaua1214a52018-12-14 14:00:25 +01001124 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001125 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001126 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001127 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001128
William Lallemanda400a3a2017-11-20 19:13:12 +01001129 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001130 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001131 if (res) {
1132 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001133 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1134 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001135 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001136 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001137 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001138 appctx->rule = rule;
1139 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001140 appctx->ctx.cache.next = NULL;
1141 appctx->ctx.cache.sent = 0;
Willy Tarreaua1214a52018-12-14 14:00:25 +01001142
1143 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001144 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 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_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001147 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001148 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001149 shctx_lock(shctx_ptr(cache));
1150 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1151 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001152 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001153 }
1154 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001155 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001156 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001157}
1158
1159
1160enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1161 struct act_rule *rule, char **err)
1162{
William Lallemand41db4602017-10-30 11:15:51 +01001163 rule->action = ACT_CUSTOM;
1164 rule->action_ptr = http_action_req_cache_use;
1165
Christopher Faulet95220e22018-12-07 17:34:39 +01001166 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001167 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001168
1169 (*orig_arg)++;
1170 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001171}
1172
1173int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1174{
1175 int err_code = 0;
1176
1177 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1178
1179 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001180 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1181 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001182 err_code |= ERR_ALERT | ERR_ABORT;
1183 goto out;
1184 }
1185
1186 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1187 err_code |= ERR_ABORT;
1188 goto out;
1189 }
1190
1191 if (tmp_cache_config == NULL) {
1192 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1193 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001194 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001195 err_code |= ERR_ALERT | ERR_ABORT;
1196 goto out;
1197 }
1198
1199 strlcpy2(tmp_cache_config->id, args[1], 33);
1200 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001201 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1202 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001203 err_code |= ERR_WARN;
1204 }
William Lallemand49b44532017-11-24 18:53:43 +01001205 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001206 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001207 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001208 }
1209 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001210 unsigned long int maxsize;
1211 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001212
1213 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1214 err_code |= ERR_ABORT;
1215 goto out;
1216 }
1217
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001218 maxsize = strtoul(args[1], &err, 10);
1219 if (err == args[1] || *err != '\0') {
1220 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1221 file, linenum, args[1]);
1222 err_code |= ERR_ABORT;
1223 goto out;
1224 }
1225
1226 if (maxsize > (UINT_MAX >> 20)) {
1227 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1228 file, linenum, args[1], UINT_MAX >> 20);
1229 err_code |= ERR_ABORT;
1230 goto out;
1231 }
1232
William Lallemand41db4602017-10-30 11:15:51 +01001233 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001234 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001235 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001236 } else if (strcmp(args[0], "max-age") == 0) {
1237 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1238 err_code |= ERR_ABORT;
1239 goto out;
1240 }
1241
1242 if (!*args[1]) {
1243 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1244 file, linenum, args[0]);
1245 err_code |= ERR_WARN;
1246 }
1247
1248 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001249 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001250 unsigned int maxobjsz;
1251 char *err;
1252
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001253 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1254 err_code |= ERR_ABORT;
1255 goto out;
1256 }
1257
1258 if (!*args[1]) {
1259 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1260 file, linenum, args[0]);
1261 err_code |= ERR_WARN;
1262 }
1263
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001264 maxobjsz = strtoul(args[1], &err, 10);
1265 if (err == args[1] || *err != '\0') {
1266 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1267 file, linenum, args[1]);
1268 err_code |= ERR_ABORT;
1269 goto out;
1270 }
1271 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001272 }
1273 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001274 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001275 err_code |= ERR_ALERT | ERR_FATAL;
1276 goto out;
1277 }
1278out:
1279 return err_code;
1280}
1281
1282/* once the cache section is parsed */
1283
1284int cfg_post_parse_section_cache()
1285{
William Lallemand41db4602017-10-30 11:15:51 +01001286 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001287
1288 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001289
1290 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001291 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001292 err_code |= ERR_FATAL | ERR_ALERT;
1293 goto out;
1294 }
1295
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001296 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001297 /* Default max. file size is a 256th of the cache size. */
1298 tmp_cache_config->maxobjsz =
1299 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001300 }
1301 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1302 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1303 err_code |= ERR_FATAL | ERR_ALERT;
1304 goto out;
1305 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001306
William Lallemandd1d1e222019-08-28 15:22:49 +02001307 /* add to the list of cache to init and reinit tmp_cache_config
1308 * for next cache section, if any.
1309 */
1310 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1311 tmp_cache_config = NULL;
1312 return err_code;
1313 }
1314out:
1315 free(tmp_cache_config);
1316 tmp_cache_config = NULL;
1317 return err_code;
1318
1319}
1320
1321int post_check_cache()
1322{
1323 struct proxy *px;
1324 struct cache *back, *cache_config, *cache;
1325 struct shared_context *shctx;
1326 int ret_shctx;
1327 int err_code = 0;
1328
1329 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1330
1331 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1332 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001333
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001334 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001335 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001336 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001337 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001338 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001339
1340 err_code |= ERR_FATAL | ERR_ALERT;
1341 goto out;
1342 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001343 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001344 /* the cache structure is stored in the shctx and added to the
1345 * caches list, we can remove the entry from the caches_config
1346 * list */
1347 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001348 cache = (struct cache *)shctx->data;
1349 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001350 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001351 LIST_DEL(&cache_config->list);
1352 free(cache_config);
1353
1354 /* Find all references for this cache in the existing filters
1355 * (over all proxies) and reference it in matching filters.
1356 */
1357 for (px = proxies_list; px; px = px->next) {
1358 struct flt_conf *fconf;
1359 struct cache_flt_conf *cconf;
1360
1361 list_for_each_entry(fconf, &px->filter_configs, list) {
1362 if (fconf->id != cache_store_flt_id)
1363 continue;
1364
1365 cconf = fconf->conf;
1366 if (!strcmp(cache->id, cconf->c.name)) {
1367 free(cconf->c.name);
1368 cconf->c.cache = cache;
1369 break;
1370 }
1371 }
1372 }
William Lallemand41db4602017-10-30 11:15:51 +01001373 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001374
William Lallemand41db4602017-10-30 11:15:51 +01001375out:
William Lallemand41db4602017-10-30 11:15:51 +01001376 return err_code;
1377
William Lallemand41db4602017-10-30 11:15:51 +01001378}
1379
William Lallemand41db4602017-10-30 11:15:51 +01001380struct flt_ops cache_ops = {
1381 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001382 .check = cache_store_check,
1383 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001384
Christopher Faulet65554e12020-03-06 14:52:06 +01001385 /* Handle stream init/deinit */
1386 .attach = cache_store_strm_init,
1387 .detach = cache_store_strm_deinit,
1388
William Lallemand4da3f8a2017-10-31 14:33:34 +01001389 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001390 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001391
1392 /* Filter HTTP requests and responses */
1393 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001394 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001395 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001396};
1397
Christopher Faulet99a17a22018-12-11 09:18:27 +01001398
1399
1400static int
1401parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1402 struct flt_conf *fconf, char **err, void *private)
1403{
1404 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001405 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001406 char *name = NULL;
1407 int pos = *cur_arg;
1408
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001409 /* Get the cache filter name. <pos> point on "cache" keyword */
1410 if (!*args[pos + 1]) {
1411 memprintf(err, "%s : expects an <id> argument", args[pos]);
1412 goto error;
1413 }
1414 name = strdup(args[pos + 1]);
1415 if (!name) {
1416 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1417 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001418 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001419 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001420
1421 /* Check if an implicit filter with the same name already exists. If so,
1422 * we remove the implicit filter to use the explicit one. */
1423 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1424 if (f->id != cache_store_flt_id)
1425 continue;
1426
1427 cconf = f->conf;
1428 if (strcmp(name, cconf->c.name)) {
1429 cconf = NULL;
1430 continue;
1431 }
1432
1433 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1434 cconf = NULL;
1435 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1436 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01001437 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001438 }
1439
1440 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1441 LIST_DEL(&f->list);
1442 free(f);
1443 free(name);
1444 break;
1445 }
1446
1447 /* No implicit cache filter found, create configuration for the explicit one */
1448 if (!cconf) {
1449 cconf = calloc(1, sizeof(*cconf));
1450 if (!cconf) {
1451 memprintf(err, "%s: out of memory", args[*cur_arg]);
1452 goto error;
1453 }
1454 cconf->c.name = name;
1455 }
1456
1457 cconf->flags = 0;
1458 fconf->id = cache_store_flt_id;
1459 fconf->conf = cconf;
1460 fconf->ops = &cache_ops;
1461
1462 *cur_arg = pos;
1463 return 0;
1464
1465 error:
1466 free(name);
1467 free(cconf);
1468 return -1;
1469}
1470
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001471static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001472{
1473 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1474 return 1;
1475
1476 return 0;
1477}
1478
1479static int cli_io_handler_show_cache(struct appctx *appctx)
1480{
1481 struct cache* cache = appctx->ctx.cli.p0;
1482 struct stream_interface *si = appctx->owner;
1483
William Lallemand1f49a362017-11-21 20:01:26 +01001484 if (cache == NULL) {
1485 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1486 }
1487
1488 list_for_each_entry_from(cache, &caches, list) {
1489 struct eb32_node *node = NULL;
1490 unsigned int next_key;
1491 struct cache_entry *entry;
1492
William Lallemand1f49a362017-11-21 20:01:26 +01001493 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001494 if (!next_key) {
1495 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1496 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001497 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001498 return 0;
1499 }
1500 }
William Lallemand1f49a362017-11-21 20:01:26 +01001501
1502 appctx->ctx.cli.p0 = cache;
1503
1504 while (1) {
1505
1506 shctx_lock(shctx_ptr(cache));
1507 node = eb32_lookup_ge(&cache->entries, next_key);
1508 if (!node) {
1509 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001510 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001511 break;
1512 }
1513
1514 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01001515 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 +01001516
1517 next_key = node->key + 1;
1518 appctx->ctx.cli.i0 = next_key;
1519
1520 shctx_unlock(shctx_ptr(cache));
1521
1522 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001523 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001524 return 0;
1525 }
1526 }
1527
1528 }
1529
1530 return 1;
1531
1532}
1533
Christopher Faulet99a17a22018-12-11 09:18:27 +01001534/* Declare the filter parser for "cache" keyword */
1535static struct flt_kw_list filter_kws = { "CACHE", { }, {
1536 { "cache", parse_cache_flt, NULL },
1537 { NULL, NULL, NULL },
1538 }
1539};
1540
1541INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1542
William Lallemand1f49a362017-11-21 20:01:26 +01001543static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001544 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1545 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001546}};
1547
Willy Tarreau0108d902018-11-25 19:14:37 +01001548INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001549
William Lallemand41db4602017-10-30 11:15:51 +01001550static struct action_kw_list http_res_actions = {
1551 .kw = {
1552 { "cache-store", parse_cache_store },
1553 { NULL, NULL }
1554 }
1555};
1556
Willy Tarreau0108d902018-11-25 19:14:37 +01001557INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1558
William Lallemand41db4602017-10-30 11:15:51 +01001559static struct action_kw_list http_req_actions = {
1560 .kw = {
1561 { "cache-use", parse_cache_use },
1562 { NULL, NULL }
1563 }
1564};
1565
Willy Tarreau0108d902018-11-25 19:14:37 +01001566INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1567
Willy Tarreau2231b632019-03-29 18:26:52 +01001568struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001569 .obj_type = OBJ_TYPE_APPLET,
1570 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001571 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001572 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001573};
1574
Willy Tarreaue6552512018-11-26 11:33:13 +01001575/* config parsers for this section */
1576REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02001577REGISTER_POST_CHECK(post_check_cache);