blob: 1fd8dc654d9fdfc446af7d123e98e393b56244fa [file] [log] [blame]
William Lallemand41db4602017-10-30 11:15:51 +01001/*
2 * Cache management
3 *
4 * Copyright 2017 HAProxy Technologies
5 * William Lallemand <wlallemand@haproxy.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
William Lallemand41db4602017-10-30 11:15:51 +010013#include <eb32tree.h>
William Lallemandf528fff2017-11-23 19:43:17 +010014#include <import/sha1.h>
William Lallemand41db4602017-10-30 11:15:51 +010015
William Lallemand75d93292017-11-21 20:01:24 +010016#include <types/action.h>
William Lallemand1f49a362017-11-21 20:01:26 +010017#include <types/cli.h>
William Lallemand75d93292017-11-21 20:01:24 +010018#include <types/filters.h>
19#include <types/proxy.h>
20#include <types/shctx.h>
21
William Lallemand41db4602017-10-30 11:15:51 +010022#include <proto/channel.h>
William Lallemand1f49a362017-11-21 20:01:26 +010023#include <proto/cli.h>
William Lallemand41db4602017-10-30 11:15:51 +010024#include <proto/proxy.h>
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010025#include <proto/http_htx.h>
William Lallemand41db4602017-10-30 11:15:51 +010026#include <proto/filters.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020027#include <proto/http_rules.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020028#include <proto/http_ana.h>
William Lallemand41db4602017-10-30 11:15:51 +010029#include <proto/log.h>
30#include <proto/stream.h>
31#include <proto/stream_interface.h>
32#include <proto/shctx.h>
33
William Lallemand41db4602017-10-30 11:15:51 +010034
35#include <common/cfgparse.h>
36#include <common/hash.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010037#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010038#include <common/initcall.h>
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);
88static struct cache *tmp_cache_config = NULL;
89
Willy Tarreau8ceae722018-11-26 11:58:30 +010090DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
91
William Lallemandf528fff2017-11-23 19:43:17 +010092struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010093{
94 struct eb32_node *node;
95 struct cache_entry *entry;
96
William Lallemandf528fff2017-11-23 19:43:17 +010097 node = eb32_lookup(&cache->entries, (*(unsigned int *)hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010098 if (!node)
99 return NULL;
100
101 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100102
103 /* if that's not the right node */
104 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
105 return NULL;
106
William Lallemand08727662017-11-21 20:01:27 +0100107 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100108 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100109 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100110 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100111 entry->eb.key = 0;
112 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100113 return NULL;
114
115}
116
117static inline struct shared_context *shctx_ptr(struct cache *cache)
118{
119 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
120}
121
William Lallemand77c11972017-10-31 20:43:01 +0100122static inline struct shared_block *block_ptr(struct cache_entry *entry)
123{
124 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
125}
126
127
128
William Lallemand41db4602017-10-30 11:15:51 +0100129static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100130cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100131{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100132 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100133 return 0;
134}
135
Christopher Faulet95220e22018-12-07 17:34:39 +0100136static void
137cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
138{
139 struct cache_flt_conf *cconf = fconf->conf;
140
141 free(cconf);
142}
143
William Lallemand4da3f8a2017-10-31 14:33:34 +0100144static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100145cache_store_check(struct proxy *px, struct flt_conf *fconf)
146{
147 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100148 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100149 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100150 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100151
152 /* resolve the cache name to a ptr in the filter config */
153 list_for_each_entry(cache, &caches, list) {
154 if (!strcmp(cache->id, cconf->c.name)) {
Christopher Faulet95220e22018-12-07 17:34:39 +0100155 free(cconf->c.name);
156 cconf->c.cache = cache;
157 goto found;
158 }
159 }
160
161 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
162 proxy_type_str(px), px->id, (char *)cconf->c.name);
163 return 1;
164
165 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100166 /* Here <cache> points on the cache the filter must use and <cconf>
167 * points on the cache filter configuration. */
168
169 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100170 * enabled and if it is after the cache. When the compression is before
171 * the cache, an error is returned. Also check if the cache filter must
172 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100173 list_for_each_entry(f, &px->filter_configs, list) {
174 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100175 /* The compression filter must be evaluated after the cache. */
176 if (comp) {
177 ha_alert("config: %s '%s': unable to enable the compression filter before "
178 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
179 return 1;
180 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100181 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200182 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100183 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200184 else if (f->id == fcgi_flt_id)
185 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100186 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
187 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200188 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100189 * declaration is required. */
190 ha_alert("config: %s '%s': require an explicit filter declaration "
191 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
192 return 1;
193 }
194
Christopher Fauletafd819c2018-12-11 08:57:45 +0100195 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100196 return 0;
197}
198
199static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100200cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
201{
202 if (!(chn->flags & CF_ISRESP))
203 return 1;
204
205 if (filter->ctx == NULL) {
206 struct cache_st *st;
207
Willy Tarreaubafbe012017-11-24 17:34:44 +0100208 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100209 if (st == NULL)
210 return -1;
211
William Lallemand4da3f8a2017-10-31 14:33:34 +0100212 st->first_block = NULL;
213 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100214
215 /* Register post-analyzer on AN_RES_WAIT_HTTP */
216 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100217 }
218
William Lallemand4da3f8a2017-10-31 14:33:34 +0100219 return 1;
220}
221
222static int
William Lallemand49dc0482017-11-24 14:33:54 +0100223cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
224{
225 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100226 struct cache_flt_conf *cconf = FLT_CONF(filter);
227 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100228 struct shared_context *shctx = shctx_ptr(cache);
229
230 if (!(chn->flags & CF_ISRESP))
231 return 1;
232
233 /* Everything should be released in the http_end filter, but we need to do it
234 * there too, in case of errors */
235
236 if (st && st->first_block) {
237
238 shctx_lock(shctx);
239 shctx_row_dec_hot(shctx, st->first_block);
240 shctx_unlock(shctx);
241
242 }
243 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100244 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100245 filter->ctx = NULL;
246 }
247
248 return 1;
249}
250
Christopher Faulet839791a2019-01-07 16:12:07 +0100251static int
252cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
253 unsigned an_bit)
254{
255 struct http_txn *txn = s->txn;
256 struct http_msg *msg = &txn->rsp;
257 struct cache_st *st = filter->ctx;
258
259 if (an_bit != AN_RES_WAIT_HTTP)
260 goto end;
261
262 /* Here we need to check if any compression filter precedes the cache
263 * filter. This is only possible when the compression is configured in
264 * the frontend while the cache filter is configured on the
265 * backend. This case cannot be detected during HAProxy startup. So in
266 * such cases, the cache is disabled.
267 */
268 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
269 pool_free(pool_head_cache_st, st);
270 filter->ctx = NULL;
271 }
272
273 end:
274 return 1;
275}
William Lallemand49dc0482017-11-24 14:33:54 +0100276
277static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100278cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
279{
280 struct cache_st *st = filter->ctx;
281
William Lallemand4da3f8a2017-10-31 14:33:34 +0100282 if (!(msg->chn->flags & CF_ISRESP) || !st)
283 return 1;
284
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200285 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100286 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100287 return 1;
288}
289
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200290static inline void disable_cache_entry(struct cache_st *st,
291 struct filter *filter, struct shared_context *shctx)
292{
293 struct cache_entry *object;
294
295 object = (struct cache_entry *)st->first_block->data;
296 filter->ctx = NULL; /* disable cache */
297 shctx_lock(shctx);
298 shctx_row_dec_hot(shctx, st->first_block);
299 object->eb.key = 0;
300 shctx_unlock(shctx);
301 pool_free(pool_head_cache_st, st);
302}
303
William Lallemand4da3f8a2017-10-31 14:33:34 +0100304static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100305cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
306 unsigned int offset, unsigned int len)
307{
Christopher Faulet95220e22018-12-07 17:34:39 +0100308 struct cache_flt_conf *cconf = FLT_CONF(filter);
309 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100310 struct cache_st *st = filter->ctx;
311 struct htx *htx = htxbuf(&msg->chn->buf);
312 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200313 struct shared_block *fb;
314 unsigned int orig_len, to_forward;
315 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100316
317 if (!len)
318 return len;
319
320 if (!st->first_block) {
321 unregister_data_filter(s, msg->chn, filter);
322 return len;
323 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100324
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200325 chunk_reset(&trash);
326 orig_len = len;
327 to_forward = 0;
Christopher Fauletee847d42019-05-23 11:55:33 +0200328 for (blk = htx_get_first_blk(htx); blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100329 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200330 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100331 struct ist v;
332
Christopher Fauletee847d42019-05-23 11:55:33 +0200333 if (offset >= sz) {
334 offset -= sz;
335 continue;
336 }
337
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100338 switch (type) {
339 case HTX_BLK_UNUSED:
340 break;
341
342 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100343 v = htx_get_blk_value(htx, blk);
344 v.ptr += offset;
345 v.len -= offset;
346 if (v.len > len)
347 v.len = len;
348
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200349 info = (type << 28) + v.len;
350 chunk_memcat(&trash, (char *)&info, sizeof(info));
351 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100352 to_forward += v.len;
353 len -= v.len;
354 break;
355
356 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200357 /* Here offset must always be 0 because only
358 * DATA blocks can be partially transferred. */
359 if (offset)
360 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100361 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200362 goto end;
363
364 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
365 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100366 to_forward += sz;
367 len -= sz;
368 break;
369 }
370
371 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100372 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200373
374 end:
375 shctx_lock(shctx);
376 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
377 if (!fb) {
378 shctx_unlock(shctx);
379 goto no_cache;
380 }
381 shctx_unlock(shctx);
382
383 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
384 (unsigned char *)b_head(&trash), b_data(&trash));
385 if (ret < 0)
386 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100387
388 return to_forward;
389
390 no_cache:
391 disable_cache_entry(st, filter, shctx);
392 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200393 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100394}
395
396static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100397cache_store_http_end(struct stream *s, struct filter *filter,
398 struct http_msg *msg)
399{
400 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100401 struct cache_flt_conf *cconf = FLT_CONF(filter);
402 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100403 struct shared_context *shctx = shctx_ptr(cache);
404 struct cache_entry *object;
405
406 if (!(msg->chn->flags & CF_ISRESP))
407 return 1;
408
409 if (st && st->first_block) {
410
411 object = (struct cache_entry *)st->first_block->data;
412
413 /* does not need to test if the insertion worked, if it
414 * doesn't, the blocks will be reused anyway */
415
416 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100417 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
418 object->eb.key = 0;
419 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100420 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100421 shctx_row_dec_hot(shctx, st->first_block);
422 shctx_unlock(shctx);
423
424 }
425 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100426 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100427 filter->ctx = NULL;
428 }
429
430 return 1;
431}
432
433 /*
434 * This intends to be used when checking HTTP headers for some
435 * word=value directive. Return a pointer to the first character of value, if
436 * the word was not found or if there wasn't any value assigned ot it return NULL
437 */
438char *directive_value(const char *sample, int slen, const char *word, int wlen)
439{
440 int st = 0;
441
442 if (slen < wlen)
443 return 0;
444
445 while (wlen) {
446 char c = *sample ^ *word;
447 if (c && c != ('A' ^ 'a'))
448 return NULL;
449 sample++;
450 word++;
451 slen--;
452 wlen--;
453 }
454
455 while (slen) {
456 if (st == 0) {
457 if (*sample != '=')
458 return NULL;
459 sample++;
460 slen--;
461 st = 1;
462 continue;
463 } else {
464 return (char *)sample;
465 }
466 }
467
468 return NULL;
469}
470
471/*
472 * Return the maxage in seconds of an HTTP response.
473 * Compute the maxage using either:
474 * - the assigned max-age of the cache
475 * - the s-maxage directive
476 * - the max-age directive
477 * - (Expires - Data) headers
478 * - the default-max-age of the cache
479 *
480 */
William Lallemand49b44532017-11-24 18:53:43 +0100481int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100482{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200483 struct htx *htx = htxbuf(&s->res.buf);
484 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100485 int smaxage = -1;
486 int maxage = -1;
487
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200488 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
489 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100490
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200491 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
492 if (value) {
493 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100494
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200495 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
496 chunk_strncat(chk, "", 1);
497 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100498 }
499
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200500 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
501 if (value) {
502 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200503
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200504 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
505 chunk_strncat(chk, "", 1);
506 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100507 }
508 }
509
510 /* TODO: Expires - Data */
511
512
513 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100514 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100515
516 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100517 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100518
William Lallemand49b44532017-11-24 18:53:43 +0100519 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100520
521}
522
523
William Lallemanda400a3a2017-11-20 19:13:12 +0100524static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
525{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200526 struct cache_entry *object = (struct cache_entry *)block->data;
527
528 if (first == block && object->eb.key)
529 eb32_delete(&object->eb);
530 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100531}
532
William Lallemand41db4602017-10-30 11:15:51 +0100533/*
534 * This fonction will store the headers of the response in a buffer and then
535 * register a filter to store the data
536 */
537enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200538 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100539{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200540 unsigned int age;
541 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100542 struct http_txn *txn = s->txn;
543 struct http_msg *msg = &txn->rsp;
544 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100545 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100546 struct cache_flt_conf *cconf = rule->arg.act.p[0];
547 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100548 struct cache_st *cache_ctx = NULL;
549 struct cache_entry *object, *old;
Willy Tarreauc9036c02019-01-11 19:38:25 +0100550 unsigned int key = *(unsigned int *)txn->cache_hash;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200551 struct htx *htx;
552 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200553 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200554 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100555
William Lallemand4da3f8a2017-10-31 14:33:34 +0100556 /* Don't cache if the response came from a cache */
557 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
558 s->target == &http_cache_applet.obj_type) {
559 goto out;
560 }
561
562 /* cache only HTTP/1.1 */
563 if (!(txn->req.flags & HTTP_MSGF_VER_11))
564 goto out;
565
Willy Tarreau6905d182019-10-01 17:59:17 +0200566 /* cache only GET method */
567 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100568 goto out;
569
Willy Tarreauc9036c02019-01-11 19:38:25 +0100570 /* cache key was not computed */
571 if (!key)
572 goto out;
573
William Lallemand4da3f8a2017-10-31 14:33:34 +0100574 /* cache only 200 status code */
575 if (txn->status != 200)
576 goto out;
577
Christopher Faulet839791a2019-01-07 16:12:07 +0100578 /* Find the corresponding filter instance for the current stream */
579 list_for_each_entry(filter, &s->strm_flt.filters, list) {
580 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
581 /* No filter ctx, don't cache anything */
582 if (!filter->ctx)
583 goto out;
584 cache_ctx = filter->ctx;
585 break;
586 }
587 }
588
589 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200590 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100591
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200592 /* Do not cache too big objects. */
593 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
594 htx->data + htx->extra > shctx->max_obj_size)
595 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100596
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200597 /* Does not manage Vary at the moment. We will need a secondary key later for that */
598 ctx.blk = NULL;
599 if (http_find_header(htx, ist("Vary"), &ctx, 0))
600 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100601
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200602 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100603
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200604 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
605 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100606
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200607 age = 0;
608 ctx.blk = NULL;
609 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
610 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
611 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
612 hdr_age = CACHE_ENTRY_MAX_AGE;
613 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100614 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200615 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100616 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100617
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200618 chunk_reset(&trash);
619 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
620 struct htx_blk *blk = htx_get_blk(htx, pos);
621 enum htx_blk_type type = htx_get_blk_type(blk);
622 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100623
Christopher Fauletb0667472019-09-03 22:22:12 +0200624 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200625 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
626 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
627 if (type == HTX_BLK_EOH)
628 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200629 }
630
Christopher Fauletb0667472019-09-03 22:22:12 +0200631 /* Do not cache objects if the headers are too big. */
632 if (hdrs_len > htx->size - global.tune.maxrewrite)
633 goto out;
634
William Lallemand4da3f8a2017-10-31 14:33:34 +0100635 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200636 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100637 if (!first) {
638 shctx_unlock(shctx);
639 goto out;
640 }
641 shctx_unlock(shctx);
642
Willy Tarreau1093a452018-04-06 19:02:25 +0200643 /* the received memory is not initialized, we need at least to mark
644 * the object as not indexed yet.
645 */
646 object = (struct cache_entry *)first->data;
647 object->eb.node.leaf_p = NULL;
648 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200649 object->age = age;
Willy Tarreau1093a452018-04-06 19:02:25 +0200650
William Lallemand4da3f8a2017-10-31 14:33:34 +0100651 /* reserve space for the cache_entry structure */
652 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200653 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100654 /* cache the headers in a http action because it allows to chose what
655 * to cache, for example you might want to cache a response before
656 * modifying some HTTP headers, or on the contrary after modifying
657 * those headers.
658 */
659
660 /* does not need to be locked because it's in the "hot" list,
661 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200662 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
663 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100664
665 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100666 if (cache_ctx) {
667 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100668
Willy Tarreauc9036c02019-01-11 19:38:25 +0100669 object->eb.key = key;
670
Christopher Faulet839791a2019-01-07 16:12:07 +0100671 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
672 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100673
Christopher Faulet839791a2019-01-07 16:12:07 +0100674 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100675
Christopher Faulet839791a2019-01-07 16:12:07 +0100676 old = entry_exist(cconf->c.cache, txn->cache_hash);
677 if (old) {
678 eb32_delete(&old->eb);
679 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100680 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100681 shctx_unlock(shctx);
682
683 /* store latest value and expiration time */
684 object->latest_validation = now.tv_sec;
685 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
686 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100687 }
688
689out:
690 /* if does not cache */
691 if (first) {
692 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100693 first->len = 0;
694 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100695 shctx_row_dec_hot(shctx, first);
696 shctx_unlock(shctx);
697 }
698
William Lallemand41db4602017-10-30 11:15:51 +0100699 return ACT_RET_CONT;
700}
701
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100702#define HTX_CACHE_INIT 0 /* Initial state. */
703#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
704#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200705#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
706#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100707
William Lallemandecb73b12017-11-24 14:33:55 +0100708static void http_cache_applet_release(struct appctx *appctx)
709{
Christopher Faulet95220e22018-12-07 17:34:39 +0100710 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100711 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100712 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100713 struct shared_block *first = block_ptr(cache_ptr);
714
715 shctx_lock(shctx_ptr(cache));
716 shctx_row_dec_hot(shctx_ptr(cache), first);
717 shctx_unlock(shctx_ptr(cache));
718}
719
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200720
721static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
722 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100723{
Christopher Faulet95220e22018-12-07 17:34:39 +0100724 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
725 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200726 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200727 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200728 unsigned int max, total;
729 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100730
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200731 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
732 if (!max)
733 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200734 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200735 ? (info & 0xff) + ((info >> 8) & 0xfffff)
736 : info & 0xfffffff);
737 if (blksz > max)
738 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100739
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200740 blk = htx_add_blk(htx, type, blksz);
741 if (!blk)
742 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100743
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200744 blk->info = info;
745 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200746 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200747 while (blksz) {
748 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200749 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200750 offset += max;
751 blksz -= max;
752 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200753 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200754 if (blksz || offset == shctx->block_size) {
755 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
756 offset = 0;
757 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100758 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200759 appctx->ctx.cache.offset = offset;
760 appctx->ctx.cache.next = shblk;
761 appctx->ctx.cache.sent += total;
762 return total;
763}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100764
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200765static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
766 uint32_t info, struct shared_block *shblk, unsigned int offset)
767{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100768
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200769 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
770 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
771 unsigned int max, total, rem_data;
772 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100773
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200774 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
775 if (!max)
776 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100777
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200778 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200779 if (appctx->ctx.cache.rem_data) {
780 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200781 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200782 }
783 else {
784 blksz = (info & 0xfffffff);
785 total = 4;
786 }
787 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200788 rem_data = blksz - max;
789 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100790 }
791
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200792 while (blksz) {
793 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100794
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200795 max = MIN(blksz, shctx->block_size - offset);
796 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
797 offset += sz;
798 blksz -= sz;
799 total += sz;
800 if (sz < max)
801 break;
802 if (blksz || offset == shctx->block_size) {
803 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
804 offset = 0;
805 }
806 }
807
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200808 appctx->ctx.cache.offset = offset;
809 appctx->ctx.cache.next = shblk;
810 appctx->ctx.cache.sent += total;
811 appctx->ctx.cache.rem_data = rem_data + blksz;
812 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100813}
814
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200815static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
816 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100817{
Christopher Faulet95220e22018-12-07 17:34:39 +0100818 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
819 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200820 struct shared_block *shblk;
821 unsigned int offset, sz;
822 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100823
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200824 while (len) {
825 enum htx_blk_type type;
826 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100827
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200828 shblk = appctx->ctx.cache.next;
829 offset = appctx->ctx.cache.offset;
830 if (appctx->ctx.cache.rem_data) {
831 type = HTX_BLK_DATA;
832 info = 0;
833 goto add_data_blk;
834 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100835
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200836 /* Get info of the next HTX block. May be splitted on 2 shblk */
837 sz = MIN(4, shctx->block_size - offset);
838 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
839 offset += sz;
840 if (sz < 4) {
841 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
842 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
843 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100844 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200845
846 /* Get payload of the next HTX block and insert it. */
847 type = (info >> 28);
848 if (type != HTX_BLK_DATA)
849 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
850 else {
851 add_data_blk:
852 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100853 }
854
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200855 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100856 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200857 total += ret;
858 len -= ret;
859
860 if (appctx->ctx.cache.rem_data || type == mark)
861 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100862 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100863
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100864 return total;
865}
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200866
867static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
868{
869 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
870 unsigned int age;
871 char *end;
872
873 chunk_reset(&trash);
874 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
875 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
876 age = CACHE_ENTRY_MAX_AGE;
877 end = ultoa_o(age, b_head(&trash), b_size(&trash));
878 b_set_data(&trash, end - b_head(&trash));
879 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
880 return 0;
881 return 1;
882}
883
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200884static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100885{
886 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
887 struct shared_block *first = block_ptr(cache_ptr);
888 struct stream_interface *si = appctx->owner;
889 struct channel *req = si_oc(si);
890 struct channel *res = si_ic(si);
891 struct htx *req_htx, *res_htx;
892 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200893 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100894 size_t ret, total = 0;
895
896 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200897 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100898
899 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
900 goto out;
901
902 /* Check if the input buffer is avalaible. */
903 if (!b_size(&res->buf)) {
904 si_rx_room_blk(si);
905 goto out;
906 }
907
Willy Tarreauefef3232018-12-16 00:37:45 +0100908 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100909 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100910
911 if (appctx->st0 == HTX_CACHE_INIT) {
912 appctx->ctx.cache.next = block_ptr(cache_ptr);
913 appctx->ctx.cache.offset = sizeof(*cache_ptr);
914 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200915 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100916 appctx->st0 = HTX_CACHE_HEADER;
917 }
918
919 if (appctx->st0 == HTX_CACHE_HEADER) {
920 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200921 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
922 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
923 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
924 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100925 goto error;
926
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200927 /* Skip response body for HEAD requests */
928 if (si_strm(si)->txn->meth == HTTP_METH_HEAD)
Christopher Fauletf0dd0372019-02-25 11:08:34 +0100929 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100930 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200931 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100932 }
933
934 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200935 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
936 if (len) {
937 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
938 if (ret < len) {
939 si_rx_room_blk(si);
940 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100941 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100942 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200943 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100944 }
945
946 if (appctx->st0 == HTX_CACHE_EOM) {
947 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
948 si_rx_room_blk(si);
949 goto out;
950 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100951 appctx->st0 = HTX_CACHE_END;
952 }
953
954 end:
Christopher Fauletadb36312019-02-25 11:40:49 +0100955 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100956 res->flags |= CF_READ_NULL;
957 si_shutr(si);
958 }
959
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100960 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200961 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +0100962 if (total)
963 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100964 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +0100965
966 /* eat the whole request */
967 if (co_data(req)) {
968 req_htx = htx_from_buf(&req->buf);
969 co_htx_skip(req, req_htx, co_data(req));
970 htx_to_buf(req_htx, &req->buf);
971 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100972 return;
973
974 error:
975 /* Sent and HTTP error 500 */
976 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +0200977 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100978 res->buf.data = b_data(errmsg);
979 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
980 res_htx = htx_from_buf(&res->buf);
981
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200982 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100983 appctx->st0 = HTX_CACHE_END;
984 goto end;
985}
986
987
Christopher Faulet95220e22018-12-07 17:34:39 +0100988static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +0100989{
990 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +0100991 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +0100992
Christopher Faulet95220e22018-12-07 17:34:39 +0100993 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +0100994 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +0100995 goto err;
William Lallemand41db4602017-10-30 11:15:51 +0100996 }
997
998 /* check if a cache filter was already registered with this cache
999 * name, if that's the case, must use it. */
1000 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001001 if (fconf->id == cache_store_flt_id) {
1002 cconf = fconf->conf;
1003 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1004 rule->arg.act.p[0] = cconf;
1005 return 1;
1006 }
William Lallemand41db4602017-10-30 11:15:51 +01001007 }
1008 }
1009
Christopher Faulet95220e22018-12-07 17:34:39 +01001010 /* Create the filter cache config */
1011 cconf = calloc(1, sizeof(*cconf));
1012 if (!cconf) {
1013 memprintf(err, "out of memory\n");
1014 goto err;
1015 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001016 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001017 cconf->c.name = strdup(name);
1018 if (!cconf->c.name) {
1019 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001020 goto err;
1021 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001022
William Lallemand41db4602017-10-30 11:15:51 +01001023 /* register a filter to fill the cache buffer */
1024 fconf = calloc(1, sizeof(*fconf));
1025 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001026 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001027 goto err;
1028 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001029 fconf->id = cache_store_flt_id;
1030 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001031 fconf->ops = &cache_ops;
1032 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1033
Christopher Faulet95220e22018-12-07 17:34:39 +01001034 rule->arg.act.p[0] = cconf;
1035 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001036
Christopher Faulet95220e22018-12-07 17:34:39 +01001037 err:
1038 free(cconf);
1039 return 0;
1040}
1041
1042enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1043 struct act_rule *rule, char **err)
1044{
1045 rule->action = ACT_CUSTOM;
1046 rule->action_ptr = http_action_store_cache;
1047
1048 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1049 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001050
Christopher Faulet95220e22018-12-07 17:34:39 +01001051 (*orig_arg)++;
1052 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001053}
1054
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001055/* This produces a sha1 hash of the concatenation of the HTTP method,
1056 * the first occurrence of the Host header followed by the path component
1057 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001058int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001059{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001060 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001061 struct htx *htx = htxbuf(&s->req.buf);
1062 struct htx_sl *sl;
1063 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001064 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001065 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001066 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001067
William Lallemandf528fff2017-11-23 19:43:17 +01001068 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001069 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001070
1071 switch (txn->meth) {
1072 case HTTP_METH_HEAD:
1073 case HTTP_METH_GET:
1074 chunk_memcat(trash, "GET", 3);
1075 break;
1076 default:
1077 return 0;
1078 }
1079
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001080 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001081 uri = htx_sl_req_uri(sl); // whole uri
1082 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001083 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001084
1085 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1086 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1087 * URIs are almost always sent in absolute form with their scheme. In
1088 * this case, the scheme is almost always "https". In order to support
1089 * sharing of cache objects between H1 and H2, we'll hash the absolute
1090 * URI whenever known, or prepend "https://" + the Host header for
1091 * relative URIs. The difference will only appear on absolute HTTP/1
1092 * requests sent to an origin server, which practically is never met in
1093 * the real world so we don't care about the ability to share the same
1094 * key here.URIs are normalized from the absolute URI to an origin form as
1095 * well.
1096 */
1097 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
1098 chunk_cat(trash, b_fromist(ist("https://")));
1099 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1100 return 0;
1101 chunk_cat(trash, b_fromist(ctx.value));
1102 }
1103
1104 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001105
1106 /* hash everything */
1107 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001108 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001109 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1110
1111 return 1;
1112}
1113
William Lallemand41db4602017-10-30 11:15:51 +01001114enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1115 struct session *sess, struct stream *s, int flags)
1116{
William Lallemand77c11972017-10-31 20:43:01 +01001117
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001118 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001119 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001120 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1121 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001122
Willy Tarreau6905d182019-10-01 17:59:17 +02001123 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1124 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001125 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001126 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001127 txn->flags |= TX_CACHE_IGNORE;
1128
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001129 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001130
Willy Tarreau504455c2017-12-22 17:47:35 +01001131 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1132 return ACT_RET_CONT;
1133
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001134 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001135 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001136
Willy Tarreau504455c2017-12-22 17:47:35 +01001137 if (s->txn->flags & TX_CACHE_IGNORE)
1138 return ACT_RET_CONT;
1139
Willy Tarreaua1214a52018-12-14 14:00:25 +01001140 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001141 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001142 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001143 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001144
William Lallemanda400a3a2017-11-20 19:13:12 +01001145 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001146 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001147 if (res) {
1148 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001149 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1150 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001151 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001152 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001153 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001154 appctx->rule = rule;
1155 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001156 appctx->ctx.cache.next = NULL;
1157 appctx->ctx.cache.sent = 0;
Willy Tarreaua1214a52018-12-14 14:00:25 +01001158
1159 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001160 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001161 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001162 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001163 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001164 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001165 shctx_lock(shctx_ptr(cache));
1166 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1167 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001168 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001169 }
1170 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001171 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001172 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001173}
1174
1175
1176enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1177 struct act_rule *rule, char **err)
1178{
William Lallemand41db4602017-10-30 11:15:51 +01001179 rule->action = ACT_CUSTOM;
1180 rule->action_ptr = http_action_req_cache_use;
1181
Christopher Faulet95220e22018-12-07 17:34:39 +01001182 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001183 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001184
1185 (*orig_arg)++;
1186 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001187}
1188
1189int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1190{
1191 int err_code = 0;
1192
1193 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1194
1195 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001196 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1197 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001198 err_code |= ERR_ALERT | ERR_ABORT;
1199 goto out;
1200 }
1201
1202 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1203 err_code |= ERR_ABORT;
1204 goto out;
1205 }
1206
1207 if (tmp_cache_config == NULL) {
1208 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1209 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001210 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001211 err_code |= ERR_ALERT | ERR_ABORT;
1212 goto out;
1213 }
1214
1215 strlcpy2(tmp_cache_config->id, args[1], 33);
1216 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001217 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1218 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001219 err_code |= ERR_WARN;
1220 }
William Lallemand49b44532017-11-24 18:53:43 +01001221 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001222 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001223 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001224 }
1225 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001226 unsigned long int maxsize;
1227 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001228
1229 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1230 err_code |= ERR_ABORT;
1231 goto out;
1232 }
1233
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001234 maxsize = strtoul(args[1], &err, 10);
1235 if (err == args[1] || *err != '\0') {
1236 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1237 file, linenum, args[1]);
1238 err_code |= ERR_ABORT;
1239 goto out;
1240 }
1241
1242 if (maxsize > (UINT_MAX >> 20)) {
1243 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1244 file, linenum, args[1], UINT_MAX >> 20);
1245 err_code |= ERR_ABORT;
1246 goto out;
1247 }
1248
William Lallemand41db4602017-10-30 11:15:51 +01001249 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001250 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001251 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001252 } else if (strcmp(args[0], "max-age") == 0) {
1253 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 an age parameter in seconds.\n",
1260 file, linenum, args[0]);
1261 err_code |= ERR_WARN;
1262 }
1263
1264 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001265 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001266 unsigned int maxobjsz;
1267 char *err;
1268
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001269 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1270 err_code |= ERR_ABORT;
1271 goto out;
1272 }
1273
1274 if (!*args[1]) {
1275 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1276 file, linenum, args[0]);
1277 err_code |= ERR_WARN;
1278 }
1279
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001280 maxobjsz = strtoul(args[1], &err, 10);
1281 if (err == args[1] || *err != '\0') {
1282 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1283 file, linenum, args[1]);
1284 err_code |= ERR_ABORT;
1285 goto out;
1286 }
1287 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001288 }
1289 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001290 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001291 err_code |= ERR_ALERT | ERR_FATAL;
1292 goto out;
1293 }
1294out:
1295 return err_code;
1296}
1297
1298/* once the cache section is parsed */
1299
1300int cfg_post_parse_section_cache()
1301{
1302 struct shared_context *shctx;
1303 int err_code = 0;
1304 int ret_shctx;
1305
1306 if (tmp_cache_config) {
1307 struct cache *cache;
1308
1309 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001310 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001311 err_code |= ERR_FATAL | ERR_ALERT;
1312 goto out;
1313 }
1314
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001315 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001316 /* Default max. file size is a 256th of the cache size. */
1317 tmp_cache_config->maxobjsz =
1318 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001319 }
1320 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1321 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1322 err_code |= ERR_FATAL | ERR_ALERT;
1323 goto out;
1324 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001325
1326 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE,
1327 tmp_cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001328
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001329 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001330 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001331 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001332 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001333 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001334
1335 err_code |= ERR_FATAL | ERR_ALERT;
1336 goto out;
1337 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001338 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +01001339 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
1340 cache = (struct cache *)shctx->data;
1341 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001342 LIST_ADDQ(&caches, &cache->list);
1343 }
1344out:
1345 free(tmp_cache_config);
1346 tmp_cache_config = NULL;
1347 return err_code;
1348
William Lallemand41db4602017-10-30 11:15:51 +01001349}
1350
William Lallemand41db4602017-10-30 11:15:51 +01001351struct flt_ops cache_ops = {
1352 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001353 .check = cache_store_check,
1354 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001355
William Lallemand4da3f8a2017-10-31 14:33:34 +01001356 /* Handle channels activity */
1357 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001358 .channel_end_analyze = cache_store_chn_end_analyze,
Christopher Faulet839791a2019-01-07 16:12:07 +01001359 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001360
1361 /* Filter HTTP requests and responses */
1362 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001363 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001364 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001365};
1366
Christopher Faulet99a17a22018-12-11 09:18:27 +01001367
1368
1369static int
1370parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1371 struct flt_conf *fconf, char **err, void *private)
1372{
1373 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001374 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001375 char *name = NULL;
1376 int pos = *cur_arg;
1377
1378 /* Get the cache filter name*/
1379 if (!strcmp(args[pos], "cache")) {
1380 if (!*args[pos + 1]) {
1381 memprintf(err, "%s : expects an <id> argument", args[pos]);
1382 goto error;
1383 }
1384 name = strdup(args[pos + 1]);
1385 if (!name) {
1386 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1387 goto error;
1388 }
1389 pos += 2;
1390 }
1391
1392 /* Check if an implicit filter with the same name already exists. If so,
1393 * we remove the implicit filter to use the explicit one. */
1394 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1395 if (f->id != cache_store_flt_id)
1396 continue;
1397
1398 cconf = f->conf;
1399 if (strcmp(name, cconf->c.name)) {
1400 cconf = NULL;
1401 continue;
1402 }
1403
1404 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1405 cconf = NULL;
1406 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1407 px->id, name);
1408 return -1;
1409 }
1410
1411 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1412 LIST_DEL(&f->list);
1413 free(f);
1414 free(name);
1415 break;
1416 }
1417
1418 /* No implicit cache filter found, create configuration for the explicit one */
1419 if (!cconf) {
1420 cconf = calloc(1, sizeof(*cconf));
1421 if (!cconf) {
1422 memprintf(err, "%s: out of memory", args[*cur_arg]);
1423 goto error;
1424 }
1425 cconf->c.name = name;
1426 }
1427
1428 cconf->flags = 0;
1429 fconf->id = cache_store_flt_id;
1430 fconf->conf = cconf;
1431 fconf->ops = &cache_ops;
1432
1433 *cur_arg = pos;
1434 return 0;
1435
1436 error:
1437 free(name);
1438 free(cconf);
1439 return -1;
1440}
1441
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001442static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001443{
1444 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1445 return 1;
1446
1447 return 0;
1448}
1449
1450static int cli_io_handler_show_cache(struct appctx *appctx)
1451{
1452 struct cache* cache = appctx->ctx.cli.p0;
1453 struct stream_interface *si = appctx->owner;
1454
William Lallemand1f49a362017-11-21 20:01:26 +01001455 if (cache == NULL) {
1456 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1457 }
1458
1459 list_for_each_entry_from(cache, &caches, list) {
1460 struct eb32_node *node = NULL;
1461 unsigned int next_key;
1462 struct cache_entry *entry;
1463
William Lallemand1f49a362017-11-21 20:01:26 +01001464 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001465 if (!next_key) {
1466 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1467 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001468 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001469 return 0;
1470 }
1471 }
William Lallemand1f49a362017-11-21 20:01:26 +01001472
1473 appctx->ctx.cli.p0 = cache;
1474
1475 while (1) {
1476
1477 shctx_lock(shctx_ptr(cache));
1478 node = eb32_lookup_ge(&cache->entries, next_key);
1479 if (!node) {
1480 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001481 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001482 break;
1483 }
1484
1485 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001486 chunk_printf(&trash, "%p hash:%u size:%u (%u blocks), refcount:%u, expire:%d\n", entry, (*(unsigned int *)entry->hash), block_ptr(entry)->len, block_ptr(entry)->block_count, block_ptr(entry)->refcount, entry->expire - (int)now.tv_sec);
William Lallemand1f49a362017-11-21 20:01:26 +01001487
1488 next_key = node->key + 1;
1489 appctx->ctx.cli.i0 = next_key;
1490
1491 shctx_unlock(shctx_ptr(cache));
1492
1493 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001494 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001495 return 0;
1496 }
1497 }
1498
1499 }
1500
1501 return 1;
1502
1503}
1504
Christopher Faulet99a17a22018-12-11 09:18:27 +01001505/* Declare the filter parser for "cache" keyword */
1506static struct flt_kw_list filter_kws = { "CACHE", { }, {
1507 { "cache", parse_cache_flt, NULL },
1508 { NULL, NULL, NULL },
1509 }
1510};
1511
1512INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1513
William Lallemand1f49a362017-11-21 20:01:26 +01001514static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001515 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1516 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001517}};
1518
Willy Tarreau0108d902018-11-25 19:14:37 +01001519INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001520
William Lallemand41db4602017-10-30 11:15:51 +01001521static struct action_kw_list http_res_actions = {
1522 .kw = {
1523 { "cache-store", parse_cache_store },
1524 { NULL, NULL }
1525 }
1526};
1527
Willy Tarreau0108d902018-11-25 19:14:37 +01001528INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1529
William Lallemand41db4602017-10-30 11:15:51 +01001530static struct action_kw_list http_req_actions = {
1531 .kw = {
1532 { "cache-use", parse_cache_use },
1533 { NULL, NULL }
1534 }
1535};
1536
Willy Tarreau0108d902018-11-25 19:14:37 +01001537INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1538
Willy Tarreau2231b632019-03-29 18:26:52 +01001539struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001540 .obj_type = OBJ_TYPE_APPLET,
1541 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001542 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001543 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001544};
1545
Willy Tarreaue6552512018-11-26 11:33:13 +01001546/* config parsers for this section */
1547REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);