blob: 9070e996dcb9aeb0fb9aa2b3a6581b1d0f2241c0 [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>
25#include <proto/hdr_idx.h>
26#include <proto/filters.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020027#include <proto/http_rules.h>
William Lallemand41db4602017-10-30 11:15:51 +010028#include <proto/proto_http.h>
29#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>
37
38/* flt_cache_store */
39
40static const char *cache_store_flt_id = "cache store filter";
41
Willy Tarreaubafbe012017-11-24 17:34:44 +010042static struct pool_head *pool_head_cache_st = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +010043
William Lallemand41db4602017-10-30 11:15:51 +010044struct applet http_cache_applet;
45
46struct flt_ops cache_ops;
47
48struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010049 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010050 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010051 unsigned int maxage; /* max-age */
52 unsigned int maxblocks;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +020053 unsigned int maxobjsz; /* max-object-size */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010054 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010055};
56
57/*
58 * cache ctx for filters
59 */
60struct cache_st {
61 int hdrs_len;
62 struct shared_block *first_block;
63};
64
65struct cache_entry {
66 unsigned int latest_validation; /* latest validation date */
67 unsigned int expire; /* expiration date */
68 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010069 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010070 unsigned char data[0];
71};
72
73#define CACHE_BLOCKSIZE 1024
74
75static struct list caches = LIST_HEAD_INIT(caches);
76static struct cache *tmp_cache_config = NULL;
77
William Lallemandf528fff2017-11-23 19:43:17 +010078struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010079{
80 struct eb32_node *node;
81 struct cache_entry *entry;
82
William Lallemandf528fff2017-11-23 19:43:17 +010083 node = eb32_lookup(&cache->entries, (*(unsigned int *)hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010084 if (!node)
85 return NULL;
86
87 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +010088
89 /* if that's not the right node */
90 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
91 return NULL;
92
William Lallemand08727662017-11-21 20:01:27 +010093 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +010094 return entry;
William Lallemand08727662017-11-21 20:01:27 +010095 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +010096 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +010097 entry->eb.key = 0;
98 }
William Lallemand4da3f8a2017-10-31 14:33:34 +010099 return NULL;
100
101}
102
103static inline struct shared_context *shctx_ptr(struct cache *cache)
104{
105 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
106}
107
William Lallemand77c11972017-10-31 20:43:01 +0100108static inline struct shared_block *block_ptr(struct cache_entry *entry)
109{
110 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
111}
112
113
114
William Lallemand41db4602017-10-30 11:15:51 +0100115static int
116cache_store_init(struct proxy *px, struct flt_conf *f1conf)
117{
118 return 0;
119}
120
William Lallemand4da3f8a2017-10-31 14:33:34 +0100121static int
122cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
123{
124 if (!(chn->flags & CF_ISRESP))
125 return 1;
126
127 if (filter->ctx == NULL) {
128 struct cache_st *st;
129
Willy Tarreaubafbe012017-11-24 17:34:44 +0100130 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100131 if (st == NULL)
132 return -1;
133
134 st->hdrs_len = 0;
135 st->first_block = NULL;
136 filter->ctx = st;
137 }
138
139 register_data_filter(s, chn, filter);
140
141 return 1;
142}
143
144static int
William Lallemand49dc0482017-11-24 14:33:54 +0100145cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
146{
147 struct cache_st *st = filter->ctx;
148 struct cache *cache = filter->config->conf;
149 struct shared_context *shctx = shctx_ptr(cache);
150
151 if (!(chn->flags & CF_ISRESP))
152 return 1;
153
154 /* Everything should be released in the http_end filter, but we need to do it
155 * there too, in case of errors */
156
157 if (st && st->first_block) {
158
159 shctx_lock(shctx);
160 shctx_row_dec_hot(shctx, st->first_block);
161 shctx_unlock(shctx);
162
163 }
164 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100165 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100166 filter->ctx = NULL;
167 }
168
169 return 1;
170}
171
172
173static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100174cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
175{
176 struct cache_st *st = filter->ctx;
177
William Lallemand4da3f8a2017-10-31 14:33:34 +0100178 if (!(msg->chn->flags & CF_ISRESP) || !st)
179 return 1;
180
William Lallemand9d5f54d2017-11-14 14:39:22 +0100181 st->hdrs_len = msg->sov;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100182
183 return 1;
184}
185
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200186static inline void disable_cache_entry(struct cache_st *st,
187 struct filter *filter, struct shared_context *shctx)
188{
189 struct cache_entry *object;
190
191 object = (struct cache_entry *)st->first_block->data;
192 filter->ctx = NULL; /* disable cache */
193 shctx_lock(shctx);
194 shctx_row_dec_hot(shctx, st->first_block);
195 object->eb.key = 0;
196 shctx_unlock(shctx);
197 pool_free(pool_head_cache_st, st);
198}
199
William Lallemand4da3f8a2017-10-31 14:33:34 +0100200static int
201cache_store_http_forward_data(struct stream *s, struct filter *filter,
202 struct http_msg *msg, unsigned int len)
203{
204 struct cache_st *st = filter->ctx;
205 struct shared_context *shctx = shctx_ptr((struct cache *)filter->config->conf);
206 int ret;
207
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200208 ret = 0;
209
William Lallemand4da3f8a2017-10-31 14:33:34 +0100210 /*
211 * We need to skip the HTTP headers first, because we saved them in the
212 * http-response action.
213 */
214 if (!(msg->chn->flags & CF_ISRESP) || !st)
215 return len;
216
217 if (!len) {
218 /* Nothing to foward */
219 ret = len;
220 }
William Lallemand10935bc2017-11-14 14:39:23 +0100221 else if (st->hdrs_len >= len) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100222 /* Forward part of headers */
223 ret = len;
224 st->hdrs_len -= len;
225 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100226 else {
William Lallemand10935bc2017-11-14 14:39:23 +0100227 /* Forward data */
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100228 if (filter->ctx && st->first_block) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200229 int to_append, append;
230 struct shared_block *fb;
231
232 to_append = MIN(ci_contig_data(msg->chn), len - st->hdrs_len);
233
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200234 shctx_lock(shctx);
235 fb = shctx_row_reserve_hot(shctx, st->first_block, to_append);
236 if (!fb) {
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100237 shctx_unlock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200238 disable_cache_entry(st, filter, shctx);
239 return len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100240 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200241 shctx_unlock(shctx);
242
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200243 /* Skip remaining headers to fill the cache */
244 c_adv(msg->chn, st->hdrs_len);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200245 append = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
246 (unsigned char *)ci_head(msg->chn), to_append);
247 ret = st->hdrs_len + to_append - append;
248 /* Rewind the buffer to forward all data */
249 c_rew(msg->chn, st->hdrs_len);
250 st->hdrs_len = 0;
251 if (ret < 0)
252 disable_cache_entry(st, filter, shctx);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100253 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200254 else
255 ret = len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100256 }
257
258 if ((ret != len) ||
259 (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret))
260 task_wakeup(s->task, TASK_WOKEN_MSG);
261
262 return ret;
263}
264
265static int
266cache_store_http_end(struct stream *s, struct filter *filter,
267 struct http_msg *msg)
268{
269 struct cache_st *st = filter->ctx;
270 struct cache *cache = filter->config->conf;
271 struct shared_context *shctx = shctx_ptr(cache);
272 struct cache_entry *object;
273
274 if (!(msg->chn->flags & CF_ISRESP))
275 return 1;
276
277 if (st && st->first_block) {
278
279 object = (struct cache_entry *)st->first_block->data;
280
281 /* does not need to test if the insertion worked, if it
282 * doesn't, the blocks will be reused anyway */
283
284 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100285 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
286 object->eb.key = 0;
287 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100288 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100289 shctx_row_dec_hot(shctx, st->first_block);
290 shctx_unlock(shctx);
291
292 }
293 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100294 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100295 filter->ctx = NULL;
296 }
297
298 return 1;
299}
300
301 /*
302 * This intends to be used when checking HTTP headers for some
303 * word=value directive. Return a pointer to the first character of value, if
304 * the word was not found or if there wasn't any value assigned ot it return NULL
305 */
306char *directive_value(const char *sample, int slen, const char *word, int wlen)
307{
308 int st = 0;
309
310 if (slen < wlen)
311 return 0;
312
313 while (wlen) {
314 char c = *sample ^ *word;
315 if (c && c != ('A' ^ 'a'))
316 return NULL;
317 sample++;
318 word++;
319 slen--;
320 wlen--;
321 }
322
323 while (slen) {
324 if (st == 0) {
325 if (*sample != '=')
326 return NULL;
327 sample++;
328 slen--;
329 st = 1;
330 continue;
331 } else {
332 return (char *)sample;
333 }
334 }
335
336 return NULL;
337}
338
339/*
340 * Return the maxage in seconds of an HTTP response.
341 * Compute the maxage using either:
342 * - the assigned max-age of the cache
343 * - the s-maxage directive
344 * - the max-age directive
345 * - (Expires - Data) headers
346 * - the default-max-age of the cache
347 *
348 */
William Lallemand49b44532017-11-24 18:53:43 +0100349int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100350{
351 struct http_txn *txn = s->txn;
352 struct hdr_ctx ctx;
353
354 int smaxage = -1;
355 int maxage = -1;
356
357
William Lallemand4da3f8a2017-10-31 14:33:34 +0100358 ctx.idx = 0;
359
360 /* loop on the Cache-Control values */
Willy Tarreau178b9872018-06-19 07:13:36 +0200361 while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100362 char *directive = ctx.line + ctx.val;
363 char *value;
364
365 value = directive_value(directive, ctx.vlen, "s-maxage", 8);
366 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200367 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100368
369 chunk_strncat(chk, value, ctx.vlen - 8 + 1);
370 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200371 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100372 }
373
374 value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
375 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200376 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100377
378 chunk_strncat(chk, value, ctx.vlen - 7 + 1);
379 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200380 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100381 }
382 }
383
384 /* TODO: Expires - Data */
385
386
387 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100388 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100389
390 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100391 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100392
William Lallemand49b44532017-11-24 18:53:43 +0100393 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100394
395}
396
397
William Lallemanda400a3a2017-11-20 19:13:12 +0100398static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
399{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200400 struct cache_entry *object = (struct cache_entry *)block->data;
401
402 if (first == block && object->eb.key)
403 eb32_delete(&object->eb);
404 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100405}
406
William Lallemand41db4602017-10-30 11:15:51 +0100407/*
408 * This fonction will store the headers of the response in a buffer and then
409 * register a filter to store the data
410 */
411enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
412 struct session *sess, struct stream *s, int flags)
413{
William Lallemand4da3f8a2017-10-31 14:33:34 +0100414 struct http_txn *txn = s->txn;
415 struct http_msg *msg = &txn->rsp;
416 struct filter *filter;
417 struct hdr_ctx ctx;
418 struct shared_block *first = NULL;
William Lallemand49b44532017-11-24 18:53:43 +0100419 struct cache *cache = (struct cache *)rule->arg.act.p[0];
420 struct shared_context *shctx = shctx_ptr(cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100421 struct cache_entry *object;
422
423
424 /* Don't cache if the response came from a cache */
425 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
426 s->target == &http_cache_applet.obj_type) {
427 goto out;
428 }
429
430 /* cache only HTTP/1.1 */
431 if (!(txn->req.flags & HTTP_MSGF_VER_11))
432 goto out;
433
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200434 /* Do not cache too big objects. */
435 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
436 msg->sov + msg->body_len > shctx->max_obj_size)
437 goto out;
438
William Lallemand4da3f8a2017-10-31 14:33:34 +0100439 /* cache only GET method */
440 if (txn->meth != HTTP_METH_GET)
441 goto out;
442
443 /* cache only 200 status code */
444 if (txn->status != 200)
445 goto out;
446
447 /* Does not manage Vary at the moment. We will need a secondary key later for that */
448 ctx.idx = 0;
Willy Tarreau178b9872018-06-19 07:13:36 +0200449 if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100450 goto out;
451
Willy Tarreaufaf29092017-12-21 15:59:17 +0100452 check_response_for_cacheability(s, &s->res);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100453
Willy Tarreaud4569d12017-12-22 18:03:04 +0100454 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100455 goto out;
456
William Lallemand4da3f8a2017-10-31 14:33:34 +0100457 shctx_lock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200458 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + msg->sov);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100459 if (!first) {
460 shctx_unlock(shctx);
461 goto out;
462 }
463 shctx_unlock(shctx);
464
Willy Tarreau1093a452018-04-06 19:02:25 +0200465 /* the received memory is not initialized, we need at least to mark
466 * the object as not indexed yet.
467 */
468 object = (struct cache_entry *)first->data;
469 object->eb.node.leaf_p = NULL;
470 object->eb.key = 0;
471
William Lallemand4da3f8a2017-10-31 14:33:34 +0100472 /* reserve space for the cache_entry structure */
473 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200474 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100475 /* cache the headers in a http action because it allows to chose what
476 * to cache, for example you might want to cache a response before
477 * modifying some HTTP headers, or on the contrary after modifying
478 * those headers.
479 */
480
481 /* does not need to be locked because it's in the "hot" list,
482 * copy the headers */
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200483 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)ci_head(&s->res), msg->sov) < 0)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100484 goto out;
485
486 /* register the buffer in the filter ctx for filling it with data*/
487 if (!LIST_ISEMPTY(&s->strm_flt.filters)) {
488 list_for_each_entry(filter, &s->strm_flt.filters, list) {
489 if (filter->config->id == cache_store_flt_id &&
490 filter->config->conf == rule->arg.act.p[0]) {
491 if (filter->ctx) {
492 struct cache_st *cache_ctx = filter->ctx;
Willy Tarreauc9bd34c2017-12-22 17:42:46 +0100493 struct cache_entry *old;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100494
495 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100496
William Lallemandf528fff2017-11-23 19:43:17 +0100497 object->eb.key = (*(unsigned int *)&txn->cache_hash);
498 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100499 /* Insert the node later on caching success */
500
501 shctx_lock(shctx);
Willy Tarreauc9bd34c2017-12-22 17:42:46 +0100502
503 old = entry_exist(cache, txn->cache_hash);
504 if (old) {
505 eb32_delete(&old->eb);
506 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100507 }
508 shctx_unlock(shctx);
509
510 /* store latest value and expiration time */
511 object->latest_validation = now.tv_sec;
William Lallemand49b44532017-11-24 18:53:43 +0100512 object->expire = now.tv_sec + http_calc_maxage(s, cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100513 }
514 return ACT_RET_CONT;
515 }
516 }
517 }
518
519out:
520 /* if does not cache */
521 if (first) {
522 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100523 first->len = 0;
524 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100525 shctx_row_dec_hot(shctx, first);
526 shctx_unlock(shctx);
527 }
528
William Lallemand41db4602017-10-30 11:15:51 +0100529 return ACT_RET_CONT;
530}
531
William Lallemand77c11972017-10-31 20:43:01 +0100532#define HTTP_CACHE_INIT 0
533#define HTTP_CACHE_FWD 1
534#define HTTP_CACHE_END 2
535
William Lallemandecb73b12017-11-24 14:33:55 +0100536static void http_cache_applet_release(struct appctx *appctx)
537{
538 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
539 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
540 struct shared_block *first = block_ptr(cache_ptr);
541
542 shctx_lock(shctx_ptr(cache));
543 shctx_row_dec_hot(shctx_ptr(cache), first);
544 shctx_unlock(shctx_ptr(cache));
545}
546
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200547static int cache_channel_row_data_get(struct appctx *appctx, int len)
William Lallemand77c11972017-10-31 20:43:01 +0100548{
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200549 int ret, total;
William Lallemand77c11972017-10-31 20:43:01 +0100550 struct stream_interface *si = appctx->owner;
551 struct channel *res = si_ic(si);
552 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
William Lallemand77c11972017-10-31 20:43:01 +0100553 struct shared_context *shctx = shctx_ptr(cache);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200554 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
555 struct shared_block *blk, *next = appctx->ctx.cache.next;
556 int offset;
557
558 total = 0;
559 offset = 0;
560
561 if (!next) {
562 offset = sizeof(struct cache_entry);
563 next = block_ptr(cache_ptr);
564 }
565
566 blk = next;
567 list_for_each_entry_from(blk, &shctx->hot, list) {
568 int sz;
569
570 if (len <= 0)
571 break;
572
573 sz = MIN(len, shctx->block_size - offset);
574
575 ret = ci_putblk(res, (const char *)blk->data + offset, sz);
576 if (unlikely(offset))
577 offset = 0;
578 if (ret <= 0) {
579 if (ret == -3 || ret == -1) {
580 si_applet_cant_put(si);
581 break;
582 }
583 return -1;
584 }
585
586 total += sz;
587 len -= sz;
588 }
589 appctx->ctx.cache.next = blk;
590
591 return total;
592}
593
594static void http_cache_io_handler(struct appctx *appctx)
595{
596 struct stream_interface *si = appctx->owner;
597 struct channel *res = si_ic(si);
598 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
William Lallemand77c11972017-10-31 20:43:01 +0100599 struct shared_block *first = block_ptr(cache_ptr);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200600 int *sent = &appctx->ctx.cache.sent;
William Lallemand77c11972017-10-31 20:43:01 +0100601
602 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
603 goto out;
604
605 /* Check if the input buffer is avalaible. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200606 if (res->buf.size == 0) {
William Lallemand77c11972017-10-31 20:43:01 +0100607 si_applet_cant_put(si);
608 goto out;
609 }
610
611 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
612 appctx->st0 = HTTP_CACHE_END;
613
614 /* buffer are aligned there, should be fine */
615 if (appctx->st0 == HTTP_CACHE_INIT) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200616 int len = first->len - *sent - sizeof(struct cache_entry);
William Lallemand55e76742017-11-21 20:01:28 +0100617
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200618 if (len > 0) {
619 int ret;
620
621 ret = cache_channel_row_data_get(appctx, len);
622 if (ret == -1)
623 appctx->st0 = HTTP_CACHE_END;
624 else
625 *sent += ret;
626 }
627 else {
628 *sent = 0;
629 appctx->st0 = HTTP_CACHE_FWD;
William Lallemand77c11972017-10-31 20:43:01 +0100630 }
William Lallemand77c11972017-10-31 20:43:01 +0100631 }
632
633 if (appctx->st0 == HTTP_CACHE_FWD) {
634 /* eat the whole request */
Willy Tarreau178b9872018-06-19 07:13:36 +0200635 co_skip(si_oc(si), co_data(si_oc(si))); // NOTE: when disabled does not repport the correct status code
William Lallemand77c11972017-10-31 20:43:01 +0100636 res->flags |= CF_READ_NULL;
637 si_shutr(si);
638 }
639
640 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
641 si_shutw(si);
642out:
643 ;
644}
645
William Lallemand41db4602017-10-30 11:15:51 +0100646enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
647 struct act_rule *rule, char **err)
648{
649 struct flt_conf *fconf;
650 int cur_arg = *orig_arg;
651 rule->action = ACT_CUSTOM;
652 rule->action_ptr = http_action_store_cache;
653
654 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
655 memprintf(err, "expects a cache name");
656 return ACT_RET_PRS_ERR;
657 }
658
659 /* check if a cache filter was already registered with this cache
660 * name, if that's the case, must use it. */
661 list_for_each_entry(fconf, &proxy->filter_configs, list) {
662 if (fconf->id == cache_store_flt_id && !strcmp((char *)fconf->conf, args[cur_arg])) {
663 rule->arg.act.p[0] = fconf->conf;
664 (*orig_arg)++;
665 /* filter already registered */
666 return ACT_RET_PRS_OK;
667 }
668 }
669
670 rule->arg.act.p[0] = strdup(args[cur_arg]);
671 if (!rule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100672 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100673 err++;
674 goto err;
675 }
676 /* register a filter to fill the cache buffer */
677 fconf = calloc(1, sizeof(*fconf));
678 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100679 ha_alert("config: %s '%s': out of memory\n",
680 proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100681 err++;
682 goto err;
683 }
684 fconf->id = cache_store_flt_id;
685 fconf->conf = rule->arg.act.p[0]; /* store the proxy name */
686 fconf->ops = &cache_ops;
687 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
688
689 (*orig_arg)++;
690
691 return ACT_RET_PRS_OK;
692
693err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100694 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100695}
696
William Lallemandf528fff2017-11-23 19:43:17 +0100697/* This produces a sha1 hash of the concatenation of the first
698 * occurrence of the Host header followed by the path component if it
699 * begins with a slash ('/'). */
700int sha1_hosturi(struct http_txn *txn)
701{
702 struct hdr_ctx ctx;
703
704 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +0200705 struct buffer *trash;
William Lallemandf528fff2017-11-23 19:43:17 +0100706 char *path;
707 char *end;
708 trash = get_trash_chunk();
709
710 /* retrive the host */
711 ctx.idx = 0;
Willy Tarreau178b9872018-06-19 07:13:36 +0200712 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx))
William Lallemandf528fff2017-11-23 19:43:17 +0100713 return 0;
714 chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen);
715
716 /* now retrieve the path */
Willy Tarreau178b9872018-06-19 07:13:36 +0200717 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +0200718 path = http_txn_get_path(txn);
William Lallemandf528fff2017-11-23 19:43:17 +0100719 if (!path)
720 return 0;
721 chunk_strncat(trash, path, end - path);
722
723 /* hash everything */
724 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200725 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +0100726 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
727
728 return 1;
729}
730
731
William Lallemand41db4602017-10-30 11:15:51 +0100732
733enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
734 struct session *sess, struct stream *s, int flags)
735{
William Lallemand77c11972017-10-31 20:43:01 +0100736
William Lallemand77c11972017-10-31 20:43:01 +0100737 struct cache_entry *res;
William Lallemand77c11972017-10-31 20:43:01 +0100738 struct cache *cache = (struct cache *)rule->arg.act.p[0];
739
Willy Tarreau504455c2017-12-22 17:47:35 +0100740 check_request_for_cacheability(s, &s->req);
741 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
742 return ACT_RET_CONT;
743
Willy Tarreau7704b1e2017-12-22 16:32:43 +0100744 if (!sha1_hosturi(s->txn))
745 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +0100746
Willy Tarreau504455c2017-12-22 17:47:35 +0100747 if (s->txn->flags & TX_CACHE_IGNORE)
748 return ACT_RET_CONT;
749
William Lallemanda400a3a2017-11-20 19:13:12 +0100750 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +0100751 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +0100752 if (res) {
753 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +0100754 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
755 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +0100756 s->target = &http_cache_applet.obj_type;
757 if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
758 appctx->st0 = HTTP_CACHE_INIT;
759 appctx->rule = rule;
760 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200761 appctx->ctx.cache.next = NULL;
762 appctx->ctx.cache.sent = 0;
Olivier Houchardfccf8402017-11-01 14:04:02 +0100763 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +0100764 } else {
William Lallemand55e76742017-11-21 20:01:28 +0100765 shctx_lock(shctx_ptr(cache));
766 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
767 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100768 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +0100769 }
770 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100771 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100772 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +0100773}
774
775
776enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
777 struct act_rule *rule, char **err)
778{
779 int cur_arg = *orig_arg;
780
781 rule->action = ACT_CUSTOM;
782 rule->action_ptr = http_action_req_cache_use;
783
784 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
785 memprintf(err, "expects a cache name");
786 return ACT_RET_PRS_ERR;
787 }
788
789 rule->arg.act.p[0] = strdup(args[cur_arg]);
790 if (!rule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100791 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100792 err++;
793 goto err;
794 }
795
796 (*orig_arg)++;
797 return ACT_RET_PRS_OK;
798
799err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100800 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100801
802}
803
804int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
805{
806 int err_code = 0;
807
808 if (strcmp(args[0], "cache") == 0) { /* new cache section */
809
810 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100811 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
812 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100813 err_code |= ERR_ALERT | ERR_ABORT;
814 goto out;
815 }
816
817 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
818 err_code |= ERR_ABORT;
819 goto out;
820 }
821
822 if (tmp_cache_config == NULL) {
823 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
824 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100825 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +0100826 err_code |= ERR_ALERT | ERR_ABORT;
827 goto out;
828 }
829
830 strlcpy2(tmp_cache_config->id, args[1], 33);
831 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100832 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
833 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +0100834 err_code |= ERR_WARN;
835 }
William Lallemand49b44532017-11-24 18:53:43 +0100836 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +0100837 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200838 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +0100839 }
840 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +0200841 unsigned long int maxsize;
842 char *err;
William Lallemand41db4602017-10-30 11:15:51 +0100843
844 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
845 err_code |= ERR_ABORT;
846 goto out;
847 }
848
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +0200849 maxsize = strtoul(args[1], &err, 10);
850 if (err == args[1] || *err != '\0') {
851 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
852 file, linenum, args[1]);
853 err_code |= ERR_ABORT;
854 goto out;
855 }
856
857 if (maxsize > (UINT_MAX >> 20)) {
858 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
859 file, linenum, args[1], UINT_MAX >> 20);
860 err_code |= ERR_ABORT;
861 goto out;
862 }
863
William Lallemand41db4602017-10-30 11:15:51 +0100864 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +0200865 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +0100866 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +0100867 } else if (strcmp(args[0], "max-age") == 0) {
868 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
869 err_code |= ERR_ABORT;
870 goto out;
871 }
872
873 if (!*args[1]) {
874 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
875 file, linenum, args[0]);
876 err_code |= ERR_WARN;
877 }
878
879 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200880 } else if (strcmp(args[0], "max-object-size") == 0) {
881 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
882 err_code |= ERR_ABORT;
883 goto out;
884 }
885
886 if (!*args[1]) {
887 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
888 file, linenum, args[0]);
889 err_code |= ERR_WARN;
890 }
891
892 tmp_cache_config->maxobjsz = atoi(args[1]);
893 }
894 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100895 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100896 err_code |= ERR_ALERT | ERR_FATAL;
897 goto out;
898 }
899out:
900 return err_code;
901}
902
903/* once the cache section is parsed */
904
905int cfg_post_parse_section_cache()
906{
907 struct shared_context *shctx;
908 int err_code = 0;
909 int ret_shctx;
910
911 if (tmp_cache_config) {
912 struct cache *cache;
913
914 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100915 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +0100916 err_code |= ERR_FATAL | ERR_ALERT;
917 goto out;
918 }
919
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200920 if (!tmp_cache_config->maxobjsz)
921 /* Default max. file size is a 256th of the cache size. */
922 tmp_cache_config->maxobjsz =
923 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
924
925 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE,
926 tmp_cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100927
William Lallemand41db4602017-10-30 11:15:51 +0100928 if (ret_shctx < 0) {
929 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100930 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +0100931 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100932 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +0100933
934 err_code |= ERR_FATAL | ERR_ALERT;
935 goto out;
936 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100937 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +0100938 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
939 cache = (struct cache *)shctx->data;
940 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +0100941 LIST_ADDQ(&caches, &cache->list);
942 }
943out:
944 free(tmp_cache_config);
945 tmp_cache_config = NULL;
946 return err_code;
947
948}
949
950/*
951 * Resolve the cache name to a pointer once the file is completely read.
952 */
953int cfg_cache_postparser()
954{
955 struct act_rule *hresrule, *hrqrule;
956 void *cache_ptr;
957 struct cache *cache;
958 struct proxy *curproxy = NULL;
959 int err = 0;
960 struct flt_conf *fconf;
961
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100962 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
William Lallemand41db4602017-10-30 11:15:51 +0100963
964 /* resolve the http response cache name to a ptr in the action rule */
965 list_for_each_entry(hresrule, &curproxy->http_res_rules, list) {
966 if (hresrule->action != ACT_CUSTOM ||
967 hresrule->action_ptr != http_action_store_cache)
968 continue;
969
970 cache_ptr = hresrule->arg.act.p[0];
971
972 list_for_each_entry(cache, &caches, list) {
973 if (!strcmp(cache->id, cache_ptr)) {
974 /* don't free there, it's still used in the filter conf */
975 cache_ptr = cache;
976 break;
977 }
978 }
979
980 if (cache_ptr == hresrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100981 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n",
982 curproxy->id, (char *)hresrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100983 err++;
984 }
985
986 hresrule->arg.act.p[0] = cache_ptr;
987 }
988
989 /* resolve the http request cache name to a ptr in the action rule */
990 list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) {
991 if (hrqrule->action != ACT_CUSTOM ||
992 hrqrule->action_ptr != http_action_req_cache_use)
993 continue;
994
995 cache_ptr = hrqrule->arg.act.p[0];
996
997 list_for_each_entry(cache, &caches, list) {
998 if (!strcmp(cache->id, cache_ptr)) {
999 free(cache_ptr);
1000 cache_ptr = cache;
1001 break;
1002 }
1003 }
1004
1005 if (cache_ptr == hrqrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001006 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n",
1007 curproxy->id, (char *)hrqrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001008 err++;
1009 }
1010
1011 hrqrule->arg.act.p[0] = cache_ptr;
1012 }
1013
1014 /* resolve the cache name to a ptr in the filter config */
1015 list_for_each_entry(fconf, &curproxy->filter_configs, list) {
1016
William Lallemand9c54c532017-11-02 16:38:42 +01001017 if (fconf->id != cache_store_flt_id)
1018 continue;
1019
William Lallemand41db4602017-10-30 11:15:51 +01001020 cache_ptr = fconf->conf;
1021
1022 list_for_each_entry(cache, &caches, list) {
1023 if (!strcmp(cache->id, cache_ptr)) {
1024 /* there can be only one filter per cache, so we free it there */
1025 free(cache_ptr);
1026 cache_ptr = cache;
1027 break;
1028 }
1029 }
1030
1031 if (cache_ptr == fconf->conf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001032 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
1033 curproxy->id, (char *)fconf->conf);
William Lallemand41db4602017-10-30 11:15:51 +01001034 err++;
1035 }
1036 fconf->conf = cache_ptr;
1037 }
1038 }
1039 return err;
1040}
1041
1042
1043struct flt_ops cache_ops = {
1044 .init = cache_store_init,
1045
William Lallemand4da3f8a2017-10-31 14:33:34 +01001046 /* Handle channels activity */
1047 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001048 .channel_end_analyze = cache_store_chn_end_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001049
1050 /* Filter HTTP requests and responses */
1051 .http_headers = cache_store_http_headers,
1052 .http_end = cache_store_http_end,
1053
1054 .http_forward_data = cache_store_http_forward_data,
1055
William Lallemand41db4602017-10-30 11:15:51 +01001056};
1057
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001058static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001059{
1060 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1061 return 1;
1062
1063 return 0;
1064}
1065
1066static int cli_io_handler_show_cache(struct appctx *appctx)
1067{
1068 struct cache* cache = appctx->ctx.cli.p0;
1069 struct stream_interface *si = appctx->owner;
1070
William Lallemand1f49a362017-11-21 20:01:26 +01001071 if (cache == NULL) {
1072 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1073 }
1074
1075 list_for_each_entry_from(cache, &caches, list) {
1076 struct eb32_node *node = NULL;
1077 unsigned int next_key;
1078 struct cache_entry *entry;
1079
William Lallemand1f49a362017-11-21 20:01:26 +01001080 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001081 if (!next_key) {
1082 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1083 if (ci_putchk(si_ic(si), &trash) == -1) {
1084 si_applet_cant_put(si);
1085 return 0;
1086 }
1087 }
William Lallemand1f49a362017-11-21 20:01:26 +01001088
1089 appctx->ctx.cli.p0 = cache;
1090
1091 while (1) {
1092
1093 shctx_lock(shctx_ptr(cache));
1094 node = eb32_lookup_ge(&cache->entries, next_key);
1095 if (!node) {
1096 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001097 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001098 break;
1099 }
1100
1101 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001102 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 +01001103
1104 next_key = node->key + 1;
1105 appctx->ctx.cli.i0 = next_key;
1106
1107 shctx_unlock(shctx_ptr(cache));
1108
1109 if (ci_putchk(si_ic(si), &trash) == -1) {
1110 si_applet_cant_put(si);
1111 return 0;
1112 }
1113 }
1114
1115 }
1116
1117 return 1;
1118
1119}
1120
1121static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001122 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1123 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001124}};
1125
1126
William Lallemand41db4602017-10-30 11:15:51 +01001127static struct action_kw_list http_res_actions = {
1128 .kw = {
1129 { "cache-store", parse_cache_store },
1130 { NULL, NULL }
1131 }
1132};
1133
1134static struct action_kw_list http_req_actions = {
1135 .kw = {
1136 { "cache-use", parse_cache_use },
1137 { NULL, NULL }
1138 }
1139};
1140
1141struct applet http_cache_applet = {
1142 .obj_type = OBJ_TYPE_APPLET,
1143 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001144 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001145 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001146};
1147
1148__attribute__((constructor))
1149static void __cache_init(void)
1150{
1151 cfg_register_section("cache", cfg_parse_cache, cfg_post_parse_section_cache);
1152 cfg_register_postparser("cache", cfg_cache_postparser);
William Lallemand1f49a362017-11-21 20:01:26 +01001153 cli_register_kw(&cli_kws);
William Lallemand41db4602017-10-30 11:15:51 +01001154 http_res_keywords_register(&http_res_actions);
1155 http_req_keywords_register(&http_req_actions);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001156 pool_head_cache_st = create_pool("cache_st", sizeof(struct cache_st), MEM_F_SHARED);
William Lallemand41db4602017-10-30 11:15:51 +01001157}
1158