blob: dc11cf532039d5a9ee95ff806fbbcd3ac3e010fc [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);
William Lallemandd1d1e222019-08-28 15:22:49 +020088static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +010089static struct cache *tmp_cache_config = NULL;
90
Willy Tarreau8ceae722018-11-26 11:58:30 +010091DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
92
William Lallemandf528fff2017-11-23 19:43:17 +010093struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010094{
95 struct eb32_node *node;
96 struct cache_entry *entry;
97
William Lallemandf528fff2017-11-23 19:43:17 +010098 node = eb32_lookup(&cache->entries, (*(unsigned int *)hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010099 if (!node)
100 return NULL;
101
102 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100103
104 /* if that's not the right node */
105 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
106 return NULL;
107
William Lallemand08727662017-11-21 20:01:27 +0100108 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100109 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100110 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100111 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100112 entry->eb.key = 0;
113 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100114 return NULL;
115
116}
117
118static inline struct shared_context *shctx_ptr(struct cache *cache)
119{
120 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
121}
122
William Lallemand77c11972017-10-31 20:43:01 +0100123static inline struct shared_block *block_ptr(struct cache_entry *entry)
124{
125 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
126}
127
128
129
William Lallemand41db4602017-10-30 11:15:51 +0100130static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100131cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100132{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100133 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100134 return 0;
135}
136
Christopher Faulet95220e22018-12-07 17:34:39 +0100137static void
138cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
139{
140 struct cache_flt_conf *cconf = fconf->conf;
141
142 free(cconf);
143}
144
William Lallemand4da3f8a2017-10-31 14:33:34 +0100145static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100146cache_store_check(struct proxy *px, struct flt_conf *fconf)
147{
148 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100149 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100150 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100151 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100152
William Lallemandd1d1e222019-08-28 15:22:49 +0200153 /* Find the cache corresponding to the name in the filter config. The
154 * cache will not be referenced now in the filter config because it is
155 * not fully allocated. This step will be performed during the cache
156 * post_check.
157 */
158 list_for_each_entry(cache, &caches_config, list) {
159 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100160 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100161 }
162
163 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
164 proxy_type_str(px), px->id, (char *)cconf->c.name);
165 return 1;
166
167 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100168 /* Here <cache> points on the cache the filter must use and <cconf>
169 * points on the cache filter configuration. */
170
171 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100172 * enabled and if it is after the cache. When the compression is before
173 * the cache, an error is returned. Also check if the cache filter must
174 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100175 list_for_each_entry(f, &px->filter_configs, list) {
176 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100177 /* The compression filter must be evaluated after the cache. */
178 if (comp) {
179 ha_alert("config: %s '%s': unable to enable the compression filter before "
180 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
181 return 1;
182 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100183 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200184 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100185 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200186 else if (f->id == fcgi_flt_id)
187 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100188 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
189 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200190 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100191 * declaration is required. */
192 ha_alert("config: %s '%s': require an explicit filter declaration "
193 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
194 return 1;
195 }
196
Christopher Fauletafd819c2018-12-11 08:57:45 +0100197 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100198 return 0;
199}
200
201static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100202cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
203{
204 if (!(chn->flags & CF_ISRESP))
205 return 1;
206
207 if (filter->ctx == NULL) {
208 struct cache_st *st;
209
Willy Tarreaubafbe012017-11-24 17:34:44 +0100210 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100211 if (st == NULL)
212 return -1;
213
William Lallemand4da3f8a2017-10-31 14:33:34 +0100214 st->first_block = NULL;
215 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100216
217 /* Register post-analyzer on AN_RES_WAIT_HTTP */
218 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100219 }
220
William Lallemand4da3f8a2017-10-31 14:33:34 +0100221 return 1;
222}
223
224static int
William Lallemand49dc0482017-11-24 14:33:54 +0100225cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
226{
227 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100228 struct cache_flt_conf *cconf = FLT_CONF(filter);
229 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100230 struct shared_context *shctx = shctx_ptr(cache);
231
232 if (!(chn->flags & CF_ISRESP))
233 return 1;
234
235 /* Everything should be released in the http_end filter, but we need to do it
236 * there too, in case of errors */
237
238 if (st && st->first_block) {
239
240 shctx_lock(shctx);
241 shctx_row_dec_hot(shctx, st->first_block);
242 shctx_unlock(shctx);
243
244 }
245 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100246 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100247 filter->ctx = NULL;
248 }
249
250 return 1;
251}
252
Christopher Faulet839791a2019-01-07 16:12:07 +0100253static int
254cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
255 unsigned an_bit)
256{
257 struct http_txn *txn = s->txn;
258 struct http_msg *msg = &txn->rsp;
259 struct cache_st *st = filter->ctx;
260
261 if (an_bit != AN_RES_WAIT_HTTP)
262 goto end;
263
264 /* Here we need to check if any compression filter precedes the cache
265 * filter. This is only possible when the compression is configured in
266 * the frontend while the cache filter is configured on the
267 * backend. This case cannot be detected during HAProxy startup. So in
268 * such cases, the cache is disabled.
269 */
270 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
271 pool_free(pool_head_cache_st, st);
272 filter->ctx = NULL;
273 }
274
275 end:
276 return 1;
277}
William Lallemand49dc0482017-11-24 14:33:54 +0100278
279static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100280cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
281{
282 struct cache_st *st = filter->ctx;
283
William Lallemand4da3f8a2017-10-31 14:33:34 +0100284 if (!(msg->chn->flags & CF_ISRESP) || !st)
285 return 1;
286
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200287 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100288 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100289 return 1;
290}
291
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200292static inline void disable_cache_entry(struct cache_st *st,
293 struct filter *filter, struct shared_context *shctx)
294{
295 struct cache_entry *object;
296
297 object = (struct cache_entry *)st->first_block->data;
298 filter->ctx = NULL; /* disable cache */
299 shctx_lock(shctx);
300 shctx_row_dec_hot(shctx, st->first_block);
301 object->eb.key = 0;
302 shctx_unlock(shctx);
303 pool_free(pool_head_cache_st, st);
304}
305
William Lallemand4da3f8a2017-10-31 14:33:34 +0100306static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100307cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
308 unsigned int offset, unsigned int len)
309{
Christopher Faulet95220e22018-12-07 17:34:39 +0100310 struct cache_flt_conf *cconf = FLT_CONF(filter);
311 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100312 struct cache_st *st = filter->ctx;
313 struct htx *htx = htxbuf(&msg->chn->buf);
314 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200315 struct shared_block *fb;
316 unsigned int orig_len, to_forward;
317 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100318
319 if (!len)
320 return len;
321
322 if (!st->first_block) {
323 unregister_data_filter(s, msg->chn, filter);
324 return len;
325 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100326
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200327 chunk_reset(&trash);
328 orig_len = len;
329 to_forward = 0;
Christopher Fauletee847d42019-05-23 11:55:33 +0200330 for (blk = htx_get_first_blk(htx); blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100331 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200332 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100333 struct ist v;
334
Christopher Fauletee847d42019-05-23 11:55:33 +0200335 if (offset >= sz) {
336 offset -= sz;
337 continue;
338 }
339
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100340 switch (type) {
341 case HTX_BLK_UNUSED:
342 break;
343
344 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100345 v = htx_get_blk_value(htx, blk);
346 v.ptr += offset;
347 v.len -= offset;
348 if (v.len > len)
349 v.len = len;
350
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200351 info = (type << 28) + v.len;
352 chunk_memcat(&trash, (char *)&info, sizeof(info));
353 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100354 to_forward += v.len;
355 len -= v.len;
356 break;
357
358 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200359 /* Here offset must always be 0 because only
360 * DATA blocks can be partially transferred. */
361 if (offset)
362 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100363 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200364 goto end;
365
366 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
367 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100368 to_forward += sz;
369 len -= sz;
370 break;
371 }
372
373 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100374 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200375
376 end:
377 shctx_lock(shctx);
378 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
379 if (!fb) {
380 shctx_unlock(shctx);
381 goto no_cache;
382 }
383 shctx_unlock(shctx);
384
385 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
386 (unsigned char *)b_head(&trash), b_data(&trash));
387 if (ret < 0)
388 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100389
390 return to_forward;
391
392 no_cache:
393 disable_cache_entry(st, filter, shctx);
394 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200395 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100396}
397
398static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100399cache_store_http_end(struct stream *s, struct filter *filter,
400 struct http_msg *msg)
401{
402 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100403 struct cache_flt_conf *cconf = FLT_CONF(filter);
404 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100405 struct shared_context *shctx = shctx_ptr(cache);
406 struct cache_entry *object;
407
408 if (!(msg->chn->flags & CF_ISRESP))
409 return 1;
410
411 if (st && st->first_block) {
412
413 object = (struct cache_entry *)st->first_block->data;
414
415 /* does not need to test if the insertion worked, if it
416 * doesn't, the blocks will be reused anyway */
417
418 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100419 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
420 object->eb.key = 0;
421 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100422 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100423 shctx_row_dec_hot(shctx, st->first_block);
424 shctx_unlock(shctx);
425
426 }
427 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100428 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100429 filter->ctx = NULL;
430 }
431
432 return 1;
433}
434
435 /*
436 * This intends to be used when checking HTTP headers for some
437 * word=value directive. Return a pointer to the first character of value, if
438 * the word was not found or if there wasn't any value assigned ot it return NULL
439 */
440char *directive_value(const char *sample, int slen, const char *word, int wlen)
441{
442 int st = 0;
443
444 if (slen < wlen)
445 return 0;
446
447 while (wlen) {
448 char c = *sample ^ *word;
449 if (c && c != ('A' ^ 'a'))
450 return NULL;
451 sample++;
452 word++;
453 slen--;
454 wlen--;
455 }
456
457 while (slen) {
458 if (st == 0) {
459 if (*sample != '=')
460 return NULL;
461 sample++;
462 slen--;
463 st = 1;
464 continue;
465 } else {
466 return (char *)sample;
467 }
468 }
469
470 return NULL;
471}
472
473/*
474 * Return the maxage in seconds of an HTTP response.
475 * Compute the maxage using either:
476 * - the assigned max-age of the cache
477 * - the s-maxage directive
478 * - the max-age directive
479 * - (Expires - Data) headers
480 * - the default-max-age of the cache
481 *
482 */
William Lallemand49b44532017-11-24 18:53:43 +0100483int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100484{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200485 struct htx *htx = htxbuf(&s->res.buf);
486 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100487 int smaxage = -1;
488 int maxage = -1;
489
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200490 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
491 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100492
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200493 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
494 if (value) {
495 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100496
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200497 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
498 chunk_strncat(chk, "", 1);
499 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100500 }
501
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200502 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
503 if (value) {
504 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200505
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200506 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
507 chunk_strncat(chk, "", 1);
508 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100509 }
510 }
511
512 /* TODO: Expires - Data */
513
514
515 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100516 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100517
518 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100519 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100520
William Lallemand49b44532017-11-24 18:53:43 +0100521 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100522
523}
524
525
William Lallemanda400a3a2017-11-20 19:13:12 +0100526static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
527{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200528 struct cache_entry *object = (struct cache_entry *)block->data;
529
530 if (first == block && object->eb.key)
531 eb32_delete(&object->eb);
532 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100533}
534
William Lallemand41db4602017-10-30 11:15:51 +0100535/*
536 * This fonction will store the headers of the response in a buffer and then
537 * register a filter to store the data
538 */
539enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200540 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100541{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200542 unsigned int age;
543 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100544 struct http_txn *txn = s->txn;
545 struct http_msg *msg = &txn->rsp;
546 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100547 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100548 struct cache_flt_conf *cconf = rule->arg.act.p[0];
549 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100550 struct cache_st *cache_ctx = NULL;
551 struct cache_entry *object, *old;
Willy Tarreauc9036c02019-01-11 19:38:25 +0100552 unsigned int key = *(unsigned int *)txn->cache_hash;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200553 struct htx *htx;
554 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200555 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200556 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100557
William Lallemand4da3f8a2017-10-31 14:33:34 +0100558 /* Don't cache if the response came from a cache */
559 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
560 s->target == &http_cache_applet.obj_type) {
561 goto out;
562 }
563
564 /* cache only HTTP/1.1 */
565 if (!(txn->req.flags & HTTP_MSGF_VER_11))
566 goto out;
567
Willy Tarreau6905d182019-10-01 17:59:17 +0200568 /* cache only GET method */
569 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100570 goto out;
571
Willy Tarreauc9036c02019-01-11 19:38:25 +0100572 /* cache key was not computed */
573 if (!key)
574 goto out;
575
William Lallemand4da3f8a2017-10-31 14:33:34 +0100576 /* cache only 200 status code */
577 if (txn->status != 200)
578 goto out;
579
Christopher Faulet839791a2019-01-07 16:12:07 +0100580 /* Find the corresponding filter instance for the current stream */
581 list_for_each_entry(filter, &s->strm_flt.filters, list) {
582 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
583 /* No filter ctx, don't cache anything */
584 if (!filter->ctx)
585 goto out;
586 cache_ctx = filter->ctx;
587 break;
588 }
589 }
590
591 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200592 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100593
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200594 /* Do not cache too big objects. */
595 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
596 htx->data + htx->extra > shctx->max_obj_size)
597 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100598
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200599 /* Does not manage Vary at the moment. We will need a secondary key later for that */
600 ctx.blk = NULL;
601 if (http_find_header(htx, ist("Vary"), &ctx, 0))
602 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100603
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200604 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100605
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200606 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
607 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100608
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200609 age = 0;
610 ctx.blk = NULL;
611 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
612 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
613 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
614 hdr_age = CACHE_ENTRY_MAX_AGE;
615 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100616 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200617 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100618 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100619
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200620 chunk_reset(&trash);
621 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
622 struct htx_blk *blk = htx_get_blk(htx, pos);
623 enum htx_blk_type type = htx_get_blk_type(blk);
624 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100625
Christopher Fauletb0667472019-09-03 22:22:12 +0200626 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200627 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
628 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
629 if (type == HTX_BLK_EOH)
630 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200631 }
632
Christopher Fauletb0667472019-09-03 22:22:12 +0200633 /* Do not cache objects if the headers are too big. */
634 if (hdrs_len > htx->size - global.tune.maxrewrite)
635 goto out;
636
William Lallemand4da3f8a2017-10-31 14:33:34 +0100637 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200638 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100639 if (!first) {
640 shctx_unlock(shctx);
641 goto out;
642 }
643 shctx_unlock(shctx);
644
Willy Tarreau1093a452018-04-06 19:02:25 +0200645 /* the received memory is not initialized, we need at least to mark
646 * the object as not indexed yet.
647 */
648 object = (struct cache_entry *)first->data;
649 object->eb.node.leaf_p = NULL;
650 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200651 object->age = age;
Willy Tarreau1093a452018-04-06 19:02:25 +0200652
William Lallemand4da3f8a2017-10-31 14:33:34 +0100653 /* reserve space for the cache_entry structure */
654 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200655 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100656 /* cache the headers in a http action because it allows to chose what
657 * to cache, for example you might want to cache a response before
658 * modifying some HTTP headers, or on the contrary after modifying
659 * those headers.
660 */
661
662 /* does not need to be locked because it's in the "hot" list,
663 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200664 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
665 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100666
667 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100668 if (cache_ctx) {
669 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100670
Willy Tarreauc9036c02019-01-11 19:38:25 +0100671 object->eb.key = key;
672
Christopher Faulet839791a2019-01-07 16:12:07 +0100673 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
674 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100675
Christopher Faulet839791a2019-01-07 16:12:07 +0100676 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100677
Christopher Faulet839791a2019-01-07 16:12:07 +0100678 old = entry_exist(cconf->c.cache, txn->cache_hash);
679 if (old) {
680 eb32_delete(&old->eb);
681 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100682 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100683 shctx_unlock(shctx);
684
685 /* store latest value and expiration time */
686 object->latest_validation = now.tv_sec;
687 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
688 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100689 }
690
691out:
692 /* if does not cache */
693 if (first) {
694 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100695 first->len = 0;
696 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100697 shctx_row_dec_hot(shctx, first);
698 shctx_unlock(shctx);
699 }
700
William Lallemand41db4602017-10-30 11:15:51 +0100701 return ACT_RET_CONT;
702}
703
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100704#define HTX_CACHE_INIT 0 /* Initial state. */
705#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
706#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200707#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
708#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100709
William Lallemandecb73b12017-11-24 14:33:55 +0100710static void http_cache_applet_release(struct appctx *appctx)
711{
Christopher Faulet95220e22018-12-07 17:34:39 +0100712 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100713 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100714 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100715 struct shared_block *first = block_ptr(cache_ptr);
716
717 shctx_lock(shctx_ptr(cache));
718 shctx_row_dec_hot(shctx_ptr(cache), first);
719 shctx_unlock(shctx_ptr(cache));
720}
721
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200722
723static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
724 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100725{
Christopher Faulet95220e22018-12-07 17:34:39 +0100726 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
727 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200728 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200729 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200730 unsigned int max, total;
731 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100732
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200733 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
734 if (!max)
735 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200736 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200737 ? (info & 0xff) + ((info >> 8) & 0xfffff)
738 : info & 0xfffffff);
739 if (blksz > max)
740 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100741
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200742 blk = htx_add_blk(htx, type, blksz);
743 if (!blk)
744 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100745
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200746 blk->info = info;
747 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200748 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200749 while (blksz) {
750 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200751 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200752 offset += max;
753 blksz -= max;
754 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200755 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200756 if (blksz || offset == shctx->block_size) {
757 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
758 offset = 0;
759 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100760 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200761 appctx->ctx.cache.offset = offset;
762 appctx->ctx.cache.next = shblk;
763 appctx->ctx.cache.sent += total;
764 return total;
765}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100766
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200767static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
768 uint32_t info, struct shared_block *shblk, unsigned int offset)
769{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100770
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200771 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
772 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
773 unsigned int max, total, rem_data;
774 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100775
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200776 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
777 if (!max)
778 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100779
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200780 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200781 if (appctx->ctx.cache.rem_data) {
782 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200783 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200784 }
785 else {
786 blksz = (info & 0xfffffff);
787 total = 4;
788 }
789 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200790 rem_data = blksz - max;
791 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100792 }
793
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200794 while (blksz) {
795 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100796
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200797 max = MIN(blksz, shctx->block_size - offset);
798 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
799 offset += sz;
800 blksz -= sz;
801 total += sz;
802 if (sz < max)
803 break;
804 if (blksz || offset == shctx->block_size) {
805 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
806 offset = 0;
807 }
808 }
809
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200810 appctx->ctx.cache.offset = offset;
811 appctx->ctx.cache.next = shblk;
812 appctx->ctx.cache.sent += total;
813 appctx->ctx.cache.rem_data = rem_data + blksz;
814 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100815}
816
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200817static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
818 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100819{
Christopher Faulet95220e22018-12-07 17:34:39 +0100820 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
821 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200822 struct shared_block *shblk;
823 unsigned int offset, sz;
824 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100825
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200826 while (len) {
827 enum htx_blk_type type;
828 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100829
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200830 shblk = appctx->ctx.cache.next;
831 offset = appctx->ctx.cache.offset;
832 if (appctx->ctx.cache.rem_data) {
833 type = HTX_BLK_DATA;
834 info = 0;
835 goto add_data_blk;
836 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100837
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200838 /* Get info of the next HTX block. May be splitted on 2 shblk */
839 sz = MIN(4, shctx->block_size - offset);
840 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
841 offset += sz;
842 if (sz < 4) {
843 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
844 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
845 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100846 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200847
848 /* Get payload of the next HTX block and insert it. */
849 type = (info >> 28);
850 if (type != HTX_BLK_DATA)
851 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
852 else {
853 add_data_blk:
854 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100855 }
856
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200857 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100858 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200859 total += ret;
860 len -= ret;
861
862 if (appctx->ctx.cache.rem_data || type == mark)
863 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100864 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100865
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100866 return total;
867}
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200868
869static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
870{
871 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
872 unsigned int age;
873 char *end;
874
875 chunk_reset(&trash);
876 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
877 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
878 age = CACHE_ENTRY_MAX_AGE;
879 end = ultoa_o(age, b_head(&trash), b_size(&trash));
880 b_set_data(&trash, end - b_head(&trash));
881 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
882 return 0;
883 return 1;
884}
885
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200886static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100887{
888 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
889 struct shared_block *first = block_ptr(cache_ptr);
890 struct stream_interface *si = appctx->owner;
891 struct channel *req = si_oc(si);
892 struct channel *res = si_ic(si);
893 struct htx *req_htx, *res_htx;
894 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200895 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100896 size_t ret, total = 0;
897
898 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200899 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100900
901 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
902 goto out;
903
904 /* Check if the input buffer is avalaible. */
905 if (!b_size(&res->buf)) {
906 si_rx_room_blk(si);
907 goto out;
908 }
909
Willy Tarreauefef3232018-12-16 00:37:45 +0100910 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100911 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100912
913 if (appctx->st0 == HTX_CACHE_INIT) {
914 appctx->ctx.cache.next = block_ptr(cache_ptr);
915 appctx->ctx.cache.offset = sizeof(*cache_ptr);
916 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200917 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100918 appctx->st0 = HTX_CACHE_HEADER;
919 }
920
921 if (appctx->st0 == HTX_CACHE_HEADER) {
922 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200923 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
924 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
925 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
926 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100927 goto error;
928
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200929 /* Skip response body for HEAD requests */
930 if (si_strm(si)->txn->meth == HTTP_METH_HEAD)
Christopher Fauletf0dd0372019-02-25 11:08:34 +0100931 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100932 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200933 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100934 }
935
936 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200937 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
938 if (len) {
939 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
940 if (ret < len) {
941 si_rx_room_blk(si);
942 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100943 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100944 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200945 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100946 }
947
948 if (appctx->st0 == HTX_CACHE_EOM) {
949 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
950 si_rx_room_blk(si);
951 goto out;
952 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100953 appctx->st0 = HTX_CACHE_END;
954 }
955
956 end:
Christopher Fauletadb36312019-02-25 11:40:49 +0100957 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100958 res->flags |= CF_READ_NULL;
959 si_shutr(si);
960 }
961
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100962 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200963 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +0100964 if (total)
965 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100966 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +0100967
968 /* eat the whole request */
969 if (co_data(req)) {
970 req_htx = htx_from_buf(&req->buf);
971 co_htx_skip(req, req_htx, co_data(req));
972 htx_to_buf(req_htx, &req->buf);
973 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100974 return;
975
976 error:
977 /* Sent and HTTP error 500 */
978 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +0200979 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100980 res->buf.data = b_data(errmsg);
981 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
982 res_htx = htx_from_buf(&res->buf);
983
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200984 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100985 appctx->st0 = HTX_CACHE_END;
986 goto end;
987}
988
989
Christopher Faulet95220e22018-12-07 17:34:39 +0100990static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +0100991{
992 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +0100993 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +0100994
Christopher Faulet95220e22018-12-07 17:34:39 +0100995 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +0100996 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +0100997 goto err;
William Lallemand41db4602017-10-30 11:15:51 +0100998 }
999
1000 /* check if a cache filter was already registered with this cache
1001 * name, if that's the case, must use it. */
1002 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001003 if (fconf->id == cache_store_flt_id) {
1004 cconf = fconf->conf;
1005 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1006 rule->arg.act.p[0] = cconf;
1007 return 1;
1008 }
William Lallemand41db4602017-10-30 11:15:51 +01001009 }
1010 }
1011
Christopher Faulet95220e22018-12-07 17:34:39 +01001012 /* Create the filter cache config */
1013 cconf = calloc(1, sizeof(*cconf));
1014 if (!cconf) {
1015 memprintf(err, "out of memory\n");
1016 goto err;
1017 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001018 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001019 cconf->c.name = strdup(name);
1020 if (!cconf->c.name) {
1021 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001022 goto err;
1023 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001024
William Lallemand41db4602017-10-30 11:15:51 +01001025 /* register a filter to fill the cache buffer */
1026 fconf = calloc(1, sizeof(*fconf));
1027 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001028 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001029 goto err;
1030 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001031 fconf->id = cache_store_flt_id;
1032 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001033 fconf->ops = &cache_ops;
1034 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1035
Christopher Faulet95220e22018-12-07 17:34:39 +01001036 rule->arg.act.p[0] = cconf;
1037 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001038
Christopher Faulet95220e22018-12-07 17:34:39 +01001039 err:
1040 free(cconf);
1041 return 0;
1042}
1043
1044enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1045 struct act_rule *rule, char **err)
1046{
1047 rule->action = ACT_CUSTOM;
1048 rule->action_ptr = http_action_store_cache;
1049
1050 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1051 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001052
Christopher Faulet95220e22018-12-07 17:34:39 +01001053 (*orig_arg)++;
1054 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001055}
1056
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001057/* This produces a sha1 hash of the concatenation of the HTTP method,
1058 * the first occurrence of the Host header followed by the path component
1059 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001060int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001061{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001062 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001063 struct htx *htx = htxbuf(&s->req.buf);
1064 struct htx_sl *sl;
1065 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001066 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001067 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001068 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001069
William Lallemandf528fff2017-11-23 19:43:17 +01001070 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001071 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001072
1073 switch (txn->meth) {
1074 case HTTP_METH_HEAD:
1075 case HTTP_METH_GET:
1076 chunk_memcat(trash, "GET", 3);
1077 break;
1078 default:
1079 return 0;
1080 }
1081
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001082 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001083 uri = htx_sl_req_uri(sl); // whole uri
1084 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001085 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001086
1087 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1088 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1089 * URIs are almost always sent in absolute form with their scheme. In
1090 * this case, the scheme is almost always "https". In order to support
1091 * sharing of cache objects between H1 and H2, we'll hash the absolute
1092 * URI whenever known, or prepend "https://" + the Host header for
1093 * relative URIs. The difference will only appear on absolute HTTP/1
1094 * requests sent to an origin server, which practically is never met in
1095 * the real world so we don't care about the ability to share the same
1096 * key here.URIs are normalized from the absolute URI to an origin form as
1097 * well.
1098 */
1099 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001100 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001101 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1102 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001103 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001104 }
1105
1106 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001107
1108 /* hash everything */
1109 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001110 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001111 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1112
1113 return 1;
1114}
1115
William Lallemand41db4602017-10-30 11:15:51 +01001116enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1117 struct session *sess, struct stream *s, int flags)
1118{
William Lallemand77c11972017-10-31 20:43:01 +01001119
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001120 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001121 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001122 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1123 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001124
Willy Tarreau6905d182019-10-01 17:59:17 +02001125 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1126 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001127 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001128 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001129 txn->flags |= TX_CACHE_IGNORE;
1130
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001131 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001132
Willy Tarreau504455c2017-12-22 17:47:35 +01001133 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1134 return ACT_RET_CONT;
1135
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001136 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001137 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001138
Willy Tarreau504455c2017-12-22 17:47:35 +01001139 if (s->txn->flags & TX_CACHE_IGNORE)
1140 return ACT_RET_CONT;
1141
Willy Tarreaua1214a52018-12-14 14:00:25 +01001142 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001143 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001144 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001145 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001146
William Lallemanda400a3a2017-11-20 19:13:12 +01001147 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001148 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001149 if (res) {
1150 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001151 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1152 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001153 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001154 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001155 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001156 appctx->rule = rule;
1157 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001158 appctx->ctx.cache.next = NULL;
1159 appctx->ctx.cache.sent = 0;
Willy Tarreaua1214a52018-12-14 14:00:25 +01001160
1161 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001162 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001163 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001164 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001165 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001166 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001167 shctx_lock(shctx_ptr(cache));
1168 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1169 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001170 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001171 }
1172 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001173 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001174 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001175}
1176
1177
1178enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1179 struct act_rule *rule, char **err)
1180{
William Lallemand41db4602017-10-30 11:15:51 +01001181 rule->action = ACT_CUSTOM;
1182 rule->action_ptr = http_action_req_cache_use;
1183
Christopher Faulet95220e22018-12-07 17:34:39 +01001184 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001185 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001186
1187 (*orig_arg)++;
1188 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001189}
1190
1191int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1192{
1193 int err_code = 0;
1194
1195 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1196
1197 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001198 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1199 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001200 err_code |= ERR_ALERT | ERR_ABORT;
1201 goto out;
1202 }
1203
1204 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1205 err_code |= ERR_ABORT;
1206 goto out;
1207 }
1208
1209 if (tmp_cache_config == NULL) {
1210 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1211 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001212 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001213 err_code |= ERR_ALERT | ERR_ABORT;
1214 goto out;
1215 }
1216
1217 strlcpy2(tmp_cache_config->id, args[1], 33);
1218 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001219 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1220 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001221 err_code |= ERR_WARN;
1222 }
William Lallemand49b44532017-11-24 18:53:43 +01001223 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001224 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001225 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001226 }
1227 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001228 unsigned long int maxsize;
1229 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001230
1231 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1232 err_code |= ERR_ABORT;
1233 goto out;
1234 }
1235
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001236 maxsize = strtoul(args[1], &err, 10);
1237 if (err == args[1] || *err != '\0') {
1238 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1239 file, linenum, args[1]);
1240 err_code |= ERR_ABORT;
1241 goto out;
1242 }
1243
1244 if (maxsize > (UINT_MAX >> 20)) {
1245 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1246 file, linenum, args[1], UINT_MAX >> 20);
1247 err_code |= ERR_ABORT;
1248 goto out;
1249 }
1250
William Lallemand41db4602017-10-30 11:15:51 +01001251 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001252 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001253 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001254 } else if (strcmp(args[0], "max-age") == 0) {
1255 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1256 err_code |= ERR_ABORT;
1257 goto out;
1258 }
1259
1260 if (!*args[1]) {
1261 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1262 file, linenum, args[0]);
1263 err_code |= ERR_WARN;
1264 }
1265
1266 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001267 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001268 unsigned int maxobjsz;
1269 char *err;
1270
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001271 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1272 err_code |= ERR_ABORT;
1273 goto out;
1274 }
1275
1276 if (!*args[1]) {
1277 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1278 file, linenum, args[0]);
1279 err_code |= ERR_WARN;
1280 }
1281
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001282 maxobjsz = strtoul(args[1], &err, 10);
1283 if (err == args[1] || *err != '\0') {
1284 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1285 file, linenum, args[1]);
1286 err_code |= ERR_ABORT;
1287 goto out;
1288 }
1289 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001290 }
1291 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001292 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001293 err_code |= ERR_ALERT | ERR_FATAL;
1294 goto out;
1295 }
1296out:
1297 return err_code;
1298}
1299
1300/* once the cache section is parsed */
1301
1302int cfg_post_parse_section_cache()
1303{
William Lallemand41db4602017-10-30 11:15:51 +01001304 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001305
1306 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001307
1308 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001309 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001310 err_code |= ERR_FATAL | ERR_ALERT;
1311 goto out;
1312 }
1313
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001314 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001315 /* Default max. file size is a 256th of the cache size. */
1316 tmp_cache_config->maxobjsz =
1317 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001318 }
1319 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1320 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1321 err_code |= ERR_FATAL | ERR_ALERT;
1322 goto out;
1323 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001324
William Lallemandd1d1e222019-08-28 15:22:49 +02001325 /* add to the list of cache to init and reinit tmp_cache_config
1326 * for next cache section, if any.
1327 */
1328 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1329 tmp_cache_config = NULL;
1330 return err_code;
1331 }
1332out:
1333 free(tmp_cache_config);
1334 tmp_cache_config = NULL;
1335 return err_code;
1336
1337}
1338
1339int post_check_cache()
1340{
1341 struct proxy *px;
1342 struct cache *back, *cache_config, *cache;
1343 struct shared_context *shctx;
1344 int ret_shctx;
1345 int err_code = 0;
1346
1347 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1348
1349 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1350 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001351
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001352 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001353 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001354 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001355 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001356 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001357
1358 err_code |= ERR_FATAL | ERR_ALERT;
1359 goto out;
1360 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001361 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001362 /* the cache structure is stored in the shctx and added to the
1363 * caches list, we can remove the entry from the caches_config
1364 * list */
1365 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001366 cache = (struct cache *)shctx->data;
1367 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001368 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001369 LIST_DEL(&cache_config->list);
1370 free(cache_config);
1371
1372 /* Find all references for this cache in the existing filters
1373 * (over all proxies) and reference it in matching filters.
1374 */
1375 for (px = proxies_list; px; px = px->next) {
1376 struct flt_conf *fconf;
1377 struct cache_flt_conf *cconf;
1378
1379 list_for_each_entry(fconf, &px->filter_configs, list) {
1380 if (fconf->id != cache_store_flt_id)
1381 continue;
1382
1383 cconf = fconf->conf;
1384 if (!strcmp(cache->id, cconf->c.name)) {
1385 free(cconf->c.name);
1386 cconf->c.cache = cache;
1387 break;
1388 }
1389 }
1390 }
William Lallemand41db4602017-10-30 11:15:51 +01001391 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001392
William Lallemand41db4602017-10-30 11:15:51 +01001393out:
William Lallemand41db4602017-10-30 11:15:51 +01001394 return err_code;
1395
William Lallemand41db4602017-10-30 11:15:51 +01001396}
1397
William Lallemand41db4602017-10-30 11:15:51 +01001398struct flt_ops cache_ops = {
1399 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001400 .check = cache_store_check,
1401 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001402
William Lallemand4da3f8a2017-10-31 14:33:34 +01001403 /* Handle channels activity */
1404 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001405 .channel_end_analyze = cache_store_chn_end_analyze,
Christopher Faulet839791a2019-01-07 16:12:07 +01001406 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001407
1408 /* Filter HTTP requests and responses */
1409 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001410 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001411 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001412};
1413
Christopher Faulet99a17a22018-12-11 09:18:27 +01001414
1415
1416static int
1417parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1418 struct flt_conf *fconf, char **err, void *private)
1419{
1420 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001421 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001422 char *name = NULL;
1423 int pos = *cur_arg;
1424
1425 /* Get the cache filter name*/
1426 if (!strcmp(args[pos], "cache")) {
1427 if (!*args[pos + 1]) {
1428 memprintf(err, "%s : expects an <id> argument", args[pos]);
1429 goto error;
1430 }
1431 name = strdup(args[pos + 1]);
1432 if (!name) {
1433 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1434 goto error;
1435 }
1436 pos += 2;
1437 }
1438
1439 /* Check if an implicit filter with the same name already exists. If so,
1440 * we remove the implicit filter to use the explicit one. */
1441 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1442 if (f->id != cache_store_flt_id)
1443 continue;
1444
1445 cconf = f->conf;
1446 if (strcmp(name, cconf->c.name)) {
1447 cconf = NULL;
1448 continue;
1449 }
1450
1451 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1452 cconf = NULL;
1453 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1454 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01001455 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001456 }
1457
1458 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1459 LIST_DEL(&f->list);
1460 free(f);
1461 free(name);
1462 break;
1463 }
1464
1465 /* No implicit cache filter found, create configuration for the explicit one */
1466 if (!cconf) {
1467 cconf = calloc(1, sizeof(*cconf));
1468 if (!cconf) {
1469 memprintf(err, "%s: out of memory", args[*cur_arg]);
1470 goto error;
1471 }
1472 cconf->c.name = name;
1473 }
1474
1475 cconf->flags = 0;
1476 fconf->id = cache_store_flt_id;
1477 fconf->conf = cconf;
1478 fconf->ops = &cache_ops;
1479
1480 *cur_arg = pos;
1481 return 0;
1482
1483 error:
1484 free(name);
1485 free(cconf);
1486 return -1;
1487}
1488
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001489static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001490{
1491 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1492 return 1;
1493
1494 return 0;
1495}
1496
1497static int cli_io_handler_show_cache(struct appctx *appctx)
1498{
1499 struct cache* cache = appctx->ctx.cli.p0;
1500 struct stream_interface *si = appctx->owner;
1501
William Lallemand1f49a362017-11-21 20:01:26 +01001502 if (cache == NULL) {
1503 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1504 }
1505
1506 list_for_each_entry_from(cache, &caches, list) {
1507 struct eb32_node *node = NULL;
1508 unsigned int next_key;
1509 struct cache_entry *entry;
1510
William Lallemand1f49a362017-11-21 20:01:26 +01001511 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001512 if (!next_key) {
1513 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1514 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001515 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001516 return 0;
1517 }
1518 }
William Lallemand1f49a362017-11-21 20:01:26 +01001519
1520 appctx->ctx.cli.p0 = cache;
1521
1522 while (1) {
1523
1524 shctx_lock(shctx_ptr(cache));
1525 node = eb32_lookup_ge(&cache->entries, next_key);
1526 if (!node) {
1527 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001528 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001529 break;
1530 }
1531
1532 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001533 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 +01001534
1535 next_key = node->key + 1;
1536 appctx->ctx.cli.i0 = next_key;
1537
1538 shctx_unlock(shctx_ptr(cache));
1539
1540 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001541 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001542 return 0;
1543 }
1544 }
1545
1546 }
1547
1548 return 1;
1549
1550}
1551
Christopher Faulet99a17a22018-12-11 09:18:27 +01001552/* Declare the filter parser for "cache" keyword */
1553static struct flt_kw_list filter_kws = { "CACHE", { }, {
1554 { "cache", parse_cache_flt, NULL },
1555 { NULL, NULL, NULL },
1556 }
1557};
1558
1559INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1560
William Lallemand1f49a362017-11-21 20:01:26 +01001561static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001562 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1563 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001564}};
1565
Willy Tarreau0108d902018-11-25 19:14:37 +01001566INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001567
William Lallemand41db4602017-10-30 11:15:51 +01001568static struct action_kw_list http_res_actions = {
1569 .kw = {
1570 { "cache-store", parse_cache_store },
1571 { NULL, NULL }
1572 }
1573};
1574
Willy Tarreau0108d902018-11-25 19:14:37 +01001575INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1576
William Lallemand41db4602017-10-30 11:15:51 +01001577static struct action_kw_list http_req_actions = {
1578 .kw = {
1579 { "cache-use", parse_cache_use },
1580 { NULL, NULL }
1581 }
1582};
1583
Willy Tarreau0108d902018-11-25 19:14:37 +01001584INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1585
Willy Tarreau2231b632019-03-29 18:26:52 +01001586struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001587 .obj_type = OBJ_TYPE_APPLET,
1588 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001589 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001590 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001591};
1592
Willy Tarreaue6552512018-11-26 11:33:13 +01001593/* config parsers for this section */
1594REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02001595REGISTER_POST_CHECK(post_check_cache);