blob: a198b4069aa98e455bbcb06cf66099821549da53 [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;
53 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010054};
55
56/*
57 * cache ctx for filters
58 */
59struct cache_st {
60 int hdrs_len;
61 struct shared_block *first_block;
62};
63
64struct cache_entry {
65 unsigned int latest_validation; /* latest validation date */
66 unsigned int expire; /* expiration date */
67 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010068 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010069 unsigned char data[0];
70};
71
72#define CACHE_BLOCKSIZE 1024
73
74static struct list caches = LIST_HEAD_INIT(caches);
75static struct cache *tmp_cache_config = NULL;
76
William Lallemandf528fff2017-11-23 19:43:17 +010077struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010078{
79 struct eb32_node *node;
80 struct cache_entry *entry;
81
William Lallemandf528fff2017-11-23 19:43:17 +010082 node = eb32_lookup(&cache->entries, (*(unsigned int *)hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010083 if (!node)
84 return NULL;
85
86 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +010087
88 /* if that's not the right node */
89 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
90 return NULL;
91
William Lallemand08727662017-11-21 20:01:27 +010092 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +010093 return entry;
William Lallemand08727662017-11-21 20:01:27 +010094 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +010095 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +010096 entry->eb.key = 0;
97 }
William Lallemand4da3f8a2017-10-31 14:33:34 +010098 return NULL;
99
100}
101
102static inline struct shared_context *shctx_ptr(struct cache *cache)
103{
104 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
105}
106
William Lallemand77c11972017-10-31 20:43:01 +0100107static inline struct shared_block *block_ptr(struct cache_entry *entry)
108{
109 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
110}
111
112
113
William Lallemand41db4602017-10-30 11:15:51 +0100114static int
115cache_store_init(struct proxy *px, struct flt_conf *f1conf)
116{
117 return 0;
118}
119
William Lallemand4da3f8a2017-10-31 14:33:34 +0100120static int
121cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
122{
123 if (!(chn->flags & CF_ISRESP))
124 return 1;
125
126 if (filter->ctx == NULL) {
127 struct cache_st *st;
128
Willy Tarreaubafbe012017-11-24 17:34:44 +0100129 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100130 if (st == NULL)
131 return -1;
132
133 st->hdrs_len = 0;
134 st->first_block = NULL;
135 filter->ctx = st;
136 }
137
138 register_data_filter(s, chn, filter);
139
140 return 1;
141}
142
143static int
William Lallemand49dc0482017-11-24 14:33:54 +0100144cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
145{
146 struct cache_st *st = filter->ctx;
147 struct cache *cache = filter->config->conf;
148 struct shared_context *shctx = shctx_ptr(cache);
149
150 if (!(chn->flags & CF_ISRESP))
151 return 1;
152
153 /* Everything should be released in the http_end filter, but we need to do it
154 * there too, in case of errors */
155
156 if (st && st->first_block) {
157
158 shctx_lock(shctx);
159 shctx_row_dec_hot(shctx, st->first_block);
160 shctx_unlock(shctx);
161
162 }
163 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100164 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100165 filter->ctx = NULL;
166 }
167
168 return 1;
169}
170
171
172static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100173cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
174{
175 struct cache_st *st = filter->ctx;
176
William Lallemand4da3f8a2017-10-31 14:33:34 +0100177 if (!(msg->chn->flags & CF_ISRESP) || !st)
178 return 1;
179
William Lallemand9d5f54d2017-11-14 14:39:22 +0100180 st->hdrs_len = msg->sov;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100181
182 return 1;
183}
184
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200185static inline void disable_cache_entry(struct cache_st *st,
186 struct filter *filter, struct shared_context *shctx)
187{
188 struct cache_entry *object;
189
190 object = (struct cache_entry *)st->first_block->data;
191 filter->ctx = NULL; /* disable cache */
192 shctx_lock(shctx);
193 shctx_row_dec_hot(shctx, st->first_block);
194 object->eb.key = 0;
195 shctx_unlock(shctx);
196 pool_free(pool_head_cache_st, st);
197}
198
William Lallemand4da3f8a2017-10-31 14:33:34 +0100199static int
200cache_store_http_forward_data(struct stream *s, struct filter *filter,
201 struct http_msg *msg, unsigned int len)
202{
203 struct cache_st *st = filter->ctx;
204 struct shared_context *shctx = shctx_ptr((struct cache *)filter->config->conf);
205 int ret;
206
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200207 ret = 0;
208
William Lallemand4da3f8a2017-10-31 14:33:34 +0100209 /*
210 * We need to skip the HTTP headers first, because we saved them in the
211 * http-response action.
212 */
213 if (!(msg->chn->flags & CF_ISRESP) || !st)
214 return len;
215
216 if (!len) {
217 /* Nothing to foward */
218 ret = len;
219 }
William Lallemand10935bc2017-11-14 14:39:23 +0100220 else if (st->hdrs_len >= len) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100221 /* Forward part of headers */
222 ret = len;
223 st->hdrs_len -= len;
224 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100225 else {
William Lallemand10935bc2017-11-14 14:39:23 +0100226 /* Forward data */
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100227 if (filter->ctx && st->first_block) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200228 int to_append, append;
229 struct shared_block *fb;
230
231 to_append = MIN(ci_contig_data(msg->chn), len - st->hdrs_len);
232
233 /* Skip remaining headers to fill the cache */
234 c_adv(msg->chn, st->hdrs_len);
235
236 shctx_lock(shctx);
237 fb = shctx_row_reserve_hot(shctx, st->first_block, to_append);
238 if (!fb) {
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100239 shctx_unlock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200240 disable_cache_entry(st, filter, shctx);
241 return len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100242 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200243 shctx_unlock(shctx);
244
245 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
434 /* cache only GET method */
435 if (txn->meth != HTTP_METH_GET)
436 goto out;
437
438 /* cache only 200 status code */
439 if (txn->status != 200)
440 goto out;
441
442 /* Does not manage Vary at the moment. We will need a secondary key later for that */
443 ctx.idx = 0;
Willy Tarreau178b9872018-06-19 07:13:36 +0200444 if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100445 goto out;
446
Willy Tarreaufaf29092017-12-21 15:59:17 +0100447 check_response_for_cacheability(s, &s->res);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100448
Willy Tarreaud4569d12017-12-22 18:03:04 +0100449 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100450 goto out;
451
William Lallemand4da3f8a2017-10-31 14:33:34 +0100452 shctx_lock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200453 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + msg->sov);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100454 if (!first) {
455 shctx_unlock(shctx);
456 goto out;
457 }
458 shctx_unlock(shctx);
459
Willy Tarreau1093a452018-04-06 19:02:25 +0200460 /* the received memory is not initialized, we need at least to mark
461 * the object as not indexed yet.
462 */
463 object = (struct cache_entry *)first->data;
464 object->eb.node.leaf_p = NULL;
465 object->eb.key = 0;
466
William Lallemand4da3f8a2017-10-31 14:33:34 +0100467 /* reserve space for the cache_entry structure */
468 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200469 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100470 /* cache the headers in a http action because it allows to chose what
471 * to cache, for example you might want to cache a response before
472 * modifying some HTTP headers, or on the contrary after modifying
473 * those headers.
474 */
475
476 /* does not need to be locked because it's in the "hot" list,
477 * copy the headers */
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200478 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)ci_head(&s->res), msg->sov) < 0)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100479 goto out;
480
481 /* register the buffer in the filter ctx for filling it with data*/
482 if (!LIST_ISEMPTY(&s->strm_flt.filters)) {
483 list_for_each_entry(filter, &s->strm_flt.filters, list) {
484 if (filter->config->id == cache_store_flt_id &&
485 filter->config->conf == rule->arg.act.p[0]) {
486 if (filter->ctx) {
487 struct cache_st *cache_ctx = filter->ctx;
Willy Tarreauc9bd34c2017-12-22 17:42:46 +0100488 struct cache_entry *old;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100489
490 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100491
William Lallemandf528fff2017-11-23 19:43:17 +0100492 object->eb.key = (*(unsigned int *)&txn->cache_hash);
493 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100494 /* Insert the node later on caching success */
495
496 shctx_lock(shctx);
Willy Tarreauc9bd34c2017-12-22 17:42:46 +0100497
498 old = entry_exist(cache, txn->cache_hash);
499 if (old) {
500 eb32_delete(&old->eb);
501 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100502 }
503 shctx_unlock(shctx);
504
505 /* store latest value and expiration time */
506 object->latest_validation = now.tv_sec;
William Lallemand49b44532017-11-24 18:53:43 +0100507 object->expire = now.tv_sec + http_calc_maxage(s, cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100508 }
509 return ACT_RET_CONT;
510 }
511 }
512 }
513
514out:
515 /* if does not cache */
516 if (first) {
517 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100518 first->len = 0;
519 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100520 shctx_row_dec_hot(shctx, first);
521 shctx_unlock(shctx);
522 }
523
William Lallemand41db4602017-10-30 11:15:51 +0100524 return ACT_RET_CONT;
525}
526
William Lallemand77c11972017-10-31 20:43:01 +0100527#define HTTP_CACHE_INIT 0
528#define HTTP_CACHE_FWD 1
529#define HTTP_CACHE_END 2
530
William Lallemandecb73b12017-11-24 14:33:55 +0100531static void http_cache_applet_release(struct appctx *appctx)
532{
533 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
534 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
535 struct shared_block *first = block_ptr(cache_ptr);
536
537 shctx_lock(shctx_ptr(cache));
538 shctx_row_dec_hot(shctx_ptr(cache), first);
539 shctx_unlock(shctx_ptr(cache));
540}
541
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200542static int cache_channel_row_data_get(struct appctx *appctx, int len)
William Lallemand77c11972017-10-31 20:43:01 +0100543{
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200544 int ret, total;
William Lallemand77c11972017-10-31 20:43:01 +0100545 struct stream_interface *si = appctx->owner;
546 struct channel *res = si_ic(si);
547 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
William Lallemand77c11972017-10-31 20:43:01 +0100548 struct shared_context *shctx = shctx_ptr(cache);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200549 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
550 struct shared_block *blk, *next = appctx->ctx.cache.next;
551 int offset;
552
553 total = 0;
554 offset = 0;
555
556 if (!next) {
557 offset = sizeof(struct cache_entry);
558 next = block_ptr(cache_ptr);
559 }
560
561 blk = next;
562 list_for_each_entry_from(blk, &shctx->hot, list) {
563 int sz;
564
565 if (len <= 0)
566 break;
567
568 sz = MIN(len, shctx->block_size - offset);
569
570 ret = ci_putblk(res, (const char *)blk->data + offset, sz);
571 if (unlikely(offset))
572 offset = 0;
573 if (ret <= 0) {
574 if (ret == -3 || ret == -1) {
575 si_applet_cant_put(si);
576 break;
577 }
578 return -1;
579 }
580
581 total += sz;
582 len -= sz;
583 }
584 appctx->ctx.cache.next = blk;
585
586 return total;
587}
588
589static void http_cache_io_handler(struct appctx *appctx)
590{
591 struct stream_interface *si = appctx->owner;
592 struct channel *res = si_ic(si);
593 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
William Lallemand77c11972017-10-31 20:43:01 +0100594 struct shared_block *first = block_ptr(cache_ptr);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200595 int *sent = &appctx->ctx.cache.sent;
William Lallemand77c11972017-10-31 20:43:01 +0100596
597 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
598 goto out;
599
600 /* Check if the input buffer is avalaible. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200601 if (res->buf.size == 0) {
William Lallemand77c11972017-10-31 20:43:01 +0100602 si_applet_cant_put(si);
603 goto out;
604 }
605
606 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
607 appctx->st0 = HTTP_CACHE_END;
608
609 /* buffer are aligned there, should be fine */
610 if (appctx->st0 == HTTP_CACHE_INIT) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200611 int len = first->len - *sent - sizeof(struct cache_entry);
William Lallemand55e76742017-11-21 20:01:28 +0100612
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200613 if (len > 0) {
614 int ret;
615
616 ret = cache_channel_row_data_get(appctx, len);
617 if (ret == -1)
618 appctx->st0 = HTTP_CACHE_END;
619 else
620 *sent += ret;
621 }
622 else {
623 *sent = 0;
624 appctx->st0 = HTTP_CACHE_FWD;
William Lallemand77c11972017-10-31 20:43:01 +0100625 }
William Lallemand77c11972017-10-31 20:43:01 +0100626 }
627
628 if (appctx->st0 == HTTP_CACHE_FWD) {
629 /* eat the whole request */
Willy Tarreau178b9872018-06-19 07:13:36 +0200630 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 +0100631 res->flags |= CF_READ_NULL;
632 si_shutr(si);
633 }
634
635 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
636 si_shutw(si);
637out:
638 ;
639}
640
William Lallemand41db4602017-10-30 11:15:51 +0100641enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
642 struct act_rule *rule, char **err)
643{
644 struct flt_conf *fconf;
645 int cur_arg = *orig_arg;
646 rule->action = ACT_CUSTOM;
647 rule->action_ptr = http_action_store_cache;
648
649 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
650 memprintf(err, "expects a cache name");
651 return ACT_RET_PRS_ERR;
652 }
653
654 /* check if a cache filter was already registered with this cache
655 * name, if that's the case, must use it. */
656 list_for_each_entry(fconf, &proxy->filter_configs, list) {
657 if (fconf->id == cache_store_flt_id && !strcmp((char *)fconf->conf, args[cur_arg])) {
658 rule->arg.act.p[0] = fconf->conf;
659 (*orig_arg)++;
660 /* filter already registered */
661 return ACT_RET_PRS_OK;
662 }
663 }
664
665 rule->arg.act.p[0] = strdup(args[cur_arg]);
666 if (!rule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100667 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100668 err++;
669 goto err;
670 }
671 /* register a filter to fill the cache buffer */
672 fconf = calloc(1, sizeof(*fconf));
673 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100674 ha_alert("config: %s '%s': out of memory\n",
675 proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100676 err++;
677 goto err;
678 }
679 fconf->id = cache_store_flt_id;
680 fconf->conf = rule->arg.act.p[0]; /* store the proxy name */
681 fconf->ops = &cache_ops;
682 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
683
684 (*orig_arg)++;
685
686 return ACT_RET_PRS_OK;
687
688err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100689 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100690}
691
William Lallemandf528fff2017-11-23 19:43:17 +0100692/* This produces a sha1 hash of the concatenation of the first
693 * occurrence of the Host header followed by the path component if it
694 * begins with a slash ('/'). */
695int sha1_hosturi(struct http_txn *txn)
696{
697 struct hdr_ctx ctx;
698
699 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +0200700 struct buffer *trash;
William Lallemandf528fff2017-11-23 19:43:17 +0100701 char *path;
702 char *end;
703 trash = get_trash_chunk();
704
705 /* retrive the host */
706 ctx.idx = 0;
Willy Tarreau178b9872018-06-19 07:13:36 +0200707 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx))
William Lallemandf528fff2017-11-23 19:43:17 +0100708 return 0;
709 chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen);
710
711 /* now retrieve the path */
Willy Tarreau178b9872018-06-19 07:13:36 +0200712 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +0200713 path = http_txn_get_path(txn);
William Lallemandf528fff2017-11-23 19:43:17 +0100714 if (!path)
715 return 0;
716 chunk_strncat(trash, path, end - path);
717
718 /* hash everything */
719 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200720 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +0100721 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
722
723 return 1;
724}
725
726
William Lallemand41db4602017-10-30 11:15:51 +0100727
728enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
729 struct session *sess, struct stream *s, int flags)
730{
William Lallemand77c11972017-10-31 20:43:01 +0100731
William Lallemand77c11972017-10-31 20:43:01 +0100732 struct cache_entry *res;
William Lallemand77c11972017-10-31 20:43:01 +0100733 struct cache *cache = (struct cache *)rule->arg.act.p[0];
734
Willy Tarreau504455c2017-12-22 17:47:35 +0100735 check_request_for_cacheability(s, &s->req);
736 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
737 return ACT_RET_CONT;
738
Willy Tarreau7704b1e2017-12-22 16:32:43 +0100739 if (!sha1_hosturi(s->txn))
740 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +0100741
Willy Tarreau504455c2017-12-22 17:47:35 +0100742 if (s->txn->flags & TX_CACHE_IGNORE)
743 return ACT_RET_CONT;
744
William Lallemanda400a3a2017-11-20 19:13:12 +0100745 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +0100746 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +0100747 if (res) {
748 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +0100749 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
750 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +0100751 s->target = &http_cache_applet.obj_type;
752 if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
753 appctx->st0 = HTTP_CACHE_INIT;
754 appctx->rule = rule;
755 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200756 appctx->ctx.cache.next = NULL;
757 appctx->ctx.cache.sent = 0;
Olivier Houchardfccf8402017-11-01 14:04:02 +0100758 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +0100759 } else {
William Lallemand55e76742017-11-21 20:01:28 +0100760 shctx_lock(shctx_ptr(cache));
761 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
762 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100763 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +0100764 }
765 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100766 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100767 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +0100768}
769
770
771enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
772 struct act_rule *rule, char **err)
773{
774 int cur_arg = *orig_arg;
775
776 rule->action = ACT_CUSTOM;
777 rule->action_ptr = http_action_req_cache_use;
778
779 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
780 memprintf(err, "expects a cache name");
781 return ACT_RET_PRS_ERR;
782 }
783
784 rule->arg.act.p[0] = strdup(args[cur_arg]);
785 if (!rule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100786 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100787 err++;
788 goto err;
789 }
790
791 (*orig_arg)++;
792 return ACT_RET_PRS_OK;
793
794err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100795 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100796
797}
798
799int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
800{
801 int err_code = 0;
802
803 if (strcmp(args[0], "cache") == 0) { /* new cache section */
804
805 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100806 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
807 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100808 err_code |= ERR_ALERT | ERR_ABORT;
809 goto out;
810 }
811
812 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
813 err_code |= ERR_ABORT;
814 goto out;
815 }
816
817 if (tmp_cache_config == NULL) {
818 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
819 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100820 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +0100821 err_code |= ERR_ALERT | ERR_ABORT;
822 goto out;
823 }
824
825 strlcpy2(tmp_cache_config->id, args[1], 33);
826 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100827 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
828 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +0100829 err_code |= ERR_WARN;
830 }
William Lallemand49b44532017-11-24 18:53:43 +0100831 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +0100832 tmp_cache_config->maxblocks = 0;
833 }
834 } else if (strcmp(args[0], "total-max-size") == 0) {
835 int maxsize;
836
837 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
838 err_code |= ERR_ABORT;
839 goto out;
840 }
841
842 /* size in megabytes */
843 maxsize = atoi(args[1]) * 1024 * 1024 / CACHE_BLOCKSIZE;
844 tmp_cache_config->maxblocks = maxsize;
845
William Lallemand49b44532017-11-24 18:53:43 +0100846 } else if (strcmp(args[0], "max-age") == 0) {
847 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
848 err_code |= ERR_ABORT;
849 goto out;
850 }
851
852 if (!*args[1]) {
853 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
854 file, linenum, args[0]);
855 err_code |= ERR_WARN;
856 }
857
858 tmp_cache_config->maxage = atoi(args[1]);
William Lallemand41db4602017-10-30 11:15:51 +0100859 } else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100860 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100861 err_code |= ERR_ALERT | ERR_FATAL;
862 goto out;
863 }
864out:
865 return err_code;
866}
867
868/* once the cache section is parsed */
869
870int cfg_post_parse_section_cache()
871{
872 struct shared_context *shctx;
873 int err_code = 0;
874 int ret_shctx;
875
876 if (tmp_cache_config) {
877 struct cache *cache;
878
879 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100880 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +0100881 err_code |= ERR_FATAL | ERR_ALERT;
882 goto out;
883 }
884
885 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100886
William Lallemand41db4602017-10-30 11:15:51 +0100887 if (ret_shctx < 0) {
888 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100889 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +0100890 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100891 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +0100892
893 err_code |= ERR_FATAL | ERR_ALERT;
894 goto out;
895 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100896 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +0100897 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
898 cache = (struct cache *)shctx->data;
899 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +0100900 LIST_ADDQ(&caches, &cache->list);
901 }
902out:
903 free(tmp_cache_config);
904 tmp_cache_config = NULL;
905 return err_code;
906
907}
908
909/*
910 * Resolve the cache name to a pointer once the file is completely read.
911 */
912int cfg_cache_postparser()
913{
914 struct act_rule *hresrule, *hrqrule;
915 void *cache_ptr;
916 struct cache *cache;
917 struct proxy *curproxy = NULL;
918 int err = 0;
919 struct flt_conf *fconf;
920
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100921 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
William Lallemand41db4602017-10-30 11:15:51 +0100922
923 /* resolve the http response cache name to a ptr in the action rule */
924 list_for_each_entry(hresrule, &curproxy->http_res_rules, list) {
925 if (hresrule->action != ACT_CUSTOM ||
926 hresrule->action_ptr != http_action_store_cache)
927 continue;
928
929 cache_ptr = hresrule->arg.act.p[0];
930
931 list_for_each_entry(cache, &caches, list) {
932 if (!strcmp(cache->id, cache_ptr)) {
933 /* don't free there, it's still used in the filter conf */
934 cache_ptr = cache;
935 break;
936 }
937 }
938
939 if (cache_ptr == hresrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100940 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n",
941 curproxy->id, (char *)hresrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100942 err++;
943 }
944
945 hresrule->arg.act.p[0] = cache_ptr;
946 }
947
948 /* resolve the http request cache name to a ptr in the action rule */
949 list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) {
950 if (hrqrule->action != ACT_CUSTOM ||
951 hrqrule->action_ptr != http_action_req_cache_use)
952 continue;
953
954 cache_ptr = hrqrule->arg.act.p[0];
955
956 list_for_each_entry(cache, &caches, list) {
957 if (!strcmp(cache->id, cache_ptr)) {
958 free(cache_ptr);
959 cache_ptr = cache;
960 break;
961 }
962 }
963
964 if (cache_ptr == hrqrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100965 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n",
966 curproxy->id, (char *)hrqrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100967 err++;
968 }
969
970 hrqrule->arg.act.p[0] = cache_ptr;
971 }
972
973 /* resolve the cache name to a ptr in the filter config */
974 list_for_each_entry(fconf, &curproxy->filter_configs, list) {
975
William Lallemand9c54c532017-11-02 16:38:42 +0100976 if (fconf->id != cache_store_flt_id)
977 continue;
978
William Lallemand41db4602017-10-30 11:15:51 +0100979 cache_ptr = fconf->conf;
980
981 list_for_each_entry(cache, &caches, list) {
982 if (!strcmp(cache->id, cache_ptr)) {
983 /* there can be only one filter per cache, so we free it there */
984 free(cache_ptr);
985 cache_ptr = cache;
986 break;
987 }
988 }
989
990 if (cache_ptr == fconf->conf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100991 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
992 curproxy->id, (char *)fconf->conf);
William Lallemand41db4602017-10-30 11:15:51 +0100993 err++;
994 }
995 fconf->conf = cache_ptr;
996 }
997 }
998 return err;
999}
1000
1001
1002struct flt_ops cache_ops = {
1003 .init = cache_store_init,
1004
William Lallemand4da3f8a2017-10-31 14:33:34 +01001005 /* Handle channels activity */
1006 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001007 .channel_end_analyze = cache_store_chn_end_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001008
1009 /* Filter HTTP requests and responses */
1010 .http_headers = cache_store_http_headers,
1011 .http_end = cache_store_http_end,
1012
1013 .http_forward_data = cache_store_http_forward_data,
1014
William Lallemand41db4602017-10-30 11:15:51 +01001015};
1016
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001017static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001018{
1019 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1020 return 1;
1021
1022 return 0;
1023}
1024
1025static int cli_io_handler_show_cache(struct appctx *appctx)
1026{
1027 struct cache* cache = appctx->ctx.cli.p0;
1028 struct stream_interface *si = appctx->owner;
1029
William Lallemand1f49a362017-11-21 20:01:26 +01001030 if (cache == NULL) {
1031 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1032 }
1033
1034 list_for_each_entry_from(cache, &caches, list) {
1035 struct eb32_node *node = NULL;
1036 unsigned int next_key;
1037 struct cache_entry *entry;
1038
William Lallemand1f49a362017-11-21 20:01:26 +01001039 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001040 if (!next_key) {
1041 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1042 if (ci_putchk(si_ic(si), &trash) == -1) {
1043 si_applet_cant_put(si);
1044 return 0;
1045 }
1046 }
William Lallemand1f49a362017-11-21 20:01:26 +01001047
1048 appctx->ctx.cli.p0 = cache;
1049
1050 while (1) {
1051
1052 shctx_lock(shctx_ptr(cache));
1053 node = eb32_lookup_ge(&cache->entries, next_key);
1054 if (!node) {
1055 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001056 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001057 break;
1058 }
1059
1060 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001061 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 +01001062
1063 next_key = node->key + 1;
1064 appctx->ctx.cli.i0 = next_key;
1065
1066 shctx_unlock(shctx_ptr(cache));
1067
1068 if (ci_putchk(si_ic(si), &trash) == -1) {
1069 si_applet_cant_put(si);
1070 return 0;
1071 }
1072 }
1073
1074 }
1075
1076 return 1;
1077
1078}
1079
1080static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001081 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1082 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001083}};
1084
1085
William Lallemand41db4602017-10-30 11:15:51 +01001086static struct action_kw_list http_res_actions = {
1087 .kw = {
1088 { "cache-store", parse_cache_store },
1089 { NULL, NULL }
1090 }
1091};
1092
1093static struct action_kw_list http_req_actions = {
1094 .kw = {
1095 { "cache-use", parse_cache_use },
1096 { NULL, NULL }
1097 }
1098};
1099
1100struct applet http_cache_applet = {
1101 .obj_type = OBJ_TYPE_APPLET,
1102 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001103 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001104 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001105};
1106
1107__attribute__((constructor))
1108static void __cache_init(void)
1109{
1110 cfg_register_section("cache", cfg_parse_cache, cfg_post_parse_section_cache);
1111 cfg_register_postparser("cache", cfg_cache_postparser);
William Lallemand1f49a362017-11-21 20:01:26 +01001112 cli_register_kw(&cli_kws);
William Lallemand41db4602017-10-30 11:15:51 +01001113 http_res_keywords_register(&http_res_actions);
1114 http_req_keywords_register(&http_req_actions);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001115 pool_head_cache_st = create_pool("cache_st", sizeof(struct cache_st), MEM_F_SHARED);
William Lallemand41db4602017-10-30 11:15:51 +01001116}
1117