blob: 242d7daf0b5a1e43ad89e48c0864e6a3d7e9da2e [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 Faulet27d93c32018-12-15 22:32:02 +0100184 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
185 /* Implicit declaration is only allowed with the
186 * compression. For other filters, an implicit
187 * declaration is required. */
188 ha_alert("config: %s '%s': require an explicit filter declaration "
189 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
190 return 1;
191 }
192
Christopher Fauletafd819c2018-12-11 08:57:45 +0100193 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100194 return 0;
195}
196
197static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100198cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
199{
200 if (!(chn->flags & CF_ISRESP))
201 return 1;
202
203 if (filter->ctx == NULL) {
204 struct cache_st *st;
205
Willy Tarreaubafbe012017-11-24 17:34:44 +0100206 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100207 if (st == NULL)
208 return -1;
209
William Lallemand4da3f8a2017-10-31 14:33:34 +0100210 st->first_block = NULL;
211 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100212
213 /* Register post-analyzer on AN_RES_WAIT_HTTP */
214 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100215 }
216
William Lallemand4da3f8a2017-10-31 14:33:34 +0100217 return 1;
218}
219
220static int
William Lallemand49dc0482017-11-24 14:33:54 +0100221cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
222{
223 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100224 struct cache_flt_conf *cconf = FLT_CONF(filter);
225 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100226 struct shared_context *shctx = shctx_ptr(cache);
227
228 if (!(chn->flags & CF_ISRESP))
229 return 1;
230
231 /* Everything should be released in the http_end filter, but we need to do it
232 * there too, in case of errors */
233
234 if (st && st->first_block) {
235
236 shctx_lock(shctx);
237 shctx_row_dec_hot(shctx, st->first_block);
238 shctx_unlock(shctx);
239
240 }
241 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100242 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100243 filter->ctx = NULL;
244 }
245
246 return 1;
247}
248
Christopher Faulet839791a2019-01-07 16:12:07 +0100249static int
250cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
251 unsigned an_bit)
252{
253 struct http_txn *txn = s->txn;
254 struct http_msg *msg = &txn->rsp;
255 struct cache_st *st = filter->ctx;
256
257 if (an_bit != AN_RES_WAIT_HTTP)
258 goto end;
259
260 /* Here we need to check if any compression filter precedes the cache
261 * filter. This is only possible when the compression is configured in
262 * the frontend while the cache filter is configured on the
263 * backend. This case cannot be detected during HAProxy startup. So in
264 * such cases, the cache is disabled.
265 */
266 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
267 pool_free(pool_head_cache_st, st);
268 filter->ctx = NULL;
269 }
270
271 end:
272 return 1;
273}
William Lallemand49dc0482017-11-24 14:33:54 +0100274
275static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100276cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
277{
278 struct cache_st *st = filter->ctx;
279
William Lallemand4da3f8a2017-10-31 14:33:34 +0100280 if (!(msg->chn->flags & CF_ISRESP) || !st)
281 return 1;
282
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200283 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100284 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100285 return 1;
286}
287
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200288static inline void disable_cache_entry(struct cache_st *st,
289 struct filter *filter, struct shared_context *shctx)
290{
291 struct cache_entry *object;
292
293 object = (struct cache_entry *)st->first_block->data;
294 filter->ctx = NULL; /* disable cache */
295 shctx_lock(shctx);
296 shctx_row_dec_hot(shctx, st->first_block);
297 object->eb.key = 0;
298 shctx_unlock(shctx);
299 pool_free(pool_head_cache_st, st);
300}
301
William Lallemand4da3f8a2017-10-31 14:33:34 +0100302static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100303cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
304 unsigned int offset, unsigned int len)
305{
Christopher Faulet95220e22018-12-07 17:34:39 +0100306 struct cache_flt_conf *cconf = FLT_CONF(filter);
307 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100308 struct cache_st *st = filter->ctx;
309 struct htx *htx = htxbuf(&msg->chn->buf);
310 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200311 struct shared_block *fb;
312 unsigned int orig_len, to_forward;
313 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100314
315 if (!len)
316 return len;
317
318 if (!st->first_block) {
319 unregister_data_filter(s, msg->chn, filter);
320 return len;
321 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100322
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200323 chunk_reset(&trash);
324 orig_len = len;
325 to_forward = 0;
Christopher Fauletee847d42019-05-23 11:55:33 +0200326 for (blk = htx_get_first_blk(htx); blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100327 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200328 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100329 struct ist v;
330
Christopher Fauletee847d42019-05-23 11:55:33 +0200331 if (offset >= sz) {
332 offset -= sz;
333 continue;
334 }
335
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100336 switch (type) {
337 case HTX_BLK_UNUSED:
338 break;
339
340 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100341 v = htx_get_blk_value(htx, blk);
342 v.ptr += offset;
343 v.len -= offset;
344 if (v.len > len)
345 v.len = len;
346
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200347 info = (type << 28) + v.len;
348 chunk_memcat(&trash, (char *)&info, sizeof(info));
349 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100350 to_forward += v.len;
351 len -= v.len;
352 break;
353
354 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200355 /* Here offset must always be 0 because only
356 * DATA blocks can be partially transferred. */
357 if (offset)
358 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100359 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200360 goto end;
361
362 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
363 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100364 to_forward += sz;
365 len -= sz;
366 break;
367 }
368
369 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100370 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200371
372 end:
373 shctx_lock(shctx);
374 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
375 if (!fb) {
376 shctx_unlock(shctx);
377 goto no_cache;
378 }
379 shctx_unlock(shctx);
380
381 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
382 (unsigned char *)b_head(&trash), b_data(&trash));
383 if (ret < 0)
384 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100385
386 return to_forward;
387
388 no_cache:
389 disable_cache_entry(st, filter, shctx);
390 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200391 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100392}
393
394static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100395cache_store_http_end(struct stream *s, struct filter *filter,
396 struct http_msg *msg)
397{
398 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100399 struct cache_flt_conf *cconf = FLT_CONF(filter);
400 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100401 struct shared_context *shctx = shctx_ptr(cache);
402 struct cache_entry *object;
403
404 if (!(msg->chn->flags & CF_ISRESP))
405 return 1;
406
407 if (st && st->first_block) {
408
409 object = (struct cache_entry *)st->first_block->data;
410
411 /* does not need to test if the insertion worked, if it
412 * doesn't, the blocks will be reused anyway */
413
414 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100415 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
416 object->eb.key = 0;
417 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100418 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100419 shctx_row_dec_hot(shctx, st->first_block);
420 shctx_unlock(shctx);
421
422 }
423 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100424 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100425 filter->ctx = NULL;
426 }
427
428 return 1;
429}
430
431 /*
432 * This intends to be used when checking HTTP headers for some
433 * word=value directive. Return a pointer to the first character of value, if
434 * the word was not found or if there wasn't any value assigned ot it return NULL
435 */
436char *directive_value(const char *sample, int slen, const char *word, int wlen)
437{
438 int st = 0;
439
440 if (slen < wlen)
441 return 0;
442
443 while (wlen) {
444 char c = *sample ^ *word;
445 if (c && c != ('A' ^ 'a'))
446 return NULL;
447 sample++;
448 word++;
449 slen--;
450 wlen--;
451 }
452
453 while (slen) {
454 if (st == 0) {
455 if (*sample != '=')
456 return NULL;
457 sample++;
458 slen--;
459 st = 1;
460 continue;
461 } else {
462 return (char *)sample;
463 }
464 }
465
466 return NULL;
467}
468
469/*
470 * Return the maxage in seconds of an HTTP response.
471 * Compute the maxage using either:
472 * - the assigned max-age of the cache
473 * - the s-maxage directive
474 * - the max-age directive
475 * - (Expires - Data) headers
476 * - the default-max-age of the cache
477 *
478 */
William Lallemand49b44532017-11-24 18:53:43 +0100479int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100480{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200481 struct htx *htx = htxbuf(&s->res.buf);
482 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100483 int smaxage = -1;
484 int maxage = -1;
485
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200486 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
487 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100488
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200489 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
490 if (value) {
491 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100492
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200493 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
494 chunk_strncat(chk, "", 1);
495 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100496 }
497
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200498 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
499 if (value) {
500 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200501
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200502 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
503 chunk_strncat(chk, "", 1);
504 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100505 }
506 }
507
508 /* TODO: Expires - Data */
509
510
511 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100512 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100513
514 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100515 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100516
William Lallemand49b44532017-11-24 18:53:43 +0100517 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100518
519}
520
521
William Lallemanda400a3a2017-11-20 19:13:12 +0100522static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
523{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200524 struct cache_entry *object = (struct cache_entry *)block->data;
525
526 if (first == block && object->eb.key)
527 eb32_delete(&object->eb);
528 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100529}
530
William Lallemand41db4602017-10-30 11:15:51 +0100531/*
532 * This fonction will store the headers of the response in a buffer and then
533 * register a filter to store the data
534 */
535enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200536 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100537{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200538 unsigned int age;
539 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100540 struct http_txn *txn = s->txn;
541 struct http_msg *msg = &txn->rsp;
542 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100543 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100544 struct cache_flt_conf *cconf = rule->arg.act.p[0];
545 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100546 struct cache_st *cache_ctx = NULL;
547 struct cache_entry *object, *old;
Willy Tarreauc9036c02019-01-11 19:38:25 +0100548 unsigned int key = *(unsigned int *)txn->cache_hash;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200549 struct htx *htx;
550 struct http_hdr_ctx ctx;
551 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100552
William Lallemand4da3f8a2017-10-31 14:33:34 +0100553 /* Don't cache if the response came from a cache */
554 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
555 s->target == &http_cache_applet.obj_type) {
556 goto out;
557 }
558
559 /* cache only HTTP/1.1 */
560 if (!(txn->req.flags & HTTP_MSGF_VER_11))
561 goto out;
562
Baptiste Assmann12635402019-08-07 12:24:36 +0200563 /* cache only GET or OPTIONS method */
564 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_OPTIONS)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100565 goto out;
566
Willy Tarreauc9036c02019-01-11 19:38:25 +0100567 /* cache key was not computed */
568 if (!key)
569 goto out;
570
William Lallemand4da3f8a2017-10-31 14:33:34 +0100571 /* cache only 200 status code */
572 if (txn->status != 200)
573 goto out;
574
Christopher Faulet839791a2019-01-07 16:12:07 +0100575 /* Find the corresponding filter instance for the current stream */
576 list_for_each_entry(filter, &s->strm_flt.filters, list) {
577 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
578 /* No filter ctx, don't cache anything */
579 if (!filter->ctx)
580 goto out;
581 cache_ctx = filter->ctx;
582 break;
583 }
584 }
585
586 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200587 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100588
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200589 /* Do not cache too big objects. */
590 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
591 htx->data + htx->extra > shctx->max_obj_size)
592 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100593
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200594 /* Does not manage Vary at the moment. We will need a secondary key later for that */
595 ctx.blk = NULL;
596 if (http_find_header(htx, ist("Vary"), &ctx, 0))
597 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100598
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200599 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100600
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200601 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
602 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100603
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200604 age = 0;
605 ctx.blk = NULL;
606 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
607 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
608 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
609 hdr_age = CACHE_ENTRY_MAX_AGE;
610 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100611 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200612 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100613 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100614
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200615 chunk_reset(&trash);
616 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
617 struct htx_blk *blk = htx_get_blk(htx, pos);
618 enum htx_blk_type type = htx_get_blk_type(blk);
619 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100620
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200621 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
622 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
623 if (type == HTX_BLK_EOH)
624 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200625 }
626
William Lallemand4da3f8a2017-10-31 14:33:34 +0100627 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200628 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100629 if (!first) {
630 shctx_unlock(shctx);
631 goto out;
632 }
633 shctx_unlock(shctx);
634
Willy Tarreau1093a452018-04-06 19:02:25 +0200635 /* the received memory is not initialized, we need at least to mark
636 * the object as not indexed yet.
637 */
638 object = (struct cache_entry *)first->data;
639 object->eb.node.leaf_p = NULL;
640 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200641 object->age = age;
Willy Tarreau1093a452018-04-06 19:02:25 +0200642
William Lallemand4da3f8a2017-10-31 14:33:34 +0100643 /* reserve space for the cache_entry structure */
644 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200645 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100646 /* cache the headers in a http action because it allows to chose what
647 * to cache, for example you might want to cache a response before
648 * modifying some HTTP headers, or on the contrary after modifying
649 * those headers.
650 */
651
652 /* does not need to be locked because it's in the "hot" list,
653 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200654 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
655 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100656
657 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100658 if (cache_ctx) {
659 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100660
Willy Tarreauc9036c02019-01-11 19:38:25 +0100661 object->eb.key = key;
662
Christopher Faulet839791a2019-01-07 16:12:07 +0100663 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
664 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100665
Christopher Faulet839791a2019-01-07 16:12:07 +0100666 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100667
Christopher Faulet839791a2019-01-07 16:12:07 +0100668 old = entry_exist(cconf->c.cache, txn->cache_hash);
669 if (old) {
670 eb32_delete(&old->eb);
671 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100672 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100673 shctx_unlock(shctx);
674
675 /* store latest value and expiration time */
676 object->latest_validation = now.tv_sec;
677 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
678 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100679 }
680
681out:
682 /* if does not cache */
683 if (first) {
684 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100685 first->len = 0;
686 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100687 shctx_row_dec_hot(shctx, first);
688 shctx_unlock(shctx);
689 }
690
William Lallemand41db4602017-10-30 11:15:51 +0100691 return ACT_RET_CONT;
692}
693
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100694#define HTX_CACHE_INIT 0 /* Initial state. */
695#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
696#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200697#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
698#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100699
William Lallemandecb73b12017-11-24 14:33:55 +0100700static void http_cache_applet_release(struct appctx *appctx)
701{
Christopher Faulet95220e22018-12-07 17:34:39 +0100702 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100703 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100704 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100705 struct shared_block *first = block_ptr(cache_ptr);
706
707 shctx_lock(shctx_ptr(cache));
708 shctx_row_dec_hot(shctx_ptr(cache), first);
709 shctx_unlock(shctx_ptr(cache));
710}
711
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200712
713static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
714 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100715{
Christopher Faulet95220e22018-12-07 17:34:39 +0100716 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
717 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200718 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200719 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200720 unsigned int max, total;
721 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100722
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200723 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
724 if (!max)
725 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200726 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200727 ? (info & 0xff) + ((info >> 8) & 0xfffff)
728 : info & 0xfffffff);
729 if (blksz > max)
730 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100731
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200732 blk = htx_add_blk(htx, type, blksz);
733 if (!blk)
734 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100735
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200736 blk->info = info;
737 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200738 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200739 while (blksz) {
740 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200741 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200742 offset += max;
743 blksz -= max;
744 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200745 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200746 if (blksz || offset == shctx->block_size) {
747 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
748 offset = 0;
749 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100750 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200751 appctx->ctx.cache.offset = offset;
752 appctx->ctx.cache.next = shblk;
753 appctx->ctx.cache.sent += total;
754 return total;
755}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100756
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200757static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
758 uint32_t info, struct shared_block *shblk, unsigned int offset)
759{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100760
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200761 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
762 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
763 unsigned int max, total, rem_data;
764 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100765
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200766 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
767 if (!max)
768 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100769
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200770 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200771 if (appctx->ctx.cache.rem_data) {
772 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200773 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200774 }
775 else {
776 blksz = (info & 0xfffffff);
777 total = 4;
778 }
779 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200780 rem_data = blksz - max;
781 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100782 }
783
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200784 while (blksz) {
785 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100786
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200787 max = MIN(blksz, shctx->block_size - offset);
788 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
789 offset += sz;
790 blksz -= sz;
791 total += sz;
792 if (sz < max)
793 break;
794 if (blksz || offset == shctx->block_size) {
795 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
796 offset = 0;
797 }
798 }
799
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200800 appctx->ctx.cache.offset = offset;
801 appctx->ctx.cache.next = shblk;
802 appctx->ctx.cache.sent += total;
803 appctx->ctx.cache.rem_data = rem_data + blksz;
804 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100805}
806
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200807static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
808 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100809{
Christopher Faulet95220e22018-12-07 17:34:39 +0100810 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
811 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200812 struct shared_block *shblk;
813 unsigned int offset, sz;
814 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100815
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200816 while (len) {
817 enum htx_blk_type type;
818 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100819
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200820 shblk = appctx->ctx.cache.next;
821 offset = appctx->ctx.cache.offset;
822 if (appctx->ctx.cache.rem_data) {
823 type = HTX_BLK_DATA;
824 info = 0;
825 goto add_data_blk;
826 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100827
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200828 /* Get info of the next HTX block. May be splitted on 2 shblk */
829 sz = MIN(4, shctx->block_size - offset);
830 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
831 offset += sz;
832 if (sz < 4) {
833 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
834 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
835 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100836 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200837
838 /* Get payload of the next HTX block and insert it. */
839 type = (info >> 28);
840 if (type != HTX_BLK_DATA)
841 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
842 else {
843 add_data_blk:
844 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100845 }
846
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200847 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100848 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200849 total += ret;
850 len -= ret;
851
852 if (appctx->ctx.cache.rem_data || type == mark)
853 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100854 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100855
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100856 return total;
857}
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200858
859static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
860{
861 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
862 unsigned int age;
863 char *end;
864
865 chunk_reset(&trash);
866 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
867 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
868 age = CACHE_ENTRY_MAX_AGE;
869 end = ultoa_o(age, b_head(&trash), b_size(&trash));
870 b_set_data(&trash, end - b_head(&trash));
871 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
872 return 0;
873 return 1;
874}
875
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200876static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100877{
878 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
879 struct shared_block *first = block_ptr(cache_ptr);
880 struct stream_interface *si = appctx->owner;
881 struct channel *req = si_oc(si);
882 struct channel *res = si_ic(si);
883 struct htx *req_htx, *res_htx;
884 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200885 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100886 size_t ret, total = 0;
887
888 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200889 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100890
891 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
892 goto out;
893
894 /* Check if the input buffer is avalaible. */
895 if (!b_size(&res->buf)) {
896 si_rx_room_blk(si);
897 goto out;
898 }
899
Willy Tarreauefef3232018-12-16 00:37:45 +0100900 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100901 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100902
903 if (appctx->st0 == HTX_CACHE_INIT) {
904 appctx->ctx.cache.next = block_ptr(cache_ptr);
905 appctx->ctx.cache.offset = sizeof(*cache_ptr);
906 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200907 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100908 appctx->st0 = HTX_CACHE_HEADER;
909 }
910
911 if (appctx->st0 == HTX_CACHE_HEADER) {
912 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200913 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
914 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
915 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
916 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100917 goto error;
918
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200919 /* Skip response body for HEAD requests */
920 if (si_strm(si)->txn->meth == HTTP_METH_HEAD)
Christopher Fauletf0dd0372019-02-25 11:08:34 +0100921 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100922 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200923 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100924 }
925
926 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200927 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
928 if (len) {
929 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
930 if (ret < len) {
931 si_rx_room_blk(si);
932 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100933 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100934 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200935 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100936 }
937
938 if (appctx->st0 == HTX_CACHE_EOM) {
939 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
940 si_rx_room_blk(si);
941 goto out;
942 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100943 appctx->st0 = HTX_CACHE_END;
944 }
945
946 end:
Christopher Fauletadb36312019-02-25 11:40:49 +0100947 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100948 res->flags |= CF_READ_NULL;
949 si_shutr(si);
950 }
951
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100952 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200953 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +0100954 if (total)
955 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100956 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +0100957
958 /* eat the whole request */
959 if (co_data(req)) {
960 req_htx = htx_from_buf(&req->buf);
961 co_htx_skip(req, req_htx, co_data(req));
962 htx_to_buf(req_htx, &req->buf);
963 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100964 return;
965
966 error:
967 /* Sent and HTTP error 500 */
968 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +0200969 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100970 res->buf.data = b_data(errmsg);
971 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
972 res_htx = htx_from_buf(&res->buf);
973
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200974 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100975 appctx->st0 = HTX_CACHE_END;
976 goto end;
977}
978
979
Christopher Faulet95220e22018-12-07 17:34:39 +0100980static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +0100981{
982 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +0100983 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +0100984
Christopher Faulet95220e22018-12-07 17:34:39 +0100985 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +0100986 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +0100987 goto err;
William Lallemand41db4602017-10-30 11:15:51 +0100988 }
989
990 /* check if a cache filter was already registered with this cache
991 * name, if that's the case, must use it. */
992 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +0100993 if (fconf->id == cache_store_flt_id) {
994 cconf = fconf->conf;
995 if (cconf && !strcmp((char *)cconf->c.name, name)) {
996 rule->arg.act.p[0] = cconf;
997 return 1;
998 }
William Lallemand41db4602017-10-30 11:15:51 +0100999 }
1000 }
1001
Christopher Faulet95220e22018-12-07 17:34:39 +01001002 /* Create the filter cache config */
1003 cconf = calloc(1, sizeof(*cconf));
1004 if (!cconf) {
1005 memprintf(err, "out of memory\n");
1006 goto err;
1007 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001008 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001009 cconf->c.name = strdup(name);
1010 if (!cconf->c.name) {
1011 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001012 goto err;
1013 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001014
William Lallemand41db4602017-10-30 11:15:51 +01001015 /* register a filter to fill the cache buffer */
1016 fconf = calloc(1, sizeof(*fconf));
1017 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001018 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001019 goto err;
1020 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001021 fconf->id = cache_store_flt_id;
1022 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001023 fconf->ops = &cache_ops;
1024 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1025
Christopher Faulet95220e22018-12-07 17:34:39 +01001026 rule->arg.act.p[0] = cconf;
1027 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001028
Christopher Faulet95220e22018-12-07 17:34:39 +01001029 err:
1030 free(cconf);
1031 return 0;
1032}
1033
1034enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1035 struct act_rule *rule, char **err)
1036{
1037 rule->action = ACT_CUSTOM;
1038 rule->action_ptr = http_action_store_cache;
1039
1040 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1041 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001042
Christopher Faulet95220e22018-12-07 17:34:39 +01001043 (*orig_arg)++;
1044 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001045}
1046
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001047/* This produces a sha1 hash of the concatenation of the HTTP method,
1048 * the first occurrence of the Host header followed by the path component
1049 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001050int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001051{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001052 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001053 struct htx *htx = htxbuf(&s->req.buf);
1054 struct htx_sl *sl;
1055 struct http_hdr_ctx ctx;
1056 struct ist path;
William Lallemandf528fff2017-11-23 19:43:17 +01001057 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001058 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001059
William Lallemandf528fff2017-11-23 19:43:17 +01001060 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001061 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001062
1063 switch (txn->meth) {
Baptiste Assmann12635402019-08-07 12:24:36 +02001064 case HTTP_METH_OPTIONS:
1065 chunk_memcat(trash, "OPTIONS", 7);
1066 break;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001067 case HTTP_METH_HEAD:
1068 case HTTP_METH_GET:
1069 chunk_memcat(trash, "GET", 3);
1070 break;
1071 default:
1072 return 0;
1073 }
1074
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001075 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1076 return 0;
1077 chunk_memcat(trash, ctx.value.ptr, ctx.value.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001078
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001079 sl = http_get_stline(htx);
1080 path = http_get_path(htx_sl_req_uri(sl));
1081 if (!path.ptr)
1082 return 0;
1083 chunk_memcat(trash, path.ptr, path.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001084
1085 /* hash everything */
1086 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001087 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001088 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1089
1090 return 1;
1091}
1092
William Lallemand41db4602017-10-30 11:15:51 +01001093enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1094 struct session *sess, struct stream *s, int flags)
1095{
William Lallemand77c11972017-10-31 20:43:01 +01001096
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001097 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001098 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001099 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1100 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001101
Baptiste Assmann12635402019-08-07 12:24:36 +02001102 /* Ignore cache for HTTP/1.0 requests and for requests other than GET,
1103 * HEAD and OPTIONS */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001104 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Baptiste Assmann12635402019-08-07 12:24:36 +02001105 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_OPTIONS))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001106 txn->flags |= TX_CACHE_IGNORE;
1107
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001108 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001109
Willy Tarreau504455c2017-12-22 17:47:35 +01001110 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1111 return ACT_RET_CONT;
1112
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001113 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001114 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001115
Willy Tarreau504455c2017-12-22 17:47:35 +01001116 if (s->txn->flags & TX_CACHE_IGNORE)
1117 return ACT_RET_CONT;
1118
Willy Tarreaua1214a52018-12-14 14:00:25 +01001119 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001120 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001121 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001122 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001123
William Lallemanda400a3a2017-11-20 19:13:12 +01001124 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001125 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001126 if (res) {
1127 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001128 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1129 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001130 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001131 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001132 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001133 appctx->rule = rule;
1134 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001135 appctx->ctx.cache.next = NULL;
1136 appctx->ctx.cache.sent = 0;
Willy Tarreaua1214a52018-12-14 14:00:25 +01001137
1138 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001139 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001140 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001141 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001142 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001143 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001144 shctx_lock(shctx_ptr(cache));
1145 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1146 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001147 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001148 }
1149 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001150 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001151 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001152}
1153
1154
1155enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1156 struct act_rule *rule, char **err)
1157{
William Lallemand41db4602017-10-30 11:15:51 +01001158 rule->action = ACT_CUSTOM;
1159 rule->action_ptr = http_action_req_cache_use;
1160
Christopher Faulet95220e22018-12-07 17:34:39 +01001161 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001162 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001163
1164 (*orig_arg)++;
1165 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001166}
1167
1168int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1169{
1170 int err_code = 0;
1171
1172 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1173
1174 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001175 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1176 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001177 err_code |= ERR_ALERT | ERR_ABORT;
1178 goto out;
1179 }
1180
1181 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1182 err_code |= ERR_ABORT;
1183 goto out;
1184 }
1185
1186 if (tmp_cache_config == NULL) {
1187 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1188 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001189 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001190 err_code |= ERR_ALERT | ERR_ABORT;
1191 goto out;
1192 }
1193
1194 strlcpy2(tmp_cache_config->id, args[1], 33);
1195 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001196 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1197 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001198 err_code |= ERR_WARN;
1199 }
William Lallemand49b44532017-11-24 18:53:43 +01001200 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001201 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001202 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001203 }
1204 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001205 unsigned long int maxsize;
1206 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001207
1208 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1209 err_code |= ERR_ABORT;
1210 goto out;
1211 }
1212
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001213 maxsize = strtoul(args[1], &err, 10);
1214 if (err == args[1] || *err != '\0') {
1215 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1216 file, linenum, args[1]);
1217 err_code |= ERR_ABORT;
1218 goto out;
1219 }
1220
1221 if (maxsize > (UINT_MAX >> 20)) {
1222 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1223 file, linenum, args[1], UINT_MAX >> 20);
1224 err_code |= ERR_ABORT;
1225 goto out;
1226 }
1227
William Lallemand41db4602017-10-30 11:15:51 +01001228 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001229 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001230 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001231 } else if (strcmp(args[0], "max-age") == 0) {
1232 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1233 err_code |= ERR_ABORT;
1234 goto out;
1235 }
1236
1237 if (!*args[1]) {
1238 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1239 file, linenum, args[0]);
1240 err_code |= ERR_WARN;
1241 }
1242
1243 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001244 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001245 unsigned int maxobjsz;
1246 char *err;
1247
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001248 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1249 err_code |= ERR_ABORT;
1250 goto out;
1251 }
1252
1253 if (!*args[1]) {
1254 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1255 file, linenum, args[0]);
1256 err_code |= ERR_WARN;
1257 }
1258
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001259 maxobjsz = strtoul(args[1], &err, 10);
1260 if (err == args[1] || *err != '\0') {
1261 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1262 file, linenum, args[1]);
1263 err_code |= ERR_ABORT;
1264 goto out;
1265 }
1266 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001267 }
1268 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001269 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001270 err_code |= ERR_ALERT | ERR_FATAL;
1271 goto out;
1272 }
1273out:
1274 return err_code;
1275}
1276
1277/* once the cache section is parsed */
1278
1279int cfg_post_parse_section_cache()
1280{
1281 struct shared_context *shctx;
1282 int err_code = 0;
1283 int ret_shctx;
1284
1285 if (tmp_cache_config) {
1286 struct cache *cache;
1287
1288 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001289 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001290 err_code |= ERR_FATAL | ERR_ALERT;
1291 goto out;
1292 }
1293
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001294 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001295 /* Default max. file size is a 256th of the cache size. */
1296 tmp_cache_config->maxobjsz =
1297 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001298 }
1299 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1300 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1301 err_code |= ERR_FATAL | ERR_ALERT;
1302 goto out;
1303 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001304
1305 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE,
1306 tmp_cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001307
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001308 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001309 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001310 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001311 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001312 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001313
1314 err_code |= ERR_FATAL | ERR_ALERT;
1315 goto out;
1316 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001317 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +01001318 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
1319 cache = (struct cache *)shctx->data;
1320 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001321 LIST_ADDQ(&caches, &cache->list);
1322 }
1323out:
1324 free(tmp_cache_config);
1325 tmp_cache_config = NULL;
1326 return err_code;
1327
William Lallemand41db4602017-10-30 11:15:51 +01001328}
1329
William Lallemand41db4602017-10-30 11:15:51 +01001330struct flt_ops cache_ops = {
1331 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001332 .check = cache_store_check,
1333 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001334
William Lallemand4da3f8a2017-10-31 14:33:34 +01001335 /* Handle channels activity */
1336 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001337 .channel_end_analyze = cache_store_chn_end_analyze,
Christopher Faulet839791a2019-01-07 16:12:07 +01001338 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001339
1340 /* Filter HTTP requests and responses */
1341 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001342 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001343 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001344};
1345
Christopher Faulet99a17a22018-12-11 09:18:27 +01001346
1347
1348static int
1349parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1350 struct flt_conf *fconf, char **err, void *private)
1351{
1352 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001353 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001354 char *name = NULL;
1355 int pos = *cur_arg;
1356
1357 /* Get the cache filter name*/
1358 if (!strcmp(args[pos], "cache")) {
1359 if (!*args[pos + 1]) {
1360 memprintf(err, "%s : expects an <id> argument", args[pos]);
1361 goto error;
1362 }
1363 name = strdup(args[pos + 1]);
1364 if (!name) {
1365 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1366 goto error;
1367 }
1368 pos += 2;
1369 }
1370
1371 /* Check if an implicit filter with the same name already exists. If so,
1372 * we remove the implicit filter to use the explicit one. */
1373 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1374 if (f->id != cache_store_flt_id)
1375 continue;
1376
1377 cconf = f->conf;
1378 if (strcmp(name, cconf->c.name)) {
1379 cconf = NULL;
1380 continue;
1381 }
1382
1383 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1384 cconf = NULL;
1385 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1386 px->id, name);
1387 return -1;
1388 }
1389
1390 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1391 LIST_DEL(&f->list);
1392 free(f);
1393 free(name);
1394 break;
1395 }
1396
1397 /* No implicit cache filter found, create configuration for the explicit one */
1398 if (!cconf) {
1399 cconf = calloc(1, sizeof(*cconf));
1400 if (!cconf) {
1401 memprintf(err, "%s: out of memory", args[*cur_arg]);
1402 goto error;
1403 }
1404 cconf->c.name = name;
1405 }
1406
1407 cconf->flags = 0;
1408 fconf->id = cache_store_flt_id;
1409 fconf->conf = cconf;
1410 fconf->ops = &cache_ops;
1411
1412 *cur_arg = pos;
1413 return 0;
1414
1415 error:
1416 free(name);
1417 free(cconf);
1418 return -1;
1419}
1420
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001421static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001422{
1423 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1424 return 1;
1425
1426 return 0;
1427}
1428
1429static int cli_io_handler_show_cache(struct appctx *appctx)
1430{
1431 struct cache* cache = appctx->ctx.cli.p0;
1432 struct stream_interface *si = appctx->owner;
1433
William Lallemand1f49a362017-11-21 20:01:26 +01001434 if (cache == NULL) {
1435 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1436 }
1437
1438 list_for_each_entry_from(cache, &caches, list) {
1439 struct eb32_node *node = NULL;
1440 unsigned int next_key;
1441 struct cache_entry *entry;
1442
William Lallemand1f49a362017-11-21 20:01:26 +01001443 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001444 if (!next_key) {
1445 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1446 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001447 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001448 return 0;
1449 }
1450 }
William Lallemand1f49a362017-11-21 20:01:26 +01001451
1452 appctx->ctx.cli.p0 = cache;
1453
1454 while (1) {
1455
1456 shctx_lock(shctx_ptr(cache));
1457 node = eb32_lookup_ge(&cache->entries, next_key);
1458 if (!node) {
1459 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001460 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001461 break;
1462 }
1463
1464 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001465 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 +01001466
1467 next_key = node->key + 1;
1468 appctx->ctx.cli.i0 = next_key;
1469
1470 shctx_unlock(shctx_ptr(cache));
1471
1472 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001473 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001474 return 0;
1475 }
1476 }
1477
1478 }
1479
1480 return 1;
1481
1482}
1483
Christopher Faulet99a17a22018-12-11 09:18:27 +01001484/* Declare the filter parser for "cache" keyword */
1485static struct flt_kw_list filter_kws = { "CACHE", { }, {
1486 { "cache", parse_cache_flt, NULL },
1487 { NULL, NULL, NULL },
1488 }
1489};
1490
1491INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1492
William Lallemand1f49a362017-11-21 20:01:26 +01001493static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001494 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1495 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001496}};
1497
Willy Tarreau0108d902018-11-25 19:14:37 +01001498INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001499
William Lallemand41db4602017-10-30 11:15:51 +01001500static struct action_kw_list http_res_actions = {
1501 .kw = {
1502 { "cache-store", parse_cache_store },
1503 { NULL, NULL }
1504 }
1505};
1506
Willy Tarreau0108d902018-11-25 19:14:37 +01001507INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1508
William Lallemand41db4602017-10-30 11:15:51 +01001509static struct action_kw_list http_req_actions = {
1510 .kw = {
1511 { "cache-use", parse_cache_use },
1512 { NULL, NULL }
1513 }
1514};
1515
Willy Tarreau0108d902018-11-25 19:14:37 +01001516INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1517
Willy Tarreau2231b632019-03-29 18:26:52 +01001518struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001519 .obj_type = OBJ_TYPE_APPLET,
1520 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001521 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001522 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001523};
1524
Willy Tarreaue6552512018-11-26 11:33:13 +01001525/* config parsers for this section */
1526REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);