blob: ad34a9d0b2716778f79defde4605cc9c1211f3bb [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 Tarreau87735332020-06-04 09:08:41 +020015#include <haproxy/http_htx.h>
Willy Tarreau334099c2020-06-03 18:38:48 +020016#include <haproxy/shctx.h>
Willy Tarreau8d2b7772020-05-27 10:58:19 +020017#include <import/eb32tree.h>
William Lallemandf528fff2017-11-23 19:43:17 +010018#include <import/sha1.h>
William Lallemand41db4602017-10-30 11:15:51 +010019
William Lallemand1f49a362017-11-21 20:01:26 +010020#include <types/cli.h>
William Lallemand75d93292017-11-21 20:01:24 +010021#include <types/filters.h>
22#include <types/proxy.h>
William Lallemand75d93292017-11-21 20:01:24 +010023
William Lallemand41db4602017-10-30 11:15:51 +010024#include <proto/channel.h>
William Lallemand1f49a362017-11-21 20:01:26 +010025#include <proto/cli.h>
William Lallemand41db4602017-10-30 11:15:51 +010026#include <proto/proxy.h>
William Lallemand41db4602017-10-30 11:15:51 +010027#include <proto/filters.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020028#include <proto/http_rules.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020029#include <proto/http_ana.h>
William Lallemand41db4602017-10-30 11:15:51 +010030#include <proto/log.h>
31#include <proto/stream.h>
32#include <proto/stream_interface.h>
William Lallemand41db4602017-10-30 11:15:51 +010033
William Lallemand41db4602017-10-30 11:15:51 +010034
35#include <common/cfgparse.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020036#include <haproxy/hash.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020037#include <haproxy/htx.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020038#include <haproxy/net_helper.h>
William Lallemand41db4602017-10-30 11:15:51 +010039
Christopher Faulet27d93c32018-12-15 22:32:02 +010040#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010041 * the filter keyword) */
Christopher Fauletafd819c2018-12-11 08:57:45 +010042
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010043const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010044
Willy Tarreau2231b632019-03-29 18:26:52 +010045extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010046
47struct flt_ops cache_ops;
48
49struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010050 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010051 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010052 unsigned int maxage; /* max-age */
53 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020054 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010055 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010056};
57
Christopher Faulet95220e22018-12-07 17:34:39 +010058/* cache config for filters */
59struct cache_flt_conf {
60 union {
61 struct cache *cache; /* cache used by the filter */
62 char *name; /* cache name used during conf parsing */
63 } c;
64 unsigned int flags; /* CACHE_FLT_F_* */
65};
66
William Lallemand41db4602017-10-30 11:15:51 +010067/*
68 * cache ctx for filters
69 */
70struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +010071 struct shared_block *first_block;
72};
73
74struct cache_entry {
75 unsigned int latest_validation; /* latest validation date */
76 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020077 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010078
William Lallemand41db4602017-10-30 11:15:51 +010079 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010080 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010081 unsigned char data[0];
82};
83
84#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010085#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010086
87static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +020088static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +010089static struct cache *tmp_cache_config = NULL;
90
Willy Tarreau8ceae722018-11-26 11:58:30 +010091DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
92
William Lallemandf528fff2017-11-23 19:43:17 +010093struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010094{
95 struct eb32_node *node;
96 struct cache_entry *entry;
97
Willy Tarreau8b507582020-02-25 09:35:07 +010098 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010099 if (!node)
100 return NULL;
101
102 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100103
104 /* if that's not the right node */
105 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
106 return NULL;
107
William Lallemand08727662017-11-21 20:01:27 +0100108 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100109 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100110 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100111 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100112 entry->eb.key = 0;
113 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100114 return NULL;
115
116}
117
118static inline struct shared_context *shctx_ptr(struct cache *cache)
119{
120 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
121}
122
William Lallemand77c11972017-10-31 20:43:01 +0100123static inline struct shared_block *block_ptr(struct cache_entry *entry)
124{
125 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
126}
127
128
129
William Lallemand41db4602017-10-30 11:15:51 +0100130static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100131cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100132{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100133 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100134 return 0;
135}
136
Christopher Faulet95220e22018-12-07 17:34:39 +0100137static void
138cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
139{
140 struct cache_flt_conf *cconf = fconf->conf;
141
142 free(cconf);
143}
144
William Lallemand4da3f8a2017-10-31 14:33:34 +0100145static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100146cache_store_check(struct proxy *px, struct flt_conf *fconf)
147{
148 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100149 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100150 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100151 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100152
William Lallemandd1d1e222019-08-28 15:22:49 +0200153 /* Find the cache corresponding to the name in the filter config. The
154 * cache will not be referenced now in the filter config because it is
155 * not fully allocated. This step will be performed during the cache
156 * post_check.
157 */
158 list_for_each_entry(cache, &caches_config, list) {
159 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100160 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100161 }
162
163 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
164 proxy_type_str(px), px->id, (char *)cconf->c.name);
165 return 1;
166
167 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100168 /* Here <cache> points on the cache the filter must use and <cconf>
169 * points on the cache filter configuration. */
170
171 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100172 * enabled and if it is after the cache. When the compression is before
173 * the cache, an error is returned. Also check if the cache filter must
174 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100175 list_for_each_entry(f, &px->filter_configs, list) {
176 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100177 /* The compression filter must be evaluated after the cache. */
178 if (comp) {
179 ha_alert("config: %s '%s': unable to enable the compression filter before "
180 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
181 return 1;
182 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100183 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200184 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100185 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200186 else if (f->id == fcgi_flt_id)
187 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100188 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
189 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200190 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100191 * declaration is required. */
192 ha_alert("config: %s '%s': require an explicit filter declaration "
193 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
194 return 1;
195 }
196
Christopher Fauletafd819c2018-12-11 08:57:45 +0100197 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100198 return 0;
199}
200
201static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100202cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100203{
Christopher Faulet65554e12020-03-06 14:52:06 +0100204 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100205
Christopher Faulet65554e12020-03-06 14:52:06 +0100206 st = pool_alloc_dirty(pool_head_cache_st);
207 if (st == NULL)
208 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100209
Christopher Faulet65554e12020-03-06 14:52:06 +0100210 st->first_block = NULL;
211 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100212
Christopher Faulet65554e12020-03-06 14:52:06 +0100213 /* Register post-analyzer on AN_RES_WAIT_HTTP */
214 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100215 return 1;
216}
217
Christopher Faulet65554e12020-03-06 14:52:06 +0100218static void
219cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100220{
221 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100222 struct cache_flt_conf *cconf = FLT_CONF(filter);
223 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100224 struct shared_context *shctx = shctx_ptr(cache);
225
William Lallemand49dc0482017-11-24 14:33:54 +0100226 /* Everything should be released in the http_end filter, but we need to do it
227 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100228 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100229 shctx_lock(shctx);
230 shctx_row_dec_hot(shctx, st->first_block);
231 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100232 }
233 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100234 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100235 filter->ctx = NULL;
236 }
William Lallemand49dc0482017-11-24 14:33:54 +0100237}
238
Christopher Faulet839791a2019-01-07 16:12:07 +0100239static int
240cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
241 unsigned an_bit)
242{
243 struct http_txn *txn = s->txn;
244 struct http_msg *msg = &txn->rsp;
245 struct cache_st *st = filter->ctx;
246
247 if (an_bit != AN_RES_WAIT_HTTP)
248 goto end;
249
250 /* Here we need to check if any compression filter precedes the cache
251 * filter. This is only possible when the compression is configured in
252 * the frontend while the cache filter is configured on the
253 * backend. This case cannot be detected during HAProxy startup. So in
254 * such cases, the cache is disabled.
255 */
256 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
257 pool_free(pool_head_cache_st, st);
258 filter->ctx = NULL;
259 }
260
261 end:
262 return 1;
263}
William Lallemand49dc0482017-11-24 14:33:54 +0100264
265static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100266cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
267{
268 struct cache_st *st = filter->ctx;
269
William Lallemand4da3f8a2017-10-31 14:33:34 +0100270 if (!(msg->chn->flags & CF_ISRESP) || !st)
271 return 1;
272
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200273 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100274 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100275 return 1;
276}
277
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200278static inline void disable_cache_entry(struct cache_st *st,
279 struct filter *filter, struct shared_context *shctx)
280{
281 struct cache_entry *object;
282
283 object = (struct cache_entry *)st->first_block->data;
284 filter->ctx = NULL; /* disable cache */
285 shctx_lock(shctx);
286 shctx_row_dec_hot(shctx, st->first_block);
287 object->eb.key = 0;
288 shctx_unlock(shctx);
289 pool_free(pool_head_cache_st, st);
290}
291
William Lallemand4da3f8a2017-10-31 14:33:34 +0100292static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100293cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
294 unsigned int offset, unsigned int len)
295{
Christopher Faulet95220e22018-12-07 17:34:39 +0100296 struct cache_flt_conf *cconf = FLT_CONF(filter);
297 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100298 struct cache_st *st = filter->ctx;
299 struct htx *htx = htxbuf(&msg->chn->buf);
300 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200301 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100302 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200303 unsigned int orig_len, to_forward;
304 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100305
306 if (!len)
307 return len;
308
309 if (!st->first_block) {
310 unregister_data_filter(s, msg->chn, filter);
311 return len;
312 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100313
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200314 chunk_reset(&trash);
315 orig_len = len;
316 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100317
318 htxret = htx_find_offset(htx, offset);
319 blk = htxret.blk;
320 offset = htxret.ret;
321 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100322 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200323 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100324 struct ist v;
325
326 switch (type) {
327 case HTX_BLK_UNUSED:
328 break;
329
330 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100331 v = htx_get_blk_value(htx, blk);
332 v.ptr += offset;
333 v.len -= offset;
334 if (v.len > len)
335 v.len = len;
336
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200337 info = (type << 28) + v.len;
338 chunk_memcat(&trash, (char *)&info, sizeof(info));
339 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100340 to_forward += v.len;
341 len -= v.len;
342 break;
343
344 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200345 /* Here offset must always be 0 because only
346 * DATA blocks can be partially transferred. */
347 if (offset)
348 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100349 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200350 goto end;
351
352 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
353 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100354 to_forward += sz;
355 len -= sz;
356 break;
357 }
358
359 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100360 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200361
362 end:
363 shctx_lock(shctx);
364 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
365 if (!fb) {
366 shctx_unlock(shctx);
367 goto no_cache;
368 }
369 shctx_unlock(shctx);
370
371 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
372 (unsigned char *)b_head(&trash), b_data(&trash));
373 if (ret < 0)
374 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100375
376 return to_forward;
377
378 no_cache:
379 disable_cache_entry(st, filter, shctx);
380 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200381 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100382}
383
384static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100385cache_store_http_end(struct stream *s, struct filter *filter,
386 struct http_msg *msg)
387{
388 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100389 struct cache_flt_conf *cconf = FLT_CONF(filter);
390 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100391 struct shared_context *shctx = shctx_ptr(cache);
392 struct cache_entry *object;
393
394 if (!(msg->chn->flags & CF_ISRESP))
395 return 1;
396
397 if (st && st->first_block) {
398
399 object = (struct cache_entry *)st->first_block->data;
400
401 /* does not need to test if the insertion worked, if it
402 * doesn't, the blocks will be reused anyway */
403
404 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100405 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
406 object->eb.key = 0;
407 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100408 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100409 shctx_row_dec_hot(shctx, st->first_block);
410 shctx_unlock(shctx);
411
412 }
413 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100414 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100415 filter->ctx = NULL;
416 }
417
418 return 1;
419}
420
421 /*
422 * This intends to be used when checking HTTP headers for some
423 * word=value directive. Return a pointer to the first character of value, if
424 * the word was not found or if there wasn't any value assigned ot it return NULL
425 */
426char *directive_value(const char *sample, int slen, const char *word, int wlen)
427{
428 int st = 0;
429
430 if (slen < wlen)
431 return 0;
432
433 while (wlen) {
434 char c = *sample ^ *word;
435 if (c && c != ('A' ^ 'a'))
436 return NULL;
437 sample++;
438 word++;
439 slen--;
440 wlen--;
441 }
442
443 while (slen) {
444 if (st == 0) {
445 if (*sample != '=')
446 return NULL;
447 sample++;
448 slen--;
449 st = 1;
450 continue;
451 } else {
452 return (char *)sample;
453 }
454 }
455
456 return NULL;
457}
458
459/*
460 * Return the maxage in seconds of an HTTP response.
461 * Compute the maxage using either:
462 * - the assigned max-age of the cache
463 * - the s-maxage directive
464 * - the max-age directive
465 * - (Expires - Data) headers
466 * - the default-max-age of the cache
467 *
468 */
William Lallemand49b44532017-11-24 18:53:43 +0100469int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100470{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200471 struct htx *htx = htxbuf(&s->res.buf);
472 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100473 int smaxage = -1;
474 int maxage = -1;
475
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200476 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
477 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100478
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200479 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
480 if (value) {
481 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100482
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200483 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
484 chunk_strncat(chk, "", 1);
485 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100486 }
487
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200488 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
489 if (value) {
490 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200491
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200492 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
493 chunk_strncat(chk, "", 1);
494 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100495 }
496 }
497
498 /* TODO: Expires - Data */
499
500
501 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100502 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100503
504 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100505 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100506
William Lallemand49b44532017-11-24 18:53:43 +0100507 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100508
509}
510
511
William Lallemanda400a3a2017-11-20 19:13:12 +0100512static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
513{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200514 struct cache_entry *object = (struct cache_entry *)block->data;
515
516 if (first == block && object->eb.key)
517 eb32_delete(&object->eb);
518 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100519}
520
William Lallemand41db4602017-10-30 11:15:51 +0100521/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500522 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100523 * register a filter to store the data
524 */
525enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200526 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100527{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200528 unsigned int age;
529 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100530 struct http_txn *txn = s->txn;
531 struct http_msg *msg = &txn->rsp;
532 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100533 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100534 struct cache_flt_conf *cconf = rule->arg.act.p[0];
535 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100536 struct cache_st *cache_ctx = NULL;
537 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100538 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200539 struct htx *htx;
540 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200541 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200542 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100543
William Lallemand4da3f8a2017-10-31 14:33:34 +0100544 /* Don't cache if the response came from a cache */
545 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
546 s->target == &http_cache_applet.obj_type) {
547 goto out;
548 }
549
550 /* cache only HTTP/1.1 */
551 if (!(txn->req.flags & HTTP_MSGF_VER_11))
552 goto out;
553
Willy Tarreau6905d182019-10-01 17:59:17 +0200554 /* cache only GET method */
555 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100556 goto out;
557
Willy Tarreauc9036c02019-01-11 19:38:25 +0100558 /* cache key was not computed */
559 if (!key)
560 goto out;
561
William Lallemand4da3f8a2017-10-31 14:33:34 +0100562 /* cache only 200 status code */
563 if (txn->status != 200)
564 goto out;
565
Christopher Faulet839791a2019-01-07 16:12:07 +0100566 /* Find the corresponding filter instance for the current stream */
567 list_for_each_entry(filter, &s->strm_flt.filters, list) {
568 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
569 /* No filter ctx, don't cache anything */
570 if (!filter->ctx)
571 goto out;
572 cache_ctx = filter->ctx;
573 break;
574 }
575 }
576
577 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200578 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100579
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200580 /* Do not cache too big objects. */
581 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
582 htx->data + htx->extra > shctx->max_obj_size)
583 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100584
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200585 /* Does not manage Vary at the moment. We will need a secondary key later for that */
586 ctx.blk = NULL;
587 if (http_find_header(htx, ist("Vary"), &ctx, 0))
588 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100589
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200590 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100591
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200592 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
593 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100594
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200595 age = 0;
596 ctx.blk = NULL;
597 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
598 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
599 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
600 hdr_age = CACHE_ENTRY_MAX_AGE;
601 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100602 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200603 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100604 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100605
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200606 chunk_reset(&trash);
607 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
608 struct htx_blk *blk = htx_get_blk(htx, pos);
609 enum htx_blk_type type = htx_get_blk_type(blk);
610 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100611
Christopher Fauletb0667472019-09-03 22:22:12 +0200612 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200613 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
614 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
615 if (type == HTX_BLK_EOH)
616 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200617 }
618
Christopher Fauletb0667472019-09-03 22:22:12 +0200619 /* Do not cache objects if the headers are too big. */
620 if (hdrs_len > htx->size - global.tune.maxrewrite)
621 goto out;
622
William Lallemand4da3f8a2017-10-31 14:33:34 +0100623 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200624 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100625 if (!first) {
626 shctx_unlock(shctx);
627 goto out;
628 }
629 shctx_unlock(shctx);
630
Willy Tarreau1093a452018-04-06 19:02:25 +0200631 /* the received memory is not initialized, we need at least to mark
632 * the object as not indexed yet.
633 */
634 object = (struct cache_entry *)first->data;
635 object->eb.node.leaf_p = NULL;
636 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200637 object->age = age;
Willy Tarreau1093a452018-04-06 19:02:25 +0200638
William Lallemand4da3f8a2017-10-31 14:33:34 +0100639 /* reserve space for the cache_entry structure */
640 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200641 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100642 /* cache the headers in a http action because it allows to chose what
643 * to cache, for example you might want to cache a response before
644 * modifying some HTTP headers, or on the contrary after modifying
645 * those headers.
646 */
647
648 /* does not need to be locked because it's in the "hot" list,
649 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200650 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
651 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100652
653 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100654 if (cache_ctx) {
655 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100656
Willy Tarreauc9036c02019-01-11 19:38:25 +0100657 object->eb.key = key;
658
Christopher Faulet839791a2019-01-07 16:12:07 +0100659 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
660 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100661
Christopher Faulet839791a2019-01-07 16:12:07 +0100662 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100663
Christopher Faulet839791a2019-01-07 16:12:07 +0100664 old = entry_exist(cconf->c.cache, txn->cache_hash);
665 if (old) {
666 eb32_delete(&old->eb);
667 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100668 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100669 shctx_unlock(shctx);
670
671 /* store latest value and expiration time */
672 object->latest_validation = now.tv_sec;
673 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
674 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100675 }
676
677out:
678 /* if does not cache */
679 if (first) {
680 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100681 first->len = 0;
682 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100683 shctx_row_dec_hot(shctx, first);
684 shctx_unlock(shctx);
685 }
686
William Lallemand41db4602017-10-30 11:15:51 +0100687 return ACT_RET_CONT;
688}
689
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100690#define HTX_CACHE_INIT 0 /* Initial state. */
691#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
692#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200693#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
694#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100695
William Lallemandecb73b12017-11-24 14:33:55 +0100696static void http_cache_applet_release(struct appctx *appctx)
697{
Christopher Faulet95220e22018-12-07 17:34:39 +0100698 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100699 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100700 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100701 struct shared_block *first = block_ptr(cache_ptr);
702
703 shctx_lock(shctx_ptr(cache));
704 shctx_row_dec_hot(shctx_ptr(cache), first);
705 shctx_unlock(shctx_ptr(cache));
706}
707
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200708
709static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
710 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100711{
Christopher Faulet95220e22018-12-07 17:34:39 +0100712 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
713 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200714 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200715 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200716 unsigned int max, total;
717 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100718
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200719 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
720 if (!max)
721 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200722 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200723 ? (info & 0xff) + ((info >> 8) & 0xfffff)
724 : info & 0xfffffff);
725 if (blksz > max)
726 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100727
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200728 blk = htx_add_blk(htx, type, blksz);
729 if (!blk)
730 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100731
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200732 blk->info = info;
733 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200734 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200735 while (blksz) {
736 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200737 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200738 offset += max;
739 blksz -= max;
740 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200741 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200742 if (blksz || offset == shctx->block_size) {
743 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
744 offset = 0;
745 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100746 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200747 appctx->ctx.cache.offset = offset;
748 appctx->ctx.cache.next = shblk;
749 appctx->ctx.cache.sent += total;
750 return total;
751}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100752
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200753static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
754 uint32_t info, struct shared_block *shblk, unsigned int offset)
755{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100756
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200757 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
758 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
759 unsigned int max, total, rem_data;
760 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100761
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200762 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
763 if (!max)
764 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100765
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200766 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200767 if (appctx->ctx.cache.rem_data) {
768 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200769 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200770 }
771 else {
772 blksz = (info & 0xfffffff);
773 total = 4;
774 }
775 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200776 rem_data = blksz - max;
777 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100778 }
779
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200780 while (blksz) {
781 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100782
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200783 max = MIN(blksz, shctx->block_size - offset);
784 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
785 offset += sz;
786 blksz -= sz;
787 total += sz;
788 if (sz < max)
789 break;
790 if (blksz || offset == shctx->block_size) {
791 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
792 offset = 0;
793 }
794 }
795
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200796 appctx->ctx.cache.offset = offset;
797 appctx->ctx.cache.next = shblk;
798 appctx->ctx.cache.sent += total;
799 appctx->ctx.cache.rem_data = rem_data + blksz;
800 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100801}
802
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200803static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
804 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100805{
Christopher Faulet95220e22018-12-07 17:34:39 +0100806 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
807 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200808 struct shared_block *shblk;
809 unsigned int offset, sz;
810 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100811
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200812 while (len) {
813 enum htx_blk_type type;
814 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100815
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200816 shblk = appctx->ctx.cache.next;
817 offset = appctx->ctx.cache.offset;
818 if (appctx->ctx.cache.rem_data) {
819 type = HTX_BLK_DATA;
820 info = 0;
821 goto add_data_blk;
822 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100823
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500824 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200825 sz = MIN(4, shctx->block_size - offset);
826 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
827 offset += sz;
828 if (sz < 4) {
829 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
830 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
831 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100832 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200833
834 /* Get payload of the next HTX block and insert it. */
835 type = (info >> 28);
836 if (type != HTX_BLK_DATA)
837 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
838 else {
839 add_data_blk:
840 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100841 }
842
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200843 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100844 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200845 total += ret;
846 len -= ret;
847
848 if (appctx->ctx.cache.rem_data || type == mark)
849 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100850 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100851
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100852 return total;
853}
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200854
855static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
856{
857 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
858 unsigned int age;
859 char *end;
860
861 chunk_reset(&trash);
862 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
863 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
864 age = CACHE_ENTRY_MAX_AGE;
865 end = ultoa_o(age, b_head(&trash), b_size(&trash));
866 b_set_data(&trash, end - b_head(&trash));
867 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
868 return 0;
869 return 1;
870}
871
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200872static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100873{
874 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
875 struct shared_block *first = block_ptr(cache_ptr);
876 struct stream_interface *si = appctx->owner;
877 struct channel *req = si_oc(si);
878 struct channel *res = si_ic(si);
879 struct htx *req_htx, *res_htx;
880 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200881 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100882 size_t ret, total = 0;
883
884 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200885 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100886
887 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
888 goto out;
889
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500890 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100891 if (!b_size(&res->buf)) {
892 si_rx_room_blk(si);
893 goto out;
894 }
895
Willy Tarreauefef3232018-12-16 00:37:45 +0100896 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100897 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100898
899 if (appctx->st0 == HTX_CACHE_INIT) {
900 appctx->ctx.cache.next = block_ptr(cache_ptr);
901 appctx->ctx.cache.offset = sizeof(*cache_ptr);
902 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200903 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100904 appctx->st0 = HTX_CACHE_HEADER;
905 }
906
907 if (appctx->st0 == HTX_CACHE_HEADER) {
908 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200909 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
910 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
911 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
912 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100913 goto error;
914
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200915 /* Skip response body for HEAD requests */
916 if (si_strm(si)->txn->meth == HTTP_METH_HEAD)
Christopher Fauletf0dd0372019-02-25 11:08:34 +0100917 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100918 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200919 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100920 }
921
922 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200923 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
924 if (len) {
925 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
926 if (ret < len) {
927 si_rx_room_blk(si);
928 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100929 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100930 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200931 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100932 }
933
934 if (appctx->st0 == HTX_CACHE_EOM) {
935 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
936 si_rx_room_blk(si);
937 goto out;
938 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100939 appctx->st0 = HTX_CACHE_END;
940 }
941
942 end:
Christopher Fauletadb36312019-02-25 11:40:49 +0100943 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100944 res->flags |= CF_READ_NULL;
945 si_shutr(si);
946 }
947
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100948 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200949 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +0100950 if (total)
951 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100952 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +0100953
954 /* eat the whole request */
955 if (co_data(req)) {
956 req_htx = htx_from_buf(&req->buf);
957 co_htx_skip(req, req_htx, co_data(req));
958 htx_to_buf(req_htx, &req->buf);
959 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100960 return;
961
962 error:
963 /* Sent and HTTP error 500 */
964 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +0200965 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100966 res->buf.data = b_data(errmsg);
967 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
968 res_htx = htx_from_buf(&res->buf);
969
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200970 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100971 appctx->st0 = HTX_CACHE_END;
972 goto end;
973}
974
975
Christopher Faulet95220e22018-12-07 17:34:39 +0100976static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +0100977{
978 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +0100979 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +0100980
Christopher Faulet95220e22018-12-07 17:34:39 +0100981 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +0100982 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +0100983 goto err;
William Lallemand41db4602017-10-30 11:15:51 +0100984 }
985
986 /* check if a cache filter was already registered with this cache
987 * name, if that's the case, must use it. */
988 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +0100989 if (fconf->id == cache_store_flt_id) {
990 cconf = fconf->conf;
991 if (cconf && !strcmp((char *)cconf->c.name, name)) {
992 rule->arg.act.p[0] = cconf;
993 return 1;
994 }
William Lallemand41db4602017-10-30 11:15:51 +0100995 }
996 }
997
Christopher Faulet95220e22018-12-07 17:34:39 +0100998 /* Create the filter cache config */
999 cconf = calloc(1, sizeof(*cconf));
1000 if (!cconf) {
1001 memprintf(err, "out of memory\n");
1002 goto err;
1003 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001004 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001005 cconf->c.name = strdup(name);
1006 if (!cconf->c.name) {
1007 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001008 goto err;
1009 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001010
William Lallemand41db4602017-10-30 11:15:51 +01001011 /* register a filter to fill the cache buffer */
1012 fconf = calloc(1, sizeof(*fconf));
1013 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001014 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001015 goto err;
1016 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001017 fconf->id = cache_store_flt_id;
1018 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001019 fconf->ops = &cache_ops;
1020 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1021
Christopher Faulet95220e22018-12-07 17:34:39 +01001022 rule->arg.act.p[0] = cconf;
1023 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001024
Christopher Faulet95220e22018-12-07 17:34:39 +01001025 err:
1026 free(cconf);
1027 return 0;
1028}
1029
1030enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1031 struct act_rule *rule, char **err)
1032{
1033 rule->action = ACT_CUSTOM;
1034 rule->action_ptr = http_action_store_cache;
1035
1036 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1037 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001038
Christopher Faulet95220e22018-12-07 17:34:39 +01001039 (*orig_arg)++;
1040 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001041}
1042
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001043/* This produces a sha1 hash of the concatenation of the HTTP method,
1044 * the first occurrence of the Host header followed by the path component
1045 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001046int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001047{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001048 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001049 struct htx *htx = htxbuf(&s->req.buf);
1050 struct htx_sl *sl;
1051 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001052 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001053 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001054 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001055
William Lallemandf528fff2017-11-23 19:43:17 +01001056 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001057 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001058
1059 switch (txn->meth) {
1060 case HTTP_METH_HEAD:
1061 case HTTP_METH_GET:
1062 chunk_memcat(trash, "GET", 3);
1063 break;
1064 default:
1065 return 0;
1066 }
1067
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001068 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001069 uri = htx_sl_req_uri(sl); // whole uri
1070 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001071 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001072
1073 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1074 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1075 * URIs are almost always sent in absolute form with their scheme. In
1076 * this case, the scheme is almost always "https". In order to support
1077 * sharing of cache objects between H1 and H2, we'll hash the absolute
1078 * URI whenever known, or prepend "https://" + the Host header for
1079 * relative URIs. The difference will only appear on absolute HTTP/1
1080 * requests sent to an origin server, which practically is never met in
1081 * the real world so we don't care about the ability to share the same
1082 * key here.URIs are normalized from the absolute URI to an origin form as
1083 * well.
1084 */
1085 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001086 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001087 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1088 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001089 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001090 }
1091
1092 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001093
1094 /* hash everything */
1095 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001096 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001097 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1098
1099 return 1;
1100}
1101
William Lallemand41db4602017-10-30 11:15:51 +01001102enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1103 struct session *sess, struct stream *s, int flags)
1104{
William Lallemand77c11972017-10-31 20:43:01 +01001105
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001106 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001107 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001108 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1109 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001110
Willy Tarreau6905d182019-10-01 17:59:17 +02001111 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1112 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001113 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001114 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001115 txn->flags |= TX_CACHE_IGNORE;
1116
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001117 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001118
Willy Tarreau504455c2017-12-22 17:47:35 +01001119 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1120 return ACT_RET_CONT;
1121
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001122 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001123 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001124
Willy Tarreau504455c2017-12-22 17:47:35 +01001125 if (s->txn->flags & TX_CACHE_IGNORE)
1126 return ACT_RET_CONT;
1127
Willy Tarreaua1214a52018-12-14 14:00:25 +01001128 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001129 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001130 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001131 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001132
William Lallemanda400a3a2017-11-20 19:13:12 +01001133 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001134 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001135 if (res) {
1136 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001137 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1138 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001139 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001140 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001141 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001142 appctx->rule = rule;
1143 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001144 appctx->ctx.cache.next = NULL;
1145 appctx->ctx.cache.sent = 0;
Willy Tarreaua1214a52018-12-14 14:00:25 +01001146
1147 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001148 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001149 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001150 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001151 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001152 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001153 shctx_lock(shctx_ptr(cache));
1154 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1155 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001156 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001157 }
1158 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001159 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001160 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001161}
1162
1163
1164enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1165 struct act_rule *rule, char **err)
1166{
William Lallemand41db4602017-10-30 11:15:51 +01001167 rule->action = ACT_CUSTOM;
1168 rule->action_ptr = http_action_req_cache_use;
1169
Christopher Faulet95220e22018-12-07 17:34:39 +01001170 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001171 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001172
1173 (*orig_arg)++;
1174 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001175}
1176
1177int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1178{
1179 int err_code = 0;
1180
1181 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1182
1183 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001184 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1185 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001186 err_code |= ERR_ALERT | ERR_ABORT;
1187 goto out;
1188 }
1189
1190 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1191 err_code |= ERR_ABORT;
1192 goto out;
1193 }
1194
1195 if (tmp_cache_config == NULL) {
1196 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1197 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001198 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001199 err_code |= ERR_ALERT | ERR_ABORT;
1200 goto out;
1201 }
1202
1203 strlcpy2(tmp_cache_config->id, args[1], 33);
1204 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001205 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1206 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001207 err_code |= ERR_WARN;
1208 }
William Lallemand49b44532017-11-24 18:53:43 +01001209 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001210 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001211 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001212 }
1213 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001214 unsigned long int maxsize;
1215 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001216
1217 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1218 err_code |= ERR_ABORT;
1219 goto out;
1220 }
1221
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001222 maxsize = strtoul(args[1], &err, 10);
1223 if (err == args[1] || *err != '\0') {
1224 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1225 file, linenum, args[1]);
1226 err_code |= ERR_ABORT;
1227 goto out;
1228 }
1229
1230 if (maxsize > (UINT_MAX >> 20)) {
1231 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1232 file, linenum, args[1], UINT_MAX >> 20);
1233 err_code |= ERR_ABORT;
1234 goto out;
1235 }
1236
William Lallemand41db4602017-10-30 11:15:51 +01001237 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001238 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001239 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001240 } else if (strcmp(args[0], "max-age") == 0) {
1241 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1242 err_code |= ERR_ABORT;
1243 goto out;
1244 }
1245
1246 if (!*args[1]) {
1247 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1248 file, linenum, args[0]);
1249 err_code |= ERR_WARN;
1250 }
1251
1252 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001253 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001254 unsigned int maxobjsz;
1255 char *err;
1256
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001257 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1258 err_code |= ERR_ABORT;
1259 goto out;
1260 }
1261
1262 if (!*args[1]) {
1263 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1264 file, linenum, args[0]);
1265 err_code |= ERR_WARN;
1266 }
1267
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001268 maxobjsz = strtoul(args[1], &err, 10);
1269 if (err == args[1] || *err != '\0') {
1270 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1271 file, linenum, args[1]);
1272 err_code |= ERR_ABORT;
1273 goto out;
1274 }
1275 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001276 }
1277 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001278 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001279 err_code |= ERR_ALERT | ERR_FATAL;
1280 goto out;
1281 }
1282out:
1283 return err_code;
1284}
1285
1286/* once the cache section is parsed */
1287
1288int cfg_post_parse_section_cache()
1289{
William Lallemand41db4602017-10-30 11:15:51 +01001290 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001291
1292 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001293
1294 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001295 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001296 err_code |= ERR_FATAL | ERR_ALERT;
1297 goto out;
1298 }
1299
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001300 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001301 /* Default max. file size is a 256th of the cache size. */
1302 tmp_cache_config->maxobjsz =
1303 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001304 }
1305 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1306 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1307 err_code |= ERR_FATAL | ERR_ALERT;
1308 goto out;
1309 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001310
William Lallemandd1d1e222019-08-28 15:22:49 +02001311 /* add to the list of cache to init and reinit tmp_cache_config
1312 * for next cache section, if any.
1313 */
1314 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1315 tmp_cache_config = NULL;
1316 return err_code;
1317 }
1318out:
1319 free(tmp_cache_config);
1320 tmp_cache_config = NULL;
1321 return err_code;
1322
1323}
1324
1325int post_check_cache()
1326{
1327 struct proxy *px;
1328 struct cache *back, *cache_config, *cache;
1329 struct shared_context *shctx;
1330 int ret_shctx;
1331 int err_code = 0;
1332
1333 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1334
1335 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1336 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001337
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001338 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001339 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001340 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001341 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001342 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001343
1344 err_code |= ERR_FATAL | ERR_ALERT;
1345 goto out;
1346 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001347 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001348 /* the cache structure is stored in the shctx and added to the
1349 * caches list, we can remove the entry from the caches_config
1350 * list */
1351 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001352 cache = (struct cache *)shctx->data;
1353 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001354 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001355 LIST_DEL(&cache_config->list);
1356 free(cache_config);
1357
1358 /* Find all references for this cache in the existing filters
1359 * (over all proxies) and reference it in matching filters.
1360 */
1361 for (px = proxies_list; px; px = px->next) {
1362 struct flt_conf *fconf;
1363 struct cache_flt_conf *cconf;
1364
1365 list_for_each_entry(fconf, &px->filter_configs, list) {
1366 if (fconf->id != cache_store_flt_id)
1367 continue;
1368
1369 cconf = fconf->conf;
1370 if (!strcmp(cache->id, cconf->c.name)) {
1371 free(cconf->c.name);
1372 cconf->c.cache = cache;
1373 break;
1374 }
1375 }
1376 }
William Lallemand41db4602017-10-30 11:15:51 +01001377 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001378
William Lallemand41db4602017-10-30 11:15:51 +01001379out:
William Lallemand41db4602017-10-30 11:15:51 +01001380 return err_code;
1381
William Lallemand41db4602017-10-30 11:15:51 +01001382}
1383
William Lallemand41db4602017-10-30 11:15:51 +01001384struct flt_ops cache_ops = {
1385 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001386 .check = cache_store_check,
1387 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001388
Christopher Faulet65554e12020-03-06 14:52:06 +01001389 /* Handle stream init/deinit */
1390 .attach = cache_store_strm_init,
1391 .detach = cache_store_strm_deinit,
1392
William Lallemand4da3f8a2017-10-31 14:33:34 +01001393 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001394 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001395
1396 /* Filter HTTP requests and responses */
1397 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001398 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001399 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001400};
1401
Christopher Faulet99a17a22018-12-11 09:18:27 +01001402
1403
1404static int
1405parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1406 struct flt_conf *fconf, char **err, void *private)
1407{
1408 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001409 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001410 char *name = NULL;
1411 int pos = *cur_arg;
1412
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001413 /* Get the cache filter name. <pos> point on "cache" keyword */
1414 if (!*args[pos + 1]) {
1415 memprintf(err, "%s : expects an <id> argument", args[pos]);
1416 goto error;
1417 }
1418 name = strdup(args[pos + 1]);
1419 if (!name) {
1420 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1421 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001422 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001423 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001424
1425 /* Check if an implicit filter with the same name already exists. If so,
1426 * we remove the implicit filter to use the explicit one. */
1427 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1428 if (f->id != cache_store_flt_id)
1429 continue;
1430
1431 cconf = f->conf;
1432 if (strcmp(name, cconf->c.name)) {
1433 cconf = NULL;
1434 continue;
1435 }
1436
1437 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1438 cconf = NULL;
1439 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1440 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01001441 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001442 }
1443
1444 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1445 LIST_DEL(&f->list);
1446 free(f);
1447 free(name);
1448 break;
1449 }
1450
1451 /* No implicit cache filter found, create configuration for the explicit one */
1452 if (!cconf) {
1453 cconf = calloc(1, sizeof(*cconf));
1454 if (!cconf) {
1455 memprintf(err, "%s: out of memory", args[*cur_arg]);
1456 goto error;
1457 }
1458 cconf->c.name = name;
1459 }
1460
1461 cconf->flags = 0;
1462 fconf->id = cache_store_flt_id;
1463 fconf->conf = cconf;
1464 fconf->ops = &cache_ops;
1465
1466 *cur_arg = pos;
1467 return 0;
1468
1469 error:
1470 free(name);
1471 free(cconf);
1472 return -1;
1473}
1474
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001475static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001476{
1477 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1478 return 1;
1479
1480 return 0;
1481}
1482
1483static int cli_io_handler_show_cache(struct appctx *appctx)
1484{
1485 struct cache* cache = appctx->ctx.cli.p0;
1486 struct stream_interface *si = appctx->owner;
1487
William Lallemand1f49a362017-11-21 20:01:26 +01001488 if (cache == NULL) {
1489 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1490 }
1491
1492 list_for_each_entry_from(cache, &caches, list) {
1493 struct eb32_node *node = NULL;
1494 unsigned int next_key;
1495 struct cache_entry *entry;
1496
William Lallemand1f49a362017-11-21 20:01:26 +01001497 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001498 if (!next_key) {
1499 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1500 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001501 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001502 return 0;
1503 }
1504 }
William Lallemand1f49a362017-11-21 20:01:26 +01001505
1506 appctx->ctx.cli.p0 = cache;
1507
1508 while (1) {
1509
1510 shctx_lock(shctx_ptr(cache));
1511 node = eb32_lookup_ge(&cache->entries, next_key);
1512 if (!node) {
1513 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001514 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001515 break;
1516 }
1517
1518 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01001519 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 +01001520
1521 next_key = node->key + 1;
1522 appctx->ctx.cli.i0 = next_key;
1523
1524 shctx_unlock(shctx_ptr(cache));
1525
1526 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001527 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001528 return 0;
1529 }
1530 }
1531
1532 }
1533
1534 return 1;
1535
1536}
1537
Christopher Faulet99a17a22018-12-11 09:18:27 +01001538/* Declare the filter parser for "cache" keyword */
1539static struct flt_kw_list filter_kws = { "CACHE", { }, {
1540 { "cache", parse_cache_flt, NULL },
1541 { NULL, NULL, NULL },
1542 }
1543};
1544
1545INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1546
William Lallemand1f49a362017-11-21 20:01:26 +01001547static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001548 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1549 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001550}};
1551
Willy Tarreau0108d902018-11-25 19:14:37 +01001552INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001553
William Lallemand41db4602017-10-30 11:15:51 +01001554static struct action_kw_list http_res_actions = {
1555 .kw = {
1556 { "cache-store", parse_cache_store },
1557 { NULL, NULL }
1558 }
1559};
1560
Willy Tarreau0108d902018-11-25 19:14:37 +01001561INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1562
William Lallemand41db4602017-10-30 11:15:51 +01001563static struct action_kw_list http_req_actions = {
1564 .kw = {
1565 { "cache-use", parse_cache_use },
1566 { NULL, NULL }
1567 }
1568};
1569
Willy Tarreau0108d902018-11-25 19:14:37 +01001570INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1571
Willy Tarreau2231b632019-03-29 18:26:52 +01001572struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001573 .obj_type = OBJ_TYPE_APPLET,
1574 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001575 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001576 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001577};
1578
Willy Tarreaue6552512018-11-26 11:33:13 +01001579/* config parsers for this section */
1580REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02001581REGISTER_POST_CHECK(post_check_cache);