blob: 02bb0e048d226e9f24516894e505f108facd6624 [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
William Lallemand18f133a2017-11-08 11:25:15 +0100371 /* does not cache if Content-Length unknown */
372 if (!(msg->flags & HTTP_MSGF_CNT_LEN))
373 goto out;
374
William Lallemand4da3f8a2017-10-31 14:33:34 +0100375 /* cache only GET method */
376 if (txn->meth != HTTP_METH_GET)
377 goto out;
378
379 /* cache only 200 status code */
380 if (txn->status != 200)
381 goto out;
382
383 /* Does not manage Vary at the moment. We will need a secondary key later for that */
384 ctx.idx = 0;
385 if (http_find_header2("Vary", 4, txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx))
386 goto out;
387
388 /* we need to put this flag before using check_response_for_cacheability */
389 txn->flags |= TX_CACHEABLE;
390
391 if (txn->status != 101)
392 check_response_for_cacheability(s, &s->res);
393
394 if (!(txn->flags & TX_CACHEABLE))
395 goto out;
396
397 if ((msg->eoh + msg->body_len) > (global.tune.bufsize - global.tune.maxrewrite))
398 goto out;
399
400 shctx_lock(shctx);
401
402 first = shctx_row_reserve_hot(shctx, sizeof(struct cache_entry) + msg->eoh + msg->body_len);
403 if (!first) {
404 shctx_unlock(shctx);
405 goto out;
406 }
407 shctx_unlock(shctx);
408
409 /* reserve space for the cache_entry structure */
410 first->len = sizeof(struct cache_entry);
411
412 /* cache the headers in a http action because it allows to chose what
413 * to cache, for example you might want to cache a response before
414 * modifying some HTTP headers, or on the contrary after modifying
415 * those headers.
416 */
417
418 /* does not need to be locked because it's in the "hot" list,
419 * copy the headers */
420 if (shctx_row_data_append(shctx, first, (unsigned char *)s->res.buf->p, msg->eoh) < 0)
421 goto out;
422
423 /* register the buffer in the filter ctx for filling it with data*/
424 if (!LIST_ISEMPTY(&s->strm_flt.filters)) {
425 list_for_each_entry(filter, &s->strm_flt.filters, list) {
426 if (filter->config->id == cache_store_flt_id &&
427 filter->config->conf == rule->arg.act.p[0]) {
428 if (filter->ctx) {
429 struct cache_st *cache_ctx = filter->ctx;
430
431 cache_ctx->first_block = first;
432 object = (struct cache_entry *)first->data;
433
434 object->eb.key = hash_djb2(txn->uri, strlen(txn->uri));
435 /* Insert the node later on caching success */
436
437 shctx_lock(shctx);
438 if (entry_exist((struct cache *)rule->arg.act.p[0], object)) {
439 shctx_unlock(shctx);
440 if (filter->ctx) {
441 pool_free2(pool2_cache_st, filter->ctx);
442 filter->ctx = NULL;
443 }
444 goto out;
445 }
446 shctx_unlock(shctx);
447
448 /* store latest value and expiration time */
449 object->latest_validation = now.tv_sec;
450 object->expire = now.tv_sec + http_calc_maxage(s);
451
452 }
453 return ACT_RET_CONT;
454 }
455 }
456 }
457
458out:
459 /* if does not cache */
460 if (first) {
461 shctx_lock(shctx);
462 shctx_row_dec_hot(shctx, first);
463 shctx_unlock(shctx);
464 }
465
William Lallemand41db4602017-10-30 11:15:51 +0100466 return ACT_RET_CONT;
467}
468
William Lallemand77c11972017-10-31 20:43:01 +0100469#define HTTP_CACHE_INIT 0
470#define HTTP_CACHE_FWD 1
471#define HTTP_CACHE_END 2
472
473static void http_cache_io_handler(struct appctx *appctx)
474{
475 struct stream_interface *si = appctx->owner;
476 struct channel *res = si_ic(si);
477 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
478 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
479 struct shared_context *shctx = shctx_ptr(cache);
480 struct shared_block *first = block_ptr(cache_ptr);
481
482 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
483 goto out;
484
485 /* Check if the input buffer is avalaible. */
486 if (res->buf->size == 0) {
487 si_applet_cant_put(si);
488 goto out;
489 }
490
491 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
492 appctx->st0 = HTTP_CACHE_END;
493
494 /* buffer are aligned there, should be fine */
495 if (appctx->st0 == HTTP_CACHE_INIT) {
496 int len = first->len - sizeof(struct cache_entry);
497 if ((shctx_row_data_get(shctx, first, (unsigned char *)bi_end(res->buf), sizeof(struct cache_entry), len)) != 0) {
498 fprintf(stderr, "cache error too big: %d\n", first->len - (int)sizeof(struct cache_entry));
499 si_applet_cant_put(si);
500 goto out;
501 }
502 res->buf->i += len;
503 res->total += len;
504 appctx->st0 = HTTP_CACHE_FWD;
505 }
506
507 if (appctx->st0 == HTTP_CACHE_FWD) {
508 /* eat the whole request */
509 co_skip(si_oc(si), si_ob(si)->o); // NOTE: when disabled does not repport the correct status code
510 res->flags |= CF_READ_NULL;
511 si_shutr(si);
512 }
513
514 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
515 si_shutw(si);
516out:
517 ;
518}
519
William Lallemand41db4602017-10-30 11:15:51 +0100520enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
521 struct act_rule *rule, char **err)
522{
523 struct flt_conf *fconf;
524 int cur_arg = *orig_arg;
525 rule->action = ACT_CUSTOM;
526 rule->action_ptr = http_action_store_cache;
527
528 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
529 memprintf(err, "expects a cache name");
530 return ACT_RET_PRS_ERR;
531 }
532
533 /* check if a cache filter was already registered with this cache
534 * name, if that's the case, must use it. */
535 list_for_each_entry(fconf, &proxy->filter_configs, list) {
536 if (fconf->id == cache_store_flt_id && !strcmp((char *)fconf->conf, args[cur_arg])) {
537 rule->arg.act.p[0] = fconf->conf;
538 (*orig_arg)++;
539 /* filter already registered */
540 return ACT_RET_PRS_OK;
541 }
542 }
543
544 rule->arg.act.p[0] = strdup(args[cur_arg]);
545 if (!rule->arg.act.p[0]) {
546 Alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
547 err++;
548 goto err;
549 }
550 /* register a filter to fill the cache buffer */
551 fconf = calloc(1, sizeof(*fconf));
552 if (!fconf) {
553 Alert("config: %s '%s': out of memory\n",
554 proxy_type_str(proxy), proxy->id);
555 err++;
556 goto err;
557 }
558 fconf->id = cache_store_flt_id;
559 fconf->conf = rule->arg.act.p[0]; /* store the proxy name */
560 fconf->ops = &cache_ops;
561 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
562
563 (*orig_arg)++;
564
565 return ACT_RET_PRS_OK;
566
567err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100568 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100569}
570
571
572enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
573 struct session *sess, struct stream *s, int flags)
574{
William Lallemand77c11972017-10-31 20:43:01 +0100575
576 struct cache_entry search_entry;
577 struct cache_entry *res;
578
579 struct cache *cache = (struct cache *)rule->arg.act.p[0];
580
581 search_entry.eb.key = hash_djb2(s->txn->uri, strlen(s->txn->uri));
582 res = entry_exist(cache, &search_entry);
583 if (res) {
584 struct appctx *appctx;
585
586 s->target = &http_cache_applet.obj_type;
587 if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
588 appctx->st0 = HTTP_CACHE_INIT;
589 appctx->rule = rule;
590 appctx->ctx.cache.entry = res;
Olivier Houchardfccf8402017-11-01 14:04:02 +0100591 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +0100592 } else {
Olivier Houchardfccf8402017-11-01 14:04:02 +0100593 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +0100594 }
595 }
Olivier Houchardfccf8402017-11-01 14:04:02 +0100596 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +0100597}
598
599
600enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
601 struct act_rule *rule, char **err)
602{
603 int cur_arg = *orig_arg;
604
605 rule->action = ACT_CUSTOM;
606 rule->action_ptr = http_action_req_cache_use;
607
608 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
609 memprintf(err, "expects a cache name");
610 return ACT_RET_PRS_ERR;
611 }
612
613 rule->arg.act.p[0] = strdup(args[cur_arg]);
614 if (!rule->arg.act.p[0]) {
615 Alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
616 err++;
617 goto err;
618 }
619
620 (*orig_arg)++;
621 return ACT_RET_PRS_OK;
622
623err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100624 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100625
626}
627
628int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
629{
630 int err_code = 0;
631
632 if (strcmp(args[0], "cache") == 0) { /* new cache section */
633
634 if (!*args[1]) {
635 Alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
636 file, linenum, args[0]);
637 err_code |= ERR_ALERT | ERR_ABORT;
638 goto out;
639 }
640
641 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
642 err_code |= ERR_ABORT;
643 goto out;
644 }
645
646 if (tmp_cache_config == NULL) {
647 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
648 if (!tmp_cache_config) {
649 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
650 err_code |= ERR_ALERT | ERR_ABORT;
651 goto out;
652 }
653
654 strlcpy2(tmp_cache_config->id, args[1], 33);
655 if (strlen(args[1]) > 32) {
656 Warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
657 file, linenum, tmp_cache_config->id);
658 err_code |= ERR_WARN;
659 }
660
661 tmp_cache_config->maxblocks = 0;
662 }
663 } else if (strcmp(args[0], "total-max-size") == 0) {
664 int maxsize;
665
666 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
667 err_code |= ERR_ABORT;
668 goto out;
669 }
670
671 /* size in megabytes */
672 maxsize = atoi(args[1]) * 1024 * 1024 / CACHE_BLOCKSIZE;
673 tmp_cache_config->maxblocks = maxsize;
674
675 } else if (*args[0] != 0) {
676 Alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
677 err_code |= ERR_ALERT | ERR_FATAL;
678 goto out;
679 }
680out:
681 return err_code;
682}
683
684/* once the cache section is parsed */
685
686int cfg_post_parse_section_cache()
687{
688 struct shared_context *shctx;
689 int err_code = 0;
690 int ret_shctx;
691
692 if (tmp_cache_config) {
693 struct cache *cache;
694
695 if (tmp_cache_config->maxblocks <= 0) {
696 Alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
697 err_code |= ERR_FATAL | ERR_ALERT;
698 goto out;
699 }
700
701 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100702
William Lallemand41db4602017-10-30 11:15:51 +0100703 if (ret_shctx < 0) {
704 if (ret_shctx == SHCTX_E_INIT_LOCK)
705 Alert("Unable to initialize the lock for the cache.\n");
706 else
707 Alert("Unable to allocate cache.\n");
708
709 err_code |= ERR_FATAL | ERR_ALERT;
710 goto out;
711 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100712
William Lallemand41db4602017-10-30 11:15:51 +0100713 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
714 cache = (struct cache *)shctx->data;
715 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +0100716 LIST_ADDQ(&caches, &cache->list);
717 }
718out:
719 free(tmp_cache_config);
720 tmp_cache_config = NULL;
721 return err_code;
722
723}
724
725/*
726 * Resolve the cache name to a pointer once the file is completely read.
727 */
728int cfg_cache_postparser()
729{
730 struct act_rule *hresrule, *hrqrule;
731 void *cache_ptr;
732 struct cache *cache;
733 struct proxy *curproxy = NULL;
734 int err = 0;
735 struct flt_conf *fconf;
736
737 for (curproxy = proxy; curproxy; curproxy = curproxy->next) {
738
739 /* resolve the http response cache name to a ptr in the action rule */
740 list_for_each_entry(hresrule, &curproxy->http_res_rules, list) {
741 if (hresrule->action != ACT_CUSTOM ||
742 hresrule->action_ptr != http_action_store_cache)
743 continue;
744
745 cache_ptr = hresrule->arg.act.p[0];
746
747 list_for_each_entry(cache, &caches, list) {
748 if (!strcmp(cache->id, cache_ptr)) {
749 /* don't free there, it's still used in the filter conf */
750 cache_ptr = cache;
751 break;
752 }
753 }
754
755 if (cache_ptr == hresrule->arg.act.p[0]) {
756 Alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n",
757 curproxy->id, (char *)hresrule->arg.act.p[0]);
758 err++;
759 }
760
761 hresrule->arg.act.p[0] = cache_ptr;
762 }
763
764 /* resolve the http request cache name to a ptr in the action rule */
765 list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) {
766 if (hrqrule->action != ACT_CUSTOM ||
767 hrqrule->action_ptr != http_action_req_cache_use)
768 continue;
769
770 cache_ptr = hrqrule->arg.act.p[0];
771
772 list_for_each_entry(cache, &caches, list) {
773 if (!strcmp(cache->id, cache_ptr)) {
774 free(cache_ptr);
775 cache_ptr = cache;
776 break;
777 }
778 }
779
780 if (cache_ptr == hrqrule->arg.act.p[0]) {
781 Alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n",
782 curproxy->id, (char *)hrqrule->arg.act.p[0]);
783 err++;
784 }
785
786 hrqrule->arg.act.p[0] = cache_ptr;
787 }
788
789 /* resolve the cache name to a ptr in the filter config */
790 list_for_each_entry(fconf, &curproxy->filter_configs, list) {
791
William Lallemand9c54c532017-11-02 16:38:42 +0100792 if (fconf->id != cache_store_flt_id)
793 continue;
794
William Lallemand41db4602017-10-30 11:15:51 +0100795 cache_ptr = fconf->conf;
796
797 list_for_each_entry(cache, &caches, list) {
798 if (!strcmp(cache->id, cache_ptr)) {
799 /* there can be only one filter per cache, so we free it there */
800 free(cache_ptr);
801 cache_ptr = cache;
802 break;
803 }
804 }
805
806 if (cache_ptr == fconf->conf) {
807 Alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
808 curproxy->id, (char *)fconf->conf);
809 err++;
810 }
811 fconf->conf = cache_ptr;
812 }
813 }
814 return err;
815}
816
817
818struct flt_ops cache_ops = {
819 .init = cache_store_init,
820
William Lallemand4da3f8a2017-10-31 14:33:34 +0100821 /* Handle channels activity */
822 .channel_start_analyze = cache_store_chn_start_analyze,
823
824 /* Filter HTTP requests and responses */
825 .http_headers = cache_store_http_headers,
826 .http_end = cache_store_http_end,
827
828 .http_forward_data = cache_store_http_forward_data,
829
William Lallemand41db4602017-10-30 11:15:51 +0100830};
831
832static struct action_kw_list http_res_actions = {
833 .kw = {
834 { "cache-store", parse_cache_store },
835 { NULL, NULL }
836 }
837};
838
839static struct action_kw_list http_req_actions = {
840 .kw = {
841 { "cache-use", parse_cache_use },
842 { NULL, NULL }
843 }
844};
845
846struct applet http_cache_applet = {
847 .obj_type = OBJ_TYPE_APPLET,
848 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +0100849 .fct = http_cache_io_handler,
William Lallemand41db4602017-10-30 11:15:51 +0100850 .release = NULL,
851};
852
853__attribute__((constructor))
854static void __cache_init(void)
855{
856 cfg_register_section("cache", cfg_parse_cache, cfg_post_parse_section_cache);
857 cfg_register_postparser("cache", cfg_cache_postparser);
858 http_res_keywords_register(&http_res_actions);
859 http_req_keywords_register(&http_req_actions);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100860 pool2_cache_st = create_pool("cache_st", sizeof(struct cache_st), MEM_F_SHARED);
William Lallemand41db4602017-10-30 11:15:51 +0100861}
862