blob: 15e221012d3d03b66f30a26fc1b5591d63c0c372 [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>
14
15#include <proto/channel.h>
16#include <proto/proxy.h>
17#include <proto/hdr_idx.h>
18#include <proto/filters.h>
19#include <proto/proto_http.h>
20#include <proto/log.h>
21#include <proto/stream.h>
22#include <proto/stream_interface.h>
23#include <proto/shctx.h>
24
25#include <types/action.h>
26#include <types/filters.h>
27#include <types/proxy.h>
28#include <types/shctx.h>
29
30#include <common/cfgparse.h>
31#include <common/hash.h>
32
33/* flt_cache_store */
34
35static const char *cache_store_flt_id = "cache store filter";
36
William Lallemand4da3f8a2017-10-31 14:33:34 +010037static struct pool_head *pool2_cache_st = NULL;
38
William Lallemand41db4602017-10-30 11:15:51 +010039struct applet http_cache_applet;
40
41struct flt_ops cache_ops;
42
43struct cache {
44 char id[33]; /* cache name */
45 unsigned int maxage; /* max-age */
46 unsigned int maxblocks;
47 struct list list; /* cache linked list */
48 struct eb_root entries; /* head of cache entries based on keys */
49};
50
51/*
52 * cache ctx for filters
53 */
54struct cache_st {
55 int hdrs_len;
56 struct shared_block *first_block;
57};
58
59struct cache_entry {
60 unsigned int latest_validation; /* latest validation date */
61 unsigned int expire; /* expiration date */
62 struct eb32_node eb; /* ebtree node used to hold the cache object */
63 unsigned char data[0];
64};
65
66#define CACHE_BLOCKSIZE 1024
67
68static struct list caches = LIST_HEAD_INIT(caches);
69static struct cache *tmp_cache_config = NULL;
70
William Lallemand4da3f8a2017-10-31 14:33:34 +010071struct cache_entry *entry_exist(struct cache *cache, struct cache_entry *new_entry)
72{
73 struct eb32_node *node;
74 struct cache_entry *entry;
75
76 node = eb32_lookup(&cache->entries, new_entry->eb.key);
77 if (!node)
78 return NULL;
79
80 entry = eb32_entry(node, struct cache_entry, eb);
81 if (entry->expire > now.tv_sec)
82 return entry;
83 else
84 eb32_delete(node);
85 return NULL;
86
87}
88
89static inline struct shared_context *shctx_ptr(struct cache *cache)
90{
91 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
92}
93
William Lallemand77c11972017-10-31 20:43:01 +010094static inline struct shared_block *block_ptr(struct cache_entry *entry)
95{
96 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
97}
98
99
100
William Lallemand41db4602017-10-30 11:15:51 +0100101static int
102cache_store_init(struct proxy *px, struct flt_conf *f1conf)
103{
104 return 0;
105}
106
William Lallemand4da3f8a2017-10-31 14:33:34 +0100107static int
108cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
109{
110 if (!(chn->flags & CF_ISRESP))
111 return 1;
112
113 if (filter->ctx == NULL) {
114 struct cache_st *st;
115
116 st = pool_alloc_dirty(pool2_cache_st);
117 if (st == NULL)
118 return -1;
119
120 st->hdrs_len = 0;
121 st->first_block = NULL;
122 filter->ctx = st;
123 }
124
125 register_data_filter(s, chn, filter);
126
127 return 1;
128}
129
130static int
131cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
132{
133 struct cache_st *st = filter->ctx;
134
135 /* end of headers, exclude the final \r\n allow to forward the final
136 * \r\n in the data filter */
137 if (!(msg->chn->flags & CF_ISRESP) || !st)
138 return 1;
139
140 st->hdrs_len = msg->eoh;
141
142 return 1;
143}
144
145static int
146cache_store_http_forward_data(struct stream *s, struct filter *filter,
147 struct http_msg *msg, unsigned int len)
148{
149 struct cache_st *st = filter->ctx;
150 struct shared_context *shctx = shctx_ptr((struct cache *)filter->config->conf);
151 int ret;
152
153 /*
154 * We need to skip the HTTP headers first, because we saved them in the
155 * http-response action.
156 */
157 if (!(msg->chn->flags & CF_ISRESP) || !st)
158 return len;
159
160 if (!len) {
161 /* Nothing to foward */
162 ret = len;
163 }
164 else if (st->hdrs_len > len) {
165 /* Forward part of headers */
166 ret = len;
167 st->hdrs_len -= len;
168 }
169 else if (st->hdrs_len > 0) {
170 /* Forward remaining headers */
171 ret = st->hdrs_len;
172 st->hdrs_len = 0;
173 }
174 else {
175 /* Forward trailers data */
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100176 if (filter->ctx && st->first_block) {
177 /* disable buffering if too much data (never greater than a buffer size */
178 if (len > global.tune.bufsize - global.tune.maxrewrite - st->first_block->len) {
179 filter->ctx = NULL; /* disable cache */
180 shctx_lock(shctx);
181 shctx_row_dec_hot(shctx, st->first_block);
182 shctx_unlock(shctx);
183 pool_free2(pool2_cache_st, st);
184 ret = 0;
185 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100186
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100187 int blen;
188 blen = shctx_row_data_append(shctx,
189 st->first_block,
190 (unsigned char *)bi_ptr(msg->chn->buf),
191 MIN(bi_contig_data(msg->chn->buf), len));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100192
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100193 ret = MIN(bi_contig_data(msg->chn->buf), len) + blen;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100194 }
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100195 } else {
196 ret = len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100197 }
198 }
199
200 if ((ret != len) ||
201 (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret))
202 task_wakeup(s->task, TASK_WOKEN_MSG);
203
204 return ret;
205}
206
207static int
208cache_store_http_end(struct stream *s, struct filter *filter,
209 struct http_msg *msg)
210{
211 struct cache_st *st = filter->ctx;
212 struct cache *cache = filter->config->conf;
213 struct shared_context *shctx = shctx_ptr(cache);
214 struct cache_entry *object;
215
216 if (!(msg->chn->flags & CF_ISRESP))
217 return 1;
218
219 if (st && st->first_block) {
220
221 object = (struct cache_entry *)st->first_block->data;
222
223 /* does not need to test if the insertion worked, if it
224 * doesn't, the blocks will be reused anyway */
225
226 shctx_lock(shctx);
227 eb32_insert(&cache->entries, &object->eb);
228 shctx_unlock(shctx);
229
230 /* remove from the hotlist */
231 shctx_lock(shctx);
232 shctx_row_dec_hot(shctx, st->first_block);
233 shctx_unlock(shctx);
234
235 }
236 if (st) {
237 pool_free2(pool2_cache_st, st);
238 filter->ctx = NULL;
239 }
240
241 return 1;
242}
243
244 /*
245 * This intends to be used when checking HTTP headers for some
246 * word=value directive. Return a pointer to the first character of value, if
247 * the word was not found or if there wasn't any value assigned ot it return NULL
248 */
249char *directive_value(const char *sample, int slen, const char *word, int wlen)
250{
251 int st = 0;
252
253 if (slen < wlen)
254 return 0;
255
256 while (wlen) {
257 char c = *sample ^ *word;
258 if (c && c != ('A' ^ 'a'))
259 return NULL;
260 sample++;
261 word++;
262 slen--;
263 wlen--;
264 }
265
266 while (slen) {
267 if (st == 0) {
268 if (*sample != '=')
269 return NULL;
270 sample++;
271 slen--;
272 st = 1;
273 continue;
274 } else {
275 return (char *)sample;
276 }
277 }
278
279 return NULL;
280}
281
282/*
283 * Return the maxage in seconds of an HTTP response.
284 * Compute the maxage using either:
285 * - the assigned max-age of the cache
286 * - the s-maxage directive
287 * - the max-age directive
288 * - (Expires - Data) headers
289 * - the default-max-age of the cache
290 *
291 */
292int http_calc_maxage(struct stream *s)
293{
294 struct http_txn *txn = s->txn;
295 struct hdr_ctx ctx;
296
297 int smaxage = -1;
298 int maxage = -1;
299
300
301 /* TODO: forced maxage configuration */
302
303 ctx.idx = 0;
304
305 /* loop on the Cache-Control values */
306 while (http_find_header2("Cache-Control", 13, s->res.buf->p, &txn->hdr_idx, &ctx)) {
307 char *directive = ctx.line + ctx.val;
308 char *value;
309
310 value = directive_value(directive, ctx.vlen, "s-maxage", 8);
311 if (value) {
312 struct chunk *chk = get_trash_chunk();
313
314 chunk_strncat(chk, value, ctx.vlen - 8 + 1);
315 chunk_strncat(chk, "", 1);
316 maxage = atoi(chk->str);
317 }
318
319 value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
320 if (value) {
321 struct chunk *chk = get_trash_chunk();
322
323 chunk_strncat(chk, value, ctx.vlen - 7 + 1);
324 chunk_strncat(chk, "", 1);
325 smaxage = atoi(chk->str);
326 }
327 }
328
329 /* TODO: Expires - Data */
330
331
332 if (smaxage > 0)
333 return smaxage;
334
335 if (maxage > 0)
336 return maxage;
337
338 /* TODO: return default value */
339
340 return 60;
341
342}
343
344
William Lallemand41db4602017-10-30 11:15:51 +0100345/*
346 * This fonction will store the headers of the response in a buffer and then
347 * register a filter to store the data
348 */
349enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
350 struct session *sess, struct stream *s, int flags)
351{
William Lallemand4da3f8a2017-10-31 14:33:34 +0100352 struct http_txn *txn = s->txn;
353 struct http_msg *msg = &txn->rsp;
354 struct filter *filter;
355 struct hdr_ctx ctx;
356 struct shared_block *first = NULL;
357 struct shared_context *shctx = shctx_ptr((struct cache *)rule->arg.act.p[0]);
358 struct cache_entry *object;
359
360
361 /* Don't cache if the response came from a cache */
362 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
363 s->target == &http_cache_applet.obj_type) {
364 goto out;
365 }
366
367 /* cache only HTTP/1.1 */
368 if (!(txn->req.flags & HTTP_MSGF_VER_11))
369 goto out;
370
371 /* cache only GET method */
372 if (txn->meth != HTTP_METH_GET)
373 goto out;
374
375 /* cache only 200 status code */
376 if (txn->status != 200)
377 goto out;
378
379 /* Does not manage Vary at the moment. We will need a secondary key later for that */
380 ctx.idx = 0;
381 if (http_find_header2("Vary", 4, txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx))
382 goto out;
383
384 /* we need to put this flag before using check_response_for_cacheability */
385 txn->flags |= TX_CACHEABLE;
386
387 if (txn->status != 101)
388 check_response_for_cacheability(s, &s->res);
389
390 if (!(txn->flags & TX_CACHEABLE))
391 goto out;
392
393 if ((msg->eoh + msg->body_len) > (global.tune.bufsize - global.tune.maxrewrite))
394 goto out;
395
396 shctx_lock(shctx);
397
398 first = shctx_row_reserve_hot(shctx, sizeof(struct cache_entry) + msg->eoh + msg->body_len);
399 if (!first) {
400 shctx_unlock(shctx);
401 goto out;
402 }
403 shctx_unlock(shctx);
404
405 /* reserve space for the cache_entry structure */
406 first->len = sizeof(struct cache_entry);
407
408 /* cache the headers in a http action because it allows to chose what
409 * to cache, for example you might want to cache a response before
410 * modifying some HTTP headers, or on the contrary after modifying
411 * those headers.
412 */
413
414 /* does not need to be locked because it's in the "hot" list,
415 * copy the headers */
416 if (shctx_row_data_append(shctx, first, (unsigned char *)s->res.buf->p, msg->eoh) < 0)
417 goto out;
418
419 /* register the buffer in the filter ctx for filling it with data*/
420 if (!LIST_ISEMPTY(&s->strm_flt.filters)) {
421 list_for_each_entry(filter, &s->strm_flt.filters, list) {
422 if (filter->config->id == cache_store_flt_id &&
423 filter->config->conf == rule->arg.act.p[0]) {
424 if (filter->ctx) {
425 struct cache_st *cache_ctx = filter->ctx;
426
427 cache_ctx->first_block = first;
428 object = (struct cache_entry *)first->data;
429
430 object->eb.key = hash_djb2(txn->uri, strlen(txn->uri));
431 /* Insert the node later on caching success */
432
433 shctx_lock(shctx);
434 if (entry_exist((struct cache *)rule->arg.act.p[0], object)) {
435 shctx_unlock(shctx);
436 if (filter->ctx) {
437 pool_free2(pool2_cache_st, filter->ctx);
438 filter->ctx = NULL;
439 }
440 goto out;
441 }
442 shctx_unlock(shctx);
443
444 /* store latest value and expiration time */
445 object->latest_validation = now.tv_sec;
446 object->expire = now.tv_sec + http_calc_maxage(s);
447
448 }
449 return ACT_RET_CONT;
450 }
451 }
452 }
453
454out:
455 /* if does not cache */
456 if (first) {
457 shctx_lock(shctx);
458 shctx_row_dec_hot(shctx, first);
459 shctx_unlock(shctx);
460 }
461
William Lallemand41db4602017-10-30 11:15:51 +0100462 return ACT_RET_CONT;
463}
464
William Lallemand77c11972017-10-31 20:43:01 +0100465#define HTTP_CACHE_INIT 0
466#define HTTP_CACHE_FWD 1
467#define HTTP_CACHE_END 2
468
469static void http_cache_io_handler(struct appctx *appctx)
470{
471 struct stream_interface *si = appctx->owner;
472 struct channel *res = si_ic(si);
473 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
474 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
475 struct shared_context *shctx = shctx_ptr(cache);
476 struct shared_block *first = block_ptr(cache_ptr);
477
478 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
479 goto out;
480
481 /* Check if the input buffer is avalaible. */
482 if (res->buf->size == 0) {
483 si_applet_cant_put(si);
484 goto out;
485 }
486
487 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
488 appctx->st0 = HTTP_CACHE_END;
489
490 /* buffer are aligned there, should be fine */
491 if (appctx->st0 == HTTP_CACHE_INIT) {
492 int len = first->len - sizeof(struct cache_entry);
493 if ((shctx_row_data_get(shctx, first, (unsigned char *)bi_end(res->buf), sizeof(struct cache_entry), len)) != 0) {
494 fprintf(stderr, "cache error too big: %d\n", first->len - (int)sizeof(struct cache_entry));
495 si_applet_cant_put(si);
496 goto out;
497 }
498 res->buf->i += len;
499 res->total += len;
500 appctx->st0 = HTTP_CACHE_FWD;
501 }
502
503 if (appctx->st0 == HTTP_CACHE_FWD) {
504 /* eat the whole request */
505 co_skip(si_oc(si), si_ob(si)->o); // NOTE: when disabled does not repport the correct status code
506 res->flags |= CF_READ_NULL;
507 si_shutr(si);
508 }
509
510 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
511 si_shutw(si);
512out:
513 ;
514}
515
William Lallemand41db4602017-10-30 11:15:51 +0100516enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
517 struct act_rule *rule, char **err)
518{
519 struct flt_conf *fconf;
520 int cur_arg = *orig_arg;
521 rule->action = ACT_CUSTOM;
522 rule->action_ptr = http_action_store_cache;
523
524 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
525 memprintf(err, "expects a cache name");
526 return ACT_RET_PRS_ERR;
527 }
528
529 /* check if a cache filter was already registered with this cache
530 * name, if that's the case, must use it. */
531 list_for_each_entry(fconf, &proxy->filter_configs, list) {
532 if (fconf->id == cache_store_flt_id && !strcmp((char *)fconf->conf, args[cur_arg])) {
533 rule->arg.act.p[0] = fconf->conf;
534 (*orig_arg)++;
535 /* filter already registered */
536 return ACT_RET_PRS_OK;
537 }
538 }
539
540 rule->arg.act.p[0] = strdup(args[cur_arg]);
541 if (!rule->arg.act.p[0]) {
542 Alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
543 err++;
544 goto err;
545 }
546 /* register a filter to fill the cache buffer */
547 fconf = calloc(1, sizeof(*fconf));
548 if (!fconf) {
549 Alert("config: %s '%s': out of memory\n",
550 proxy_type_str(proxy), proxy->id);
551 err++;
552 goto err;
553 }
554 fconf->id = cache_store_flt_id;
555 fconf->conf = rule->arg.act.p[0]; /* store the proxy name */
556 fconf->ops = &cache_ops;
557 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
558
559 (*orig_arg)++;
560
561 return ACT_RET_PRS_OK;
562
563err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100564 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100565}
566
567
568enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
569 struct session *sess, struct stream *s, int flags)
570{
William Lallemand77c11972017-10-31 20:43:01 +0100571
572 struct cache_entry search_entry;
573 struct cache_entry *res;
574
575 struct cache *cache = (struct cache *)rule->arg.act.p[0];
576
577 search_entry.eb.key = hash_djb2(s->txn->uri, strlen(s->txn->uri));
578 res = entry_exist(cache, &search_entry);
579 if (res) {
580 struct appctx *appctx;
581
582 s->target = &http_cache_applet.obj_type;
583 if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
584 appctx->st0 = HTTP_CACHE_INIT;
585 appctx->rule = rule;
586 appctx->ctx.cache.entry = res;
Olivier Houchardfccf8402017-11-01 14:04:02 +0100587 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +0100588 } else {
Olivier Houchardfccf8402017-11-01 14:04:02 +0100589 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +0100590 }
591 }
Olivier Houchardfccf8402017-11-01 14:04:02 +0100592 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +0100593}
594
595
596enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
597 struct act_rule *rule, char **err)
598{
599 int cur_arg = *orig_arg;
600
601 rule->action = ACT_CUSTOM;
602 rule->action_ptr = http_action_req_cache_use;
603
604 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
605 memprintf(err, "expects a cache name");
606 return ACT_RET_PRS_ERR;
607 }
608
609 rule->arg.act.p[0] = strdup(args[cur_arg]);
610 if (!rule->arg.act.p[0]) {
611 Alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
612 err++;
613 goto err;
614 }
615
616 (*orig_arg)++;
617 return ACT_RET_PRS_OK;
618
619err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100620 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100621
622}
623
624int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
625{
626 int err_code = 0;
627
628 if (strcmp(args[0], "cache") == 0) { /* new cache section */
629
630 if (!*args[1]) {
631 Alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
632 file, linenum, args[0]);
633 err_code |= ERR_ALERT | ERR_ABORT;
634 goto out;
635 }
636
637 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
638 err_code |= ERR_ABORT;
639 goto out;
640 }
641
642 if (tmp_cache_config == NULL) {
643 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
644 if (!tmp_cache_config) {
645 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
646 err_code |= ERR_ALERT | ERR_ABORT;
647 goto out;
648 }
649
650 strlcpy2(tmp_cache_config->id, args[1], 33);
651 if (strlen(args[1]) > 32) {
652 Warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
653 file, linenum, tmp_cache_config->id);
654 err_code |= ERR_WARN;
655 }
656
657 tmp_cache_config->maxblocks = 0;
658 }
659 } else if (strcmp(args[0], "total-max-size") == 0) {
660 int maxsize;
661
662 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
663 err_code |= ERR_ABORT;
664 goto out;
665 }
666
667 /* size in megabytes */
668 maxsize = atoi(args[1]) * 1024 * 1024 / CACHE_BLOCKSIZE;
669 tmp_cache_config->maxblocks = maxsize;
670
671 } else if (*args[0] != 0) {
672 Alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
673 err_code |= ERR_ALERT | ERR_FATAL;
674 goto out;
675 }
676out:
677 return err_code;
678}
679
680/* once the cache section is parsed */
681
682int cfg_post_parse_section_cache()
683{
684 struct shared_context *shctx;
685 int err_code = 0;
686 int ret_shctx;
687
688 if (tmp_cache_config) {
689 struct cache *cache;
690
691 if (tmp_cache_config->maxblocks <= 0) {
692 Alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
693 err_code |= ERR_FATAL | ERR_ALERT;
694 goto out;
695 }
696
697 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100698
William Lallemand41db4602017-10-30 11:15:51 +0100699 if (ret_shctx < 0) {
700 if (ret_shctx == SHCTX_E_INIT_LOCK)
701 Alert("Unable to initialize the lock for the cache.\n");
702 else
703 Alert("Unable to allocate cache.\n");
704
705 err_code |= ERR_FATAL | ERR_ALERT;
706 goto out;
707 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100708
William Lallemand41db4602017-10-30 11:15:51 +0100709 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
710 cache = (struct cache *)shctx->data;
711 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +0100712 LIST_ADDQ(&caches, &cache->list);
713 }
714out:
715 free(tmp_cache_config);
716 tmp_cache_config = NULL;
717 return err_code;
718
719}
720
721/*
722 * Resolve the cache name to a pointer once the file is completely read.
723 */
724int cfg_cache_postparser()
725{
726 struct act_rule *hresrule, *hrqrule;
727 void *cache_ptr;
728 struct cache *cache;
729 struct proxy *curproxy = NULL;
730 int err = 0;
731 struct flt_conf *fconf;
732
733 for (curproxy = proxy; curproxy; curproxy = curproxy->next) {
734
735 /* resolve the http response cache name to a ptr in the action rule */
736 list_for_each_entry(hresrule, &curproxy->http_res_rules, list) {
737 if (hresrule->action != ACT_CUSTOM ||
738 hresrule->action_ptr != http_action_store_cache)
739 continue;
740
741 cache_ptr = hresrule->arg.act.p[0];
742
743 list_for_each_entry(cache, &caches, list) {
744 if (!strcmp(cache->id, cache_ptr)) {
745 /* don't free there, it's still used in the filter conf */
746 cache_ptr = cache;
747 break;
748 }
749 }
750
751 if (cache_ptr == hresrule->arg.act.p[0]) {
752 Alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n",
753 curproxy->id, (char *)hresrule->arg.act.p[0]);
754 err++;
755 }
756
757 hresrule->arg.act.p[0] = cache_ptr;
758 }
759
760 /* resolve the http request cache name to a ptr in the action rule */
761 list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) {
762 if (hrqrule->action != ACT_CUSTOM ||
763 hrqrule->action_ptr != http_action_req_cache_use)
764 continue;
765
766 cache_ptr = hrqrule->arg.act.p[0];
767
768 list_for_each_entry(cache, &caches, list) {
769 if (!strcmp(cache->id, cache_ptr)) {
770 free(cache_ptr);
771 cache_ptr = cache;
772 break;
773 }
774 }
775
776 if (cache_ptr == hrqrule->arg.act.p[0]) {
777 Alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n",
778 curproxy->id, (char *)hrqrule->arg.act.p[0]);
779 err++;
780 }
781
782 hrqrule->arg.act.p[0] = cache_ptr;
783 }
784
785 /* resolve the cache name to a ptr in the filter config */
786 list_for_each_entry(fconf, &curproxy->filter_configs, list) {
787
William Lallemand9c54c532017-11-02 16:38:42 +0100788 if (fconf->id != cache_store_flt_id)
789 continue;
790
William Lallemand41db4602017-10-30 11:15:51 +0100791 cache_ptr = fconf->conf;
792
793 list_for_each_entry(cache, &caches, list) {
794 if (!strcmp(cache->id, cache_ptr)) {
795 /* there can be only one filter per cache, so we free it there */
796 free(cache_ptr);
797 cache_ptr = cache;
798 break;
799 }
800 }
801
802 if (cache_ptr == fconf->conf) {
803 Alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
804 curproxy->id, (char *)fconf->conf);
805 err++;
806 }
807 fconf->conf = cache_ptr;
808 }
809 }
810 return err;
811}
812
813
814struct flt_ops cache_ops = {
815 .init = cache_store_init,
816
William Lallemand4da3f8a2017-10-31 14:33:34 +0100817 /* Handle channels activity */
818 .channel_start_analyze = cache_store_chn_start_analyze,
819
820 /* Filter HTTP requests and responses */
821 .http_headers = cache_store_http_headers,
822 .http_end = cache_store_http_end,
823
824 .http_forward_data = cache_store_http_forward_data,
825
William Lallemand41db4602017-10-30 11:15:51 +0100826};
827
828static struct action_kw_list http_res_actions = {
829 .kw = {
830 { "cache-store", parse_cache_store },
831 { NULL, NULL }
832 }
833};
834
835static struct action_kw_list http_req_actions = {
836 .kw = {
837 { "cache-use", parse_cache_use },
838 { NULL, NULL }
839 }
840};
841
842struct applet http_cache_applet = {
843 .obj_type = OBJ_TYPE_APPLET,
844 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +0100845 .fct = http_cache_io_handler,
William Lallemand41db4602017-10-30 11:15:51 +0100846 .release = NULL,
847};
848
849__attribute__((constructor))
850static void __cache_init(void)
851{
852 cfg_register_section("cache", cfg_parse_cache, cfg_post_parse_section_cache);
853 cfg_register_postparser("cache", cfg_cache_postparser);
854 http_res_keywords_register(&http_res_actions);
855 http_req_keywords_register(&http_req_actions);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100856 pool2_cache_st = create_pool("cache_st", sizeof(struct cache_st), MEM_F_SHARED);
William Lallemand41db4602017-10-30 11:15:51 +0100857}
858