blob: 864857c3f346b77f4b4c5155df6473b6fb8ee405 [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>
27#include <proto/proto_http.h>
28#include <proto/log.h>
29#include <proto/stream.h>
30#include <proto/stream_interface.h>
31#include <proto/shctx.h>
32
William Lallemand41db4602017-10-30 11:15:51 +010033
34#include <common/cfgparse.h>
35#include <common/hash.h>
36
37/* flt_cache_store */
38
39static const char *cache_store_flt_id = "cache store filter";
40
Willy Tarreaubafbe012017-11-24 17:34:44 +010041static struct pool_head *pool_head_cache_st = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +010042
William Lallemand41db4602017-10-30 11:15:51 +010043struct applet http_cache_applet;
44
45struct flt_ops cache_ops;
46
47struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010048 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010049 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010050 unsigned int maxage; /* max-age */
51 unsigned int maxblocks;
52 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010053};
54
55/*
56 * cache ctx for filters
57 */
58struct cache_st {
59 int hdrs_len;
60 struct shared_block *first_block;
61};
62
63struct cache_entry {
64 unsigned int latest_validation; /* latest validation date */
65 unsigned int expire; /* expiration date */
66 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010067 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010068 unsigned char data[0];
69};
70
71#define CACHE_BLOCKSIZE 1024
72
73static struct list caches = LIST_HEAD_INIT(caches);
74static struct cache *tmp_cache_config = NULL;
75
William Lallemandf528fff2017-11-23 19:43:17 +010076struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010077{
78 struct eb32_node *node;
79 struct cache_entry *entry;
80
William Lallemandf528fff2017-11-23 19:43:17 +010081 node = eb32_lookup(&cache->entries, (*(unsigned int *)hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010082 if (!node)
83 return NULL;
84
85 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +010086
87 /* if that's not the right node */
88 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
89 return NULL;
90
William Lallemand08727662017-11-21 20:01:27 +010091 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +010092 return entry;
William Lallemand08727662017-11-21 20:01:27 +010093 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +010094 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +010095 entry->eb.key = 0;
96 }
William Lallemand4da3f8a2017-10-31 14:33:34 +010097 return NULL;
98
99}
100
101static inline struct shared_context *shctx_ptr(struct cache *cache)
102{
103 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
104}
105
William Lallemand77c11972017-10-31 20:43:01 +0100106static inline struct shared_block *block_ptr(struct cache_entry *entry)
107{
108 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
109}
110
111
112
William Lallemand41db4602017-10-30 11:15:51 +0100113static int
114cache_store_init(struct proxy *px, struct flt_conf *f1conf)
115{
116 return 0;
117}
118
William Lallemand4da3f8a2017-10-31 14:33:34 +0100119static int
120cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
121{
122 if (!(chn->flags & CF_ISRESP))
123 return 1;
124
125 if (filter->ctx == NULL) {
126 struct cache_st *st;
127
Willy Tarreaubafbe012017-11-24 17:34:44 +0100128 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100129 if (st == NULL)
130 return -1;
131
132 st->hdrs_len = 0;
133 st->first_block = NULL;
134 filter->ctx = st;
135 }
136
137 register_data_filter(s, chn, filter);
138
139 return 1;
140}
141
142static int
William Lallemand49dc0482017-11-24 14:33:54 +0100143cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
144{
145 struct cache_st *st = filter->ctx;
146 struct cache *cache = filter->config->conf;
147 struct shared_context *shctx = shctx_ptr(cache);
148
149 if (!(chn->flags & CF_ISRESP))
150 return 1;
151
152 /* Everything should be released in the http_end filter, but we need to do it
153 * there too, in case of errors */
154
155 if (st && st->first_block) {
156
157 shctx_lock(shctx);
158 shctx_row_dec_hot(shctx, st->first_block);
159 shctx_unlock(shctx);
160
161 }
162 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100163 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100164 filter->ctx = NULL;
165 }
166
167 return 1;
168}
169
170
171static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100172cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
173{
174 struct cache_st *st = filter->ctx;
175
William Lallemand4da3f8a2017-10-31 14:33:34 +0100176 if (!(msg->chn->flags & CF_ISRESP) || !st)
177 return 1;
178
William Lallemand9d5f54d2017-11-14 14:39:22 +0100179 st->hdrs_len = msg->sov;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100180
181 return 1;
182}
183
184static int
185cache_store_http_forward_data(struct stream *s, struct filter *filter,
186 struct http_msg *msg, unsigned int len)
187{
188 struct cache_st *st = filter->ctx;
189 struct shared_context *shctx = shctx_ptr((struct cache *)filter->config->conf);
William Lallemand08727662017-11-21 20:01:27 +0100190 struct cache_entry *object;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100191 int ret;
192
193 /*
194 * We need to skip the HTTP headers first, because we saved them in the
195 * http-response action.
196 */
197 if (!(msg->chn->flags & CF_ISRESP) || !st)
198 return len;
199
200 if (!len) {
201 /* Nothing to foward */
202 ret = len;
203 }
William Lallemand10935bc2017-11-14 14:39:23 +0100204 else if (st->hdrs_len >= len) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100205 /* Forward part of headers */
206 ret = len;
207 st->hdrs_len -= len;
208 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100209 else {
William Lallemand10935bc2017-11-14 14:39:23 +0100210 /* Forward data */
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100211 if (filter->ctx && st->first_block) {
212 /* disable buffering if too much data (never greater than a buffer size */
William Lallemand10935bc2017-11-14 14:39:23 +0100213 if (len - st->hdrs_len > global.tune.bufsize - global.tune.maxrewrite - st->first_block->len) {
William Lallemande1533f52017-11-14 14:39:24 +0100214 disable_cache:
William Lallemand08727662017-11-21 20:01:27 +0100215 object = (struct cache_entry *)st->first_block->data;
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100216 filter->ctx = NULL; /* disable cache */
217 shctx_lock(shctx);
218 shctx_row_dec_hot(shctx, st->first_block);
William Lallemand08727662017-11-21 20:01:27 +0100219 object->eb.key = 0;
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100220 shctx_unlock(shctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100221 pool_free(pool_head_cache_st, st);
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100222 } else {
William Lallemand10935bc2017-11-14 14:39:23 +0100223 /* Skip remaining headers to fill the cache */
224 b_adv(msg->chn->buf, st->hdrs_len);
225 ret = shctx_row_data_append(shctx,
226 st->first_block,
227 (unsigned char *)bi_ptr(msg->chn->buf),
228 MIN(bi_contig_data(msg->chn->buf), len - st->hdrs_len));
229 /* Rewind the buffer to forward all data */
230 b_rew(msg->chn->buf, st->hdrs_len);
William Lallemandbcd91012017-11-28 11:33:02 +0100231 st->hdrs_len = 0;
William Lallemande1533f52017-11-14 14:39:24 +0100232 if (ret)
233 goto disable_cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100234 }
235 }
William Lallemand10935bc2017-11-14 14:39:23 +0100236 ret = len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100237 }
238
239 if ((ret != len) ||
240 (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret))
241 task_wakeup(s->task, TASK_WOKEN_MSG);
242
243 return ret;
244}
245
246static int
247cache_store_http_end(struct stream *s, struct filter *filter,
248 struct http_msg *msg)
249{
250 struct cache_st *st = filter->ctx;
251 struct cache *cache = filter->config->conf;
252 struct shared_context *shctx = shctx_ptr(cache);
253 struct cache_entry *object;
254
255 if (!(msg->chn->flags & CF_ISRESP))
256 return 1;
257
258 if (st && st->first_block) {
259
260 object = (struct cache_entry *)st->first_block->data;
261
262 /* does not need to test if the insertion worked, if it
263 * doesn't, the blocks will be reused anyway */
264
265 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100266 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
267 object->eb.key = 0;
268 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100269 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100270 shctx_row_dec_hot(shctx, st->first_block);
271 shctx_unlock(shctx);
272
273 }
274 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100275 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100276 filter->ctx = NULL;
277 }
278
279 return 1;
280}
281
282 /*
283 * This intends to be used when checking HTTP headers for some
284 * word=value directive. Return a pointer to the first character of value, if
285 * the word was not found or if there wasn't any value assigned ot it return NULL
286 */
287char *directive_value(const char *sample, int slen, const char *word, int wlen)
288{
289 int st = 0;
290
291 if (slen < wlen)
292 return 0;
293
294 while (wlen) {
295 char c = *sample ^ *word;
296 if (c && c != ('A' ^ 'a'))
297 return NULL;
298 sample++;
299 word++;
300 slen--;
301 wlen--;
302 }
303
304 while (slen) {
305 if (st == 0) {
306 if (*sample != '=')
307 return NULL;
308 sample++;
309 slen--;
310 st = 1;
311 continue;
312 } else {
313 return (char *)sample;
314 }
315 }
316
317 return NULL;
318}
319
320/*
321 * Return the maxage in seconds of an HTTP response.
322 * Compute the maxage using either:
323 * - the assigned max-age of the cache
324 * - the s-maxage directive
325 * - the max-age directive
326 * - (Expires - Data) headers
327 * - the default-max-age of the cache
328 *
329 */
William Lallemand49b44532017-11-24 18:53:43 +0100330int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100331{
332 struct http_txn *txn = s->txn;
333 struct hdr_ctx ctx;
334
335 int smaxage = -1;
336 int maxage = -1;
337
338
William Lallemand4da3f8a2017-10-31 14:33:34 +0100339 ctx.idx = 0;
340
341 /* loop on the Cache-Control values */
342 while (http_find_header2("Cache-Control", 13, s->res.buf->p, &txn->hdr_idx, &ctx)) {
343 char *directive = ctx.line + ctx.val;
344 char *value;
345
346 value = directive_value(directive, ctx.vlen, "s-maxage", 8);
347 if (value) {
348 struct chunk *chk = get_trash_chunk();
349
350 chunk_strncat(chk, value, ctx.vlen - 8 + 1);
351 chunk_strncat(chk, "", 1);
352 maxage = atoi(chk->str);
353 }
354
355 value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
356 if (value) {
357 struct chunk *chk = get_trash_chunk();
358
359 chunk_strncat(chk, value, ctx.vlen - 7 + 1);
360 chunk_strncat(chk, "", 1);
361 smaxage = atoi(chk->str);
362 }
363 }
364
365 /* TODO: Expires - Data */
366
367
368 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100369 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100370
371 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100372 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100373
William Lallemand49b44532017-11-24 18:53:43 +0100374 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100375
376}
377
378
William Lallemanda400a3a2017-11-20 19:13:12 +0100379static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
380{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200381 struct cache_entry *object = (struct cache_entry *)block->data;
382
383 if (first == block && object->eb.key)
384 eb32_delete(&object->eb);
385 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100386}
387
William Lallemand41db4602017-10-30 11:15:51 +0100388/*
389 * This fonction will store the headers of the response in a buffer and then
390 * register a filter to store the data
391 */
392enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
393 struct session *sess, struct stream *s, int flags)
394{
William Lallemand4da3f8a2017-10-31 14:33:34 +0100395 struct http_txn *txn = s->txn;
396 struct http_msg *msg = &txn->rsp;
397 struct filter *filter;
398 struct hdr_ctx ctx;
399 struct shared_block *first = NULL;
William Lallemand49b44532017-11-24 18:53:43 +0100400 struct cache *cache = (struct cache *)rule->arg.act.p[0];
401 struct shared_context *shctx = shctx_ptr(cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100402 struct cache_entry *object;
403
404
405 /* Don't cache if the response came from a cache */
406 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
407 s->target == &http_cache_applet.obj_type) {
408 goto out;
409 }
410
411 /* cache only HTTP/1.1 */
412 if (!(txn->req.flags & HTTP_MSGF_VER_11))
413 goto out;
414
William Lallemand18f133a2017-11-08 11:25:15 +0100415 /* does not cache if Content-Length unknown */
416 if (!(msg->flags & HTTP_MSGF_CNT_LEN))
417 goto out;
418
William Lallemand4da3f8a2017-10-31 14:33:34 +0100419 /* cache only GET method */
420 if (txn->meth != HTTP_METH_GET)
421 goto out;
422
423 /* cache only 200 status code */
424 if (txn->status != 200)
425 goto out;
426
427 /* Does not manage Vary at the moment. We will need a secondary key later for that */
428 ctx.idx = 0;
429 if (http_find_header2("Vary", 4, txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx))
430 goto out;
431
Willy Tarreaufaf29092017-12-21 15:59:17 +0100432 check_response_for_cacheability(s, &s->res);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100433
Willy Tarreaud4569d12017-12-22 18:03:04 +0100434 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100435 goto out;
436
William Lallemand9d5f54d2017-11-14 14:39:22 +0100437 if ((msg->sov + msg->body_len) > (global.tune.bufsize - global.tune.maxrewrite))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100438 goto out;
439
440 shctx_lock(shctx);
441
William Lallemand9d5f54d2017-11-14 14:39:22 +0100442 first = shctx_row_reserve_hot(shctx, sizeof(struct cache_entry) + msg->sov + msg->body_len);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100443 if (!first) {
444 shctx_unlock(shctx);
445 goto out;
446 }
447 shctx_unlock(shctx);
448
449 /* reserve space for the cache_entry structure */
450 first->len = sizeof(struct cache_entry);
451
452 /* cache the headers in a http action because it allows to chose what
453 * to cache, for example you might want to cache a response before
454 * modifying some HTTP headers, or on the contrary after modifying
455 * those headers.
456 */
457
458 /* does not need to be locked because it's in the "hot" list,
459 * copy the headers */
William Lallemand9d5f54d2017-11-14 14:39:22 +0100460 if (shctx_row_data_append(shctx, first, (unsigned char *)s->res.buf->p, msg->sov) < 0)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100461 goto out;
462
463 /* register the buffer in the filter ctx for filling it with data*/
464 if (!LIST_ISEMPTY(&s->strm_flt.filters)) {
465 list_for_each_entry(filter, &s->strm_flt.filters, list) {
466 if (filter->config->id == cache_store_flt_id &&
467 filter->config->conf == rule->arg.act.p[0]) {
468 if (filter->ctx) {
469 struct cache_st *cache_ctx = filter->ctx;
Willy Tarreauc9bd34c2017-12-22 17:42:46 +0100470 struct cache_entry *old;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100471
472 cache_ctx->first_block = first;
473 object = (struct cache_entry *)first->data;
474
William Lallemandf528fff2017-11-23 19:43:17 +0100475 object->eb.key = (*(unsigned int *)&txn->cache_hash);
476 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100477 /* Insert the node later on caching success */
478
479 shctx_lock(shctx);
Willy Tarreauc9bd34c2017-12-22 17:42:46 +0100480
481 old = entry_exist(cache, txn->cache_hash);
482 if (old) {
483 eb32_delete(&old->eb);
484 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100485 }
486 shctx_unlock(shctx);
487
488 /* store latest value and expiration time */
489 object->latest_validation = now.tv_sec;
William Lallemand49b44532017-11-24 18:53:43 +0100490 object->expire = now.tv_sec + http_calc_maxage(s, cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100491 }
492 return ACT_RET_CONT;
493 }
494 }
495 }
496
497out:
498 /* if does not cache */
499 if (first) {
William Lallemand08727662017-11-21 20:01:27 +0100500 object = (struct cache_entry *)first->data;
501
William Lallemand4da3f8a2017-10-31 14:33:34 +0100502 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100503 first->len = 0;
504 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100505 shctx_row_dec_hot(shctx, first);
506 shctx_unlock(shctx);
507 }
508
William Lallemand41db4602017-10-30 11:15:51 +0100509 return ACT_RET_CONT;
510}
511
William Lallemand77c11972017-10-31 20:43:01 +0100512#define HTTP_CACHE_INIT 0
513#define HTTP_CACHE_FWD 1
514#define HTTP_CACHE_END 2
515
William Lallemandecb73b12017-11-24 14:33:55 +0100516static void http_cache_applet_release(struct appctx *appctx)
517{
518 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
519 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
520 struct shared_block *first = block_ptr(cache_ptr);
521
522 shctx_lock(shctx_ptr(cache));
523 shctx_row_dec_hot(shctx_ptr(cache), first);
524 shctx_unlock(shctx_ptr(cache));
525}
526
William Lallemand77c11972017-10-31 20:43:01 +0100527static void http_cache_io_handler(struct appctx *appctx)
528{
529 struct stream_interface *si = appctx->owner;
530 struct channel *res = si_ic(si);
531 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
532 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
533 struct shared_context *shctx = shctx_ptr(cache);
534 struct shared_block *first = block_ptr(cache_ptr);
535
536 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
537 goto out;
538
539 /* Check if the input buffer is avalaible. */
540 if (res->buf->size == 0) {
541 si_applet_cant_put(si);
542 goto out;
543 }
544
545 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
546 appctx->st0 = HTTP_CACHE_END;
547
548 /* buffer are aligned there, should be fine */
549 if (appctx->st0 == HTTP_CACHE_INIT) {
550 int len = first->len - sizeof(struct cache_entry);
551 if ((shctx_row_data_get(shctx, first, (unsigned char *)bi_end(res->buf), sizeof(struct cache_entry), len)) != 0) {
William Lallemanda71cd1d2017-11-24 18:53:42 +0100552 /* should never get there, because at the moment, a
553 * cache object can never be bigger than a buffer */
554 abort();
William Lallemand55e76742017-11-21 20:01:28 +0100555
William Lallemand77c11972017-10-31 20:43:01 +0100556 si_applet_cant_put(si);
557 goto out;
558 }
559 res->buf->i += len;
560 res->total += len;
561 appctx->st0 = HTTP_CACHE_FWD;
562 }
563
564 if (appctx->st0 == HTTP_CACHE_FWD) {
565 /* eat the whole request */
566 co_skip(si_oc(si), si_ob(si)->o); // NOTE: when disabled does not repport the correct status code
567 res->flags |= CF_READ_NULL;
568 si_shutr(si);
569 }
570
571 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
572 si_shutw(si);
573out:
574 ;
575}
576
William Lallemand41db4602017-10-30 11:15:51 +0100577enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
578 struct act_rule *rule, char **err)
579{
580 struct flt_conf *fconf;
581 int cur_arg = *orig_arg;
582 rule->action = ACT_CUSTOM;
583 rule->action_ptr = http_action_store_cache;
584
585 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
586 memprintf(err, "expects a cache name");
587 return ACT_RET_PRS_ERR;
588 }
589
590 /* check if a cache filter was already registered with this cache
591 * name, if that's the case, must use it. */
592 list_for_each_entry(fconf, &proxy->filter_configs, list) {
593 if (fconf->id == cache_store_flt_id && !strcmp((char *)fconf->conf, args[cur_arg])) {
594 rule->arg.act.p[0] = fconf->conf;
595 (*orig_arg)++;
596 /* filter already registered */
597 return ACT_RET_PRS_OK;
598 }
599 }
600
601 rule->arg.act.p[0] = strdup(args[cur_arg]);
602 if (!rule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100603 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100604 err++;
605 goto err;
606 }
607 /* register a filter to fill the cache buffer */
608 fconf = calloc(1, sizeof(*fconf));
609 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100610 ha_alert("config: %s '%s': out of memory\n",
611 proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100612 err++;
613 goto err;
614 }
615 fconf->id = cache_store_flt_id;
616 fconf->conf = rule->arg.act.p[0]; /* store the proxy name */
617 fconf->ops = &cache_ops;
618 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
619
620 (*orig_arg)++;
621
622 return ACT_RET_PRS_OK;
623
624err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100625 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100626}
627
William Lallemandf528fff2017-11-23 19:43:17 +0100628/* This produces a sha1 hash of the concatenation of the first
629 * occurrence of the Host header followed by the path component if it
630 * begins with a slash ('/'). */
631int sha1_hosturi(struct http_txn *txn)
632{
633 struct hdr_ctx ctx;
634
635 blk_SHA_CTX sha1_ctx;
636 struct chunk *trash;
637 char *path;
638 char *end;
639 trash = get_trash_chunk();
640
641 /* retrive the host */
642 ctx.idx = 0;
643 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx))
644 return 0;
645 chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen);
646
647 /* now retrieve the path */
648 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
649 path = http_get_path(txn);
650 if (!path)
651 return 0;
652 chunk_strncat(trash, path, end - path);
653
654 /* hash everything */
655 blk_SHA1_Init(&sha1_ctx);
656 blk_SHA1_Update(&sha1_ctx, trash->str, trash->len);
657 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
658
659 return 1;
660}
661
662
William Lallemand41db4602017-10-30 11:15:51 +0100663
664enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
665 struct session *sess, struct stream *s, int flags)
666{
William Lallemand77c11972017-10-31 20:43:01 +0100667
William Lallemand77c11972017-10-31 20:43:01 +0100668 struct cache_entry *res;
William Lallemand77c11972017-10-31 20:43:01 +0100669 struct cache *cache = (struct cache *)rule->arg.act.p[0];
670
Willy Tarreau504455c2017-12-22 17:47:35 +0100671 check_request_for_cacheability(s, &s->req);
672 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
673 return ACT_RET_CONT;
674
Willy Tarreau7704b1e2017-12-22 16:32:43 +0100675 if (!sha1_hosturi(s->txn))
676 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +0100677
Willy Tarreau504455c2017-12-22 17:47:35 +0100678 if (s->txn->flags & TX_CACHE_IGNORE)
679 return ACT_RET_CONT;
680
William Lallemanda400a3a2017-11-20 19:13:12 +0100681 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +0100682 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +0100683 if (res) {
684 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +0100685 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
686 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +0100687 s->target = &http_cache_applet.obj_type;
688 if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
689 appctx->st0 = HTTP_CACHE_INIT;
690 appctx->rule = rule;
691 appctx->ctx.cache.entry = res;
Olivier Houchardfccf8402017-11-01 14:04:02 +0100692 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +0100693 } else {
William Lallemand55e76742017-11-21 20:01:28 +0100694 shctx_lock(shctx_ptr(cache));
695 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
696 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100697 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +0100698 }
699 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100700 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100701 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +0100702}
703
704
705enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
706 struct act_rule *rule, char **err)
707{
708 int cur_arg = *orig_arg;
709
710 rule->action = ACT_CUSTOM;
711 rule->action_ptr = http_action_req_cache_use;
712
713 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
714 memprintf(err, "expects a cache name");
715 return ACT_RET_PRS_ERR;
716 }
717
718 rule->arg.act.p[0] = strdup(args[cur_arg]);
719 if (!rule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100720 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100721 err++;
722 goto err;
723 }
724
725 (*orig_arg)++;
726 return ACT_RET_PRS_OK;
727
728err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100729 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100730
731}
732
733int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
734{
735 int err_code = 0;
736
737 if (strcmp(args[0], "cache") == 0) { /* new cache section */
738
739 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100740 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
741 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100742 err_code |= ERR_ALERT | ERR_ABORT;
743 goto out;
744 }
745
746 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
747 err_code |= ERR_ABORT;
748 goto out;
749 }
750
751 if (tmp_cache_config == NULL) {
752 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
753 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100754 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +0100755 err_code |= ERR_ALERT | ERR_ABORT;
756 goto out;
757 }
758
759 strlcpy2(tmp_cache_config->id, args[1], 33);
760 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100761 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
762 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +0100763 err_code |= ERR_WARN;
764 }
William Lallemand49b44532017-11-24 18:53:43 +0100765 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +0100766 tmp_cache_config->maxblocks = 0;
767 }
768 } else if (strcmp(args[0], "total-max-size") == 0) {
769 int maxsize;
770
771 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
772 err_code |= ERR_ABORT;
773 goto out;
774 }
775
776 /* size in megabytes */
777 maxsize = atoi(args[1]) * 1024 * 1024 / CACHE_BLOCKSIZE;
778 tmp_cache_config->maxblocks = maxsize;
779
William Lallemand49b44532017-11-24 18:53:43 +0100780 } else if (strcmp(args[0], "max-age") == 0) {
781 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
782 err_code |= ERR_ABORT;
783 goto out;
784 }
785
786 if (!*args[1]) {
787 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
788 file, linenum, args[0]);
789 err_code |= ERR_WARN;
790 }
791
792 tmp_cache_config->maxage = atoi(args[1]);
William Lallemand41db4602017-10-30 11:15:51 +0100793 } else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100794 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100795 err_code |= ERR_ALERT | ERR_FATAL;
796 goto out;
797 }
798out:
799 return err_code;
800}
801
802/* once the cache section is parsed */
803
804int cfg_post_parse_section_cache()
805{
806 struct shared_context *shctx;
807 int err_code = 0;
808 int ret_shctx;
809
810 if (tmp_cache_config) {
811 struct cache *cache;
812
813 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100814 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +0100815 err_code |= ERR_FATAL | ERR_ALERT;
816 goto out;
817 }
818
819 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100820
William Lallemand41db4602017-10-30 11:15:51 +0100821 if (ret_shctx < 0) {
822 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100823 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +0100824 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100825 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +0100826
827 err_code |= ERR_FATAL | ERR_ALERT;
828 goto out;
829 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100830 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +0100831 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
832 cache = (struct cache *)shctx->data;
833 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +0100834 LIST_ADDQ(&caches, &cache->list);
835 }
836out:
837 free(tmp_cache_config);
838 tmp_cache_config = NULL;
839 return err_code;
840
841}
842
843/*
844 * Resolve the cache name to a pointer once the file is completely read.
845 */
846int cfg_cache_postparser()
847{
848 struct act_rule *hresrule, *hrqrule;
849 void *cache_ptr;
850 struct cache *cache;
851 struct proxy *curproxy = NULL;
852 int err = 0;
853 struct flt_conf *fconf;
854
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100855 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
William Lallemand41db4602017-10-30 11:15:51 +0100856
857 /* resolve the http response cache name to a ptr in the action rule */
858 list_for_each_entry(hresrule, &curproxy->http_res_rules, list) {
859 if (hresrule->action != ACT_CUSTOM ||
860 hresrule->action_ptr != http_action_store_cache)
861 continue;
862
863 cache_ptr = hresrule->arg.act.p[0];
864
865 list_for_each_entry(cache, &caches, list) {
866 if (!strcmp(cache->id, cache_ptr)) {
867 /* don't free there, it's still used in the filter conf */
868 cache_ptr = cache;
869 break;
870 }
871 }
872
873 if (cache_ptr == hresrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100874 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n",
875 curproxy->id, (char *)hresrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100876 err++;
877 }
878
879 hresrule->arg.act.p[0] = cache_ptr;
880 }
881
882 /* resolve the http request cache name to a ptr in the action rule */
883 list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) {
884 if (hrqrule->action != ACT_CUSTOM ||
885 hrqrule->action_ptr != http_action_req_cache_use)
886 continue;
887
888 cache_ptr = hrqrule->arg.act.p[0];
889
890 list_for_each_entry(cache, &caches, list) {
891 if (!strcmp(cache->id, cache_ptr)) {
892 free(cache_ptr);
893 cache_ptr = cache;
894 break;
895 }
896 }
897
898 if (cache_ptr == hrqrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100899 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n",
900 curproxy->id, (char *)hrqrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100901 err++;
902 }
903
904 hrqrule->arg.act.p[0] = cache_ptr;
905 }
906
907 /* resolve the cache name to a ptr in the filter config */
908 list_for_each_entry(fconf, &curproxy->filter_configs, list) {
909
William Lallemand9c54c532017-11-02 16:38:42 +0100910 if (fconf->id != cache_store_flt_id)
911 continue;
912
William Lallemand41db4602017-10-30 11:15:51 +0100913 cache_ptr = fconf->conf;
914
915 list_for_each_entry(cache, &caches, list) {
916 if (!strcmp(cache->id, cache_ptr)) {
917 /* there can be only one filter per cache, so we free it there */
918 free(cache_ptr);
919 cache_ptr = cache;
920 break;
921 }
922 }
923
924 if (cache_ptr == fconf->conf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100925 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
926 curproxy->id, (char *)fconf->conf);
William Lallemand41db4602017-10-30 11:15:51 +0100927 err++;
928 }
929 fconf->conf = cache_ptr;
930 }
931 }
932 return err;
933}
934
935
936struct flt_ops cache_ops = {
937 .init = cache_store_init,
938
William Lallemand4da3f8a2017-10-31 14:33:34 +0100939 /* Handle channels activity */
940 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +0100941 .channel_end_analyze = cache_store_chn_end_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +0100942
943 /* Filter HTTP requests and responses */
944 .http_headers = cache_store_http_headers,
945 .http_end = cache_store_http_end,
946
947 .http_forward_data = cache_store_http_forward_data,
948
William Lallemand41db4602017-10-30 11:15:51 +0100949};
950
William Lallemand1f49a362017-11-21 20:01:26 +0100951static int cli_parse_show_cache(char **args, struct appctx *appctx, void *private)
952{
953 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
954 return 1;
955
956 return 0;
957}
958
959static int cli_io_handler_show_cache(struct appctx *appctx)
960{
961 struct cache* cache = appctx->ctx.cli.p0;
962 struct stream_interface *si = appctx->owner;
963
William Lallemand1f49a362017-11-21 20:01:26 +0100964 if (cache == NULL) {
965 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
966 }
967
968 list_for_each_entry_from(cache, &caches, list) {
969 struct eb32_node *node = NULL;
970 unsigned int next_key;
971 struct cache_entry *entry;
972
William Lallemand1f49a362017-11-21 20:01:26 +0100973 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +0200974 if (!next_key) {
975 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
976 if (ci_putchk(si_ic(si), &trash) == -1) {
977 si_applet_cant_put(si);
978 return 0;
979 }
980 }
William Lallemand1f49a362017-11-21 20:01:26 +0100981
982 appctx->ctx.cli.p0 = cache;
983
984 while (1) {
985
986 shctx_lock(shctx_ptr(cache));
987 node = eb32_lookup_ge(&cache->entries, next_key);
988 if (!node) {
989 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +0200990 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +0100991 break;
992 }
993
994 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +0200995 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 +0100996
997 next_key = node->key + 1;
998 appctx->ctx.cli.i0 = next_key;
999
1000 shctx_unlock(shctx_ptr(cache));
1001
1002 if (ci_putchk(si_ic(si), &trash) == -1) {
1003 si_applet_cant_put(si);
1004 return 0;
1005 }
1006 }
1007
1008 }
1009
1010 return 1;
1011
1012}
1013
1014static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001015 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1016 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001017}};
1018
1019
William Lallemand41db4602017-10-30 11:15:51 +01001020static struct action_kw_list http_res_actions = {
1021 .kw = {
1022 { "cache-store", parse_cache_store },
1023 { NULL, NULL }
1024 }
1025};
1026
1027static struct action_kw_list http_req_actions = {
1028 .kw = {
1029 { "cache-use", parse_cache_use },
1030 { NULL, NULL }
1031 }
1032};
1033
1034struct applet http_cache_applet = {
1035 .obj_type = OBJ_TYPE_APPLET,
1036 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001037 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001038 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001039};
1040
1041__attribute__((constructor))
1042static void __cache_init(void)
1043{
1044 cfg_register_section("cache", cfg_parse_cache, cfg_post_parse_section_cache);
1045 cfg_register_postparser("cache", cfg_cache_postparser);
William Lallemand1f49a362017-11-21 20:01:26 +01001046 cli_register_kw(&cli_kws);
William Lallemand41db4602017-10-30 11:15:51 +01001047 http_res_keywords_register(&http_res_actions);
1048 http_req_keywords_register(&http_req_actions);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001049 pool_head_cache_st = create_pool("cache_st", sizeof(struct cache_st), MEM_F_SHARED);
William Lallemand41db4602017-10-30 11:15:51 +01001050}
1051