blob: 3b266fbde49fdbfa3d0a59fe21643d98ada5d03b [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>
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010026#include <proto/http_htx.h>
27#include <proto/htx.h>
William Lallemand41db4602017-10-30 11:15:51 +010028#include <proto/filters.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020029#include <proto/http_rules.h>
William Lallemand41db4602017-10-30 11:15:51 +010030#include <proto/proto_http.h>
31#include <proto/log.h>
32#include <proto/stream.h>
33#include <proto/stream_interface.h>
34#include <proto/shctx.h>
35
William Lallemand41db4602017-10-30 11:15:51 +010036
37#include <common/cfgparse.h>
38#include <common/hash.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010039#include <common/initcall.h>
William Lallemand41db4602017-10-30 11:15:51 +010040
41/* flt_cache_store */
Christopher Faulet1f672c52018-12-03 14:30:41 +010042#define CACHE_F_LEGACY_HTTP 0x00000001 /* The cache is used to store raw HTTP
43 * messages (legacy implementation) */
44#define CACHE_F_HTX 0x00000002 /* The cache is used to store HTX messages */
William Lallemand41db4602017-10-30 11:15:51 +010045
46static const char *cache_store_flt_id = "cache store filter";
47
48struct applet http_cache_applet;
49
50struct flt_ops cache_ops;
51
52struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010053 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010054 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010055 unsigned int maxage; /* max-age */
56 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020057 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010058 char id[33]; /* cache name */
Christopher Faulet1f672c52018-12-03 14:30:41 +010059 unsigned int flags; /* CACHE_F_* */
William Lallemand41db4602017-10-30 11:15:51 +010060};
61
62/*
63 * cache ctx for filters
64 */
65struct cache_st {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010066 int hdrs_len; // field used in legacy mode only
William Lallemand41db4602017-10-30 11:15:51 +010067 struct shared_block *first_block;
68};
69
70struct cache_entry {
71 unsigned int latest_validation; /* latest validation date */
72 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020073 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010074 unsigned int eoh; /* Origin server end of headers offset. */ // field used in legacy mode only
75
76 unsigned int hdrs_len; // field used in HTX mode only
77 unsigned int data_len; // field used in HTX mode only
78
William Lallemand41db4602017-10-30 11:15:51 +010079 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010080 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010081 unsigned char data[0];
82};
83
84#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010085#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010086
87static struct list caches = LIST_HEAD_INIT(caches);
88static struct cache *tmp_cache_config = NULL;
89
Willy Tarreau8ceae722018-11-26 11:58:30 +010090DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
91
William Lallemandf528fff2017-11-23 19:43:17 +010092struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010093{
94 struct eb32_node *node;
95 struct cache_entry *entry;
96
William Lallemandf528fff2017-11-23 19:43:17 +010097 node = eb32_lookup(&cache->entries, (*(unsigned int *)hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010098 if (!node)
99 return NULL;
100
101 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100102
103 /* if that's not the right node */
104 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
105 return NULL;
106
William Lallemand08727662017-11-21 20:01:27 +0100107 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100108 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100109 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100110 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100111 entry->eb.key = 0;
112 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100113 return NULL;
114
115}
116
117static inline struct shared_context *shctx_ptr(struct cache *cache)
118{
119 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
120}
121
William Lallemand77c11972017-10-31 20:43:01 +0100122static inline struct shared_block *block_ptr(struct cache_entry *entry)
123{
124 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
125}
126
127
128
William Lallemand41db4602017-10-30 11:15:51 +0100129static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100130cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100131{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100132 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100133 return 0;
134}
135
William Lallemand4da3f8a2017-10-31 14:33:34 +0100136static int
137cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
138{
139 if (!(chn->flags & CF_ISRESP))
140 return 1;
141
142 if (filter->ctx == NULL) {
143 struct cache_st *st;
144
Willy Tarreaubafbe012017-11-24 17:34:44 +0100145 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100146 if (st == NULL)
147 return -1;
148
149 st->hdrs_len = 0;
150 st->first_block = NULL;
151 filter->ctx = st;
152 }
153
William Lallemand4da3f8a2017-10-31 14:33:34 +0100154 return 1;
155}
156
157static int
William Lallemand49dc0482017-11-24 14:33:54 +0100158cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
159{
160 struct cache_st *st = filter->ctx;
161 struct cache *cache = filter->config->conf;
162 struct shared_context *shctx = shctx_ptr(cache);
163
164 if (!(chn->flags & CF_ISRESP))
165 return 1;
166
167 /* Everything should be released in the http_end filter, but we need to do it
168 * there too, in case of errors */
169
170 if (st && st->first_block) {
171
172 shctx_lock(shctx);
173 shctx_row_dec_hot(shctx, st->first_block);
174 shctx_unlock(shctx);
175
176 }
177 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100178 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100179 filter->ctx = NULL;
180 }
181
182 return 1;
183}
184
185
186static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100187cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
188{
189 struct cache_st *st = filter->ctx;
190
William Lallemand4da3f8a2017-10-31 14:33:34 +0100191 if (!(msg->chn->flags & CF_ISRESP) || !st)
192 return 1;
193
Christopher Faulet67658c92018-12-06 21:59:39 +0100194 if (st->first_block) {
195 register_data_filter(s, msg->chn, filter);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100196 if (!IS_HTX_STRM(s))
197 st->hdrs_len = msg->sov;
Christopher Faulet67658c92018-12-06 21:59:39 +0100198 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100199 return 1;
200}
201
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200202static inline void disable_cache_entry(struct cache_st *st,
203 struct filter *filter, struct shared_context *shctx)
204{
205 struct cache_entry *object;
206
207 object = (struct cache_entry *)st->first_block->data;
208 filter->ctx = NULL; /* disable cache */
209 shctx_lock(shctx);
210 shctx_row_dec_hot(shctx, st->first_block);
211 object->eb.key = 0;
212 shctx_unlock(shctx);
213 pool_free(pool_head_cache_st, st);
214}
215
William Lallemand4da3f8a2017-10-31 14:33:34 +0100216static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100217cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
218 unsigned int offset, unsigned int len)
219{
220 struct shared_context *shctx = shctx_ptr((struct cache *)filter->config->conf);
221 struct cache_st *st = filter->ctx;
222 struct htx *htx = htxbuf(&msg->chn->buf);
223 struct htx_blk *blk;
224 struct htx_ret htx_ret;
225 struct cache_entry *object;
226 int ret, to_forward = 0;
227
228 if (!len)
229 return len;
230
231 if (!st->first_block) {
232 unregister_data_filter(s, msg->chn, filter);
233 return len;
234 }
235 object = (struct cache_entry *)st->first_block->data;
236
237 htx_ret = htx_find_blk(htx, offset);
238 blk = htx_ret.blk;
239 offset = htx_ret.ret;
240
241 while (blk && len) {
242 struct shared_block *fb;
243 enum htx_blk_type type = htx_get_blk_type(blk);
244 uint32_t sz = htx_get_blksz(blk);
245 struct ist v;
246
247 switch (type) {
248 case HTX_BLK_UNUSED:
249 break;
250
251 case HTX_BLK_DATA:
252 case HTX_BLK_TLR:
253 v = htx_get_blk_value(htx, blk);
254 v.ptr += offset;
255 v.len -= offset;
256 if (v.len > len)
257 v.len = len;
258
259 shctx_lock(shctx);
260 fb = shctx_row_reserve_hot(shctx, st->first_block, v.len);
261 if (!fb) {
262 shctx_unlock(shctx);
263 goto no_cache;
264 }
265 shctx_unlock(shctx);
266
267 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
268 (unsigned char *)v.ptr, v.len);
269 if (ret < 0)
270 goto no_cache;
271
272 if (type == HTX_BLK_DATA)
273 object->data_len += v.len;
274 to_forward += v.len;
275 len -= v.len;
276 break;
277
278 default:
279 sz -= offset;
280 if (sz > len)
281 sz = len;
282 to_forward += sz;
283 len -= sz;
284 break;
285 }
286
287 offset = 0;
288 blk = htx_get_next_blk(htx, blk);
289 }
290
291 return to_forward;
292
293 no_cache:
294 disable_cache_entry(st, filter, shctx);
295 unregister_data_filter(s, msg->chn, filter);
296 return len;
297}
298
299static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100300cache_store_http_forward_data(struct stream *s, struct filter *filter,
301 struct http_msg *msg, unsigned int len)
302{
303 struct cache_st *st = filter->ctx;
304 struct shared_context *shctx = shctx_ptr((struct cache *)filter->config->conf);
305 int ret;
306
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200307 ret = 0;
308
William Lallemand4da3f8a2017-10-31 14:33:34 +0100309 /*
310 * We need to skip the HTTP headers first, because we saved them in the
311 * http-response action.
312 */
Christopher Faulet67658c92018-12-06 21:59:39 +0100313 if (!(msg->chn->flags & CF_ISRESP) || !st) {
314 /* should never happen */
315 unregister_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100316 return len;
Christopher Faulet67658c92018-12-06 21:59:39 +0100317 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100318
319 if (!len) {
Joseph Herlant8dae5b32018-11-15 14:07:53 -0800320 /* Nothing to forward */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100321 ret = len;
322 }
William Lallemand10935bc2017-11-14 14:39:23 +0100323 else if (st->hdrs_len >= len) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100324 /* Forward part of headers */
325 ret = len;
326 st->hdrs_len -= len;
327 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100328 else {
William Lallemand10935bc2017-11-14 14:39:23 +0100329 /* Forward data */
Christopher Faulet67658c92018-12-06 21:59:39 +0100330 if (st->first_block) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200331 int to_append, append;
332 struct shared_block *fb;
333
334 to_append = MIN(ci_contig_data(msg->chn), len - st->hdrs_len);
335
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200336 shctx_lock(shctx);
337 fb = shctx_row_reserve_hot(shctx, st->first_block, to_append);
338 if (!fb) {
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100339 shctx_unlock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200340 disable_cache_entry(st, filter, shctx);
Christopher Faulet67658c92018-12-06 21:59:39 +0100341 unregister_data_filter(s, msg->chn, filter);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200342 return len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100343 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200344 shctx_unlock(shctx);
345
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200346 /* Skip remaining headers to fill the cache */
347 c_adv(msg->chn, st->hdrs_len);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200348 append = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
349 (unsigned char *)ci_head(msg->chn), to_append);
350 ret = st->hdrs_len + to_append - append;
351 /* Rewind the buffer to forward all data */
352 c_rew(msg->chn, st->hdrs_len);
353 st->hdrs_len = 0;
Christopher Faulet67658c92018-12-06 21:59:39 +0100354 if (ret < 0) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200355 disable_cache_entry(st, filter, shctx);
Christopher Faulet67658c92018-12-06 21:59:39 +0100356 unregister_data_filter(s, msg->chn, filter);
357 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100358 }
Christopher Faulet67658c92018-12-06 21:59:39 +0100359 else {
360 /* should never happen */
361 unregister_data_filter(s, msg->chn, filter);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200362 ret = len;
Christopher Faulet67658c92018-12-06 21:59:39 +0100363 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100364 }
365
366 if ((ret != len) ||
367 (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret))
368 task_wakeup(s->task, TASK_WOKEN_MSG);
369
370 return ret;
371}
372
373static int
374cache_store_http_end(struct stream *s, struct filter *filter,
375 struct http_msg *msg)
376{
377 struct cache_st *st = filter->ctx;
378 struct cache *cache = filter->config->conf;
379 struct shared_context *shctx = shctx_ptr(cache);
380 struct cache_entry *object;
381
382 if (!(msg->chn->flags & CF_ISRESP))
383 return 1;
384
385 if (st && st->first_block) {
386
387 object = (struct cache_entry *)st->first_block->data;
388
389 /* does not need to test if the insertion worked, if it
390 * doesn't, the blocks will be reused anyway */
391
392 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100393 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
394 object->eb.key = 0;
395 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100396 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100397 shctx_row_dec_hot(shctx, st->first_block);
398 shctx_unlock(shctx);
399
400 }
401 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100402 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100403 filter->ctx = NULL;
404 }
405
406 return 1;
407}
408
409 /*
410 * This intends to be used when checking HTTP headers for some
411 * word=value directive. Return a pointer to the first character of value, if
412 * the word was not found or if there wasn't any value assigned ot it return NULL
413 */
414char *directive_value(const char *sample, int slen, const char *word, int wlen)
415{
416 int st = 0;
417
418 if (slen < wlen)
419 return 0;
420
421 while (wlen) {
422 char c = *sample ^ *word;
423 if (c && c != ('A' ^ 'a'))
424 return NULL;
425 sample++;
426 word++;
427 slen--;
428 wlen--;
429 }
430
431 while (slen) {
432 if (st == 0) {
433 if (*sample != '=')
434 return NULL;
435 sample++;
436 slen--;
437 st = 1;
438 continue;
439 } else {
440 return (char *)sample;
441 }
442 }
443
444 return NULL;
445}
446
447/*
448 * Return the maxage in seconds of an HTTP response.
449 * Compute the maxage using either:
450 * - the assigned max-age of the cache
451 * - the s-maxage directive
452 * - the max-age directive
453 * - (Expires - Data) headers
454 * - the default-max-age of the cache
455 *
456 */
William Lallemand49b44532017-11-24 18:53:43 +0100457int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100458{
459 struct http_txn *txn = s->txn;
460 struct hdr_ctx ctx;
461
462 int smaxage = -1;
463 int maxage = -1;
464
465
William Lallemand4da3f8a2017-10-31 14:33:34 +0100466 ctx.idx = 0;
467
468 /* loop on the Cache-Control values */
Willy Tarreau178b9872018-06-19 07:13:36 +0200469 while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100470 char *directive = ctx.line + ctx.val;
471 char *value;
472
473 value = directive_value(directive, ctx.vlen, "s-maxage", 8);
474 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200475 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100476
477 chunk_strncat(chk, value, ctx.vlen - 8 + 1);
478 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200479 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100480 }
481
482 value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
483 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200484 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100485
486 chunk_strncat(chk, value, ctx.vlen - 7 + 1);
487 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200488 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100489 }
490 }
491
492 /* TODO: Expires - Data */
493
494
495 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100496 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100497
498 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100499 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100500
William Lallemand49b44532017-11-24 18:53:43 +0100501 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100502
503}
504
505
William Lallemanda400a3a2017-11-20 19:13:12 +0100506static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
507{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200508 struct cache_entry *object = (struct cache_entry *)block->data;
509
510 if (first == block && object->eb.key)
511 eb32_delete(&object->eb);
512 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100513}
514
William Lallemand41db4602017-10-30 11:15:51 +0100515/*
516 * This fonction will store the headers of the response in a buffer and then
517 * register a filter to store the data
518 */
519enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
520 struct session *sess, struct stream *s, int flags)
521{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200522 unsigned int age;
523 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100524 struct http_txn *txn = s->txn;
525 struct http_msg *msg = &txn->rsp;
526 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100527 struct shared_block *first = NULL;
William Lallemand49b44532017-11-24 18:53:43 +0100528 struct cache *cache = (struct cache *)rule->arg.act.p[0];
529 struct shared_context *shctx = shctx_ptr(cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100530 struct cache_entry *object;
531
William Lallemand4da3f8a2017-10-31 14:33:34 +0100532 /* Don't cache if the response came from a cache */
533 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
534 s->target == &http_cache_applet.obj_type) {
535 goto out;
536 }
537
538 /* cache only HTTP/1.1 */
539 if (!(txn->req.flags & HTTP_MSGF_VER_11))
540 goto out;
541
542 /* cache only GET method */
543 if (txn->meth != HTTP_METH_GET)
544 goto out;
545
546 /* cache only 200 status code */
547 if (txn->status != 200)
548 goto out;
549
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100550 if (IS_HTX_STRM(s)) {
551 struct htx *htx = htxbuf(&s->res.buf);
552 struct http_hdr_ctx ctx;
553 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100554
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100555 /* Do not cache too big objects. */
556 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
557 htx->data + htx->extra > shctx->max_obj_size)
558 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100559
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100560 /* Does not manage Vary at the moment. We will need a secondary key later for that */
561 ctx.blk = NULL;
562 if (http_find_header(htx, ist("Vary"), &ctx, 0))
563 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100564
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100565 htx_check_response_for_cacheability(s, &s->res);
566
567 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
568 goto out;
569
570 age = 0;
571 ctx.blk = NULL;
572 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
573 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
574 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
575 hdr_age = CACHE_ENTRY_MAX_AGE;
576 age = hdr_age;
577 }
578 http_remove_header(htx, &ctx);
579 }
580
581 chunk_reset(&trash);
582 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
583 struct htx_blk *blk = htx_get_blk(htx, pos);
584 uint32_t sz = htx_get_blksz(blk);
585
586 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
587 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
588 break;
589 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
590 }
591 }
592 else {
593 struct hdr_ctx ctx;
594
595 /* Do not cache too big objects. */
596 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
597 msg->sov + msg->body_len > shctx->max_obj_size)
598 goto out;
599
600 /* Does not manage Vary at the moment. We will need a secondary key later for that */
601 ctx.idx = 0;
602 if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx))
603 goto out;
604
605 check_response_for_cacheability(s, &s->res);
606
607 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
608 goto out;
609
610 age = 0;
611 ctx.idx = 0;
612 if (http_find_header2("Age", 3, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
613 if (!strl2llrc(ctx.line + ctx.val, ctx.vlen, &hdr_age) && hdr_age > 0) {
614 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
615 hdr_age = CACHE_ENTRY_MAX_AGE;
616 age = hdr_age;
617 }
618 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200619 }
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200620 }
621
William Lallemand4da3f8a2017-10-31 14:33:34 +0100622 shctx_lock(shctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100623 if (IS_HTX_STRM(s))
624 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
625 else
626 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + msg->sov);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100627 if (!first) {
628 shctx_unlock(shctx);
629 goto out;
630 }
631 shctx_unlock(shctx);
632
Willy Tarreau1093a452018-04-06 19:02:25 +0200633 /* the received memory is not initialized, we need at least to mark
634 * the object as not indexed yet.
635 */
636 object = (struct cache_entry *)first->data;
637 object->eb.node.leaf_p = NULL;
638 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200639 object->age = age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100640 if (IS_HTX_STRM(s)) {
641 object->hdrs_len = trash.data;
642 object->data_len = 0;
643 }
644 else
645 object->eoh = msg->eoh;
Willy Tarreau1093a452018-04-06 19:02:25 +0200646
William Lallemand4da3f8a2017-10-31 14:33:34 +0100647 /* reserve space for the cache_entry structure */
648 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200649 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100650 /* cache the headers in a http action because it allows to chose what
651 * to cache, for example you might want to cache a response before
652 * modifying some HTTP headers, or on the contrary after modifying
653 * those headers.
654 */
655
656 /* does not need to be locked because it's in the "hot" list,
657 * copy the headers */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100658 if (IS_HTX_STRM(s)) {
659 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
660 goto out;
661 }
662 else {
663 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)ci_head(&s->res), msg->sov) < 0)
664 goto out;
665 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100666
667 /* register the buffer in the filter ctx for filling it with data*/
668 if (!LIST_ISEMPTY(&s->strm_flt.filters)) {
669 list_for_each_entry(filter, &s->strm_flt.filters, list) {
670 if (filter->config->id == cache_store_flt_id &&
671 filter->config->conf == rule->arg.act.p[0]) {
672 if (filter->ctx) {
673 struct cache_st *cache_ctx = filter->ctx;
Willy Tarreauc9bd34c2017-12-22 17:42:46 +0100674 struct cache_entry *old;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100675
676 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100677
William Lallemandf528fff2017-11-23 19:43:17 +0100678 object->eb.key = (*(unsigned int *)&txn->cache_hash);
679 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100680 /* Insert the node later on caching success */
681
682 shctx_lock(shctx);
Willy Tarreauc9bd34c2017-12-22 17:42:46 +0100683
684 old = entry_exist(cache, txn->cache_hash);
685 if (old) {
686 eb32_delete(&old->eb);
687 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100688 }
689 shctx_unlock(shctx);
690
691 /* store latest value and expiration time */
692 object->latest_validation = now.tv_sec;
William Lallemand49b44532017-11-24 18:53:43 +0100693 object->expire = now.tv_sec + http_calc_maxage(s, cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100694 }
695 return ACT_RET_CONT;
696 }
697 }
698 }
699
700out:
701 /* if does not cache */
702 if (first) {
703 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100704 first->len = 0;
705 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100706 shctx_row_dec_hot(shctx, first);
707 shctx_unlock(shctx);
708 }
709
William Lallemand41db4602017-10-30 11:15:51 +0100710 return ACT_RET_CONT;
711}
712
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200713#define HTTP_CACHE_INIT 0 /* Initial state. */
714#define HTTP_CACHE_HEADER 1 /* Cache entry headers forwarded. */
715#define HTTP_CACHE_FWD 2 /* Cache entry completely forwarded. */
716#define HTTP_CACHE_END 3 /* Cache entry treatment terminated. */
William Lallemand77c11972017-10-31 20:43:01 +0100717
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100718#define HTX_CACHE_INIT 0 /* Initial state. */
719#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
720#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
721#define HTX_CACHE_EOD 3 /* Cache entry data forwarded. DATA->TLR transition */
722#define HTX_CACHE_TLR 4 /* Cache entry trailers forwarding */
723#define HTX_CACHE_EOM 5 /* Cache entry completely forwarded. Finish the HTX message */
724#define HTX_CACHE_END 6 /* Cache entry treatment terminated */
725
William Lallemandecb73b12017-11-24 14:33:55 +0100726static void http_cache_applet_release(struct appctx *appctx)
727{
728 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
729 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
730 struct shared_block *first = block_ptr(cache_ptr);
731
732 shctx_lock(shctx_ptr(cache));
733 shctx_row_dec_hot(shctx_ptr(cache), first);
734 shctx_unlock(shctx_ptr(cache));
735}
736
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100737static size_t htx_cache_dump_headers(struct appctx *appctx, struct htx *htx)
738{
739 struct cache *cache = appctx->rule->arg.act.p[0];
740 struct shared_context *shctx = shctx_ptr(cache);
741 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
742 struct shared_block *shblk = appctx->ctx.cache.next;
743 struct buffer *tmp = get_trash_chunk();
744 char *end;
745 unsigned int offset, len, age;
746
747 offset = appctx->ctx.cache.offset;
748 len = cache_ptr->hdrs_len;
749
750 /* 1. Retrieve all headers from the cache */
751 list_for_each_entry_from(shblk, &shctx->hot, list) {
752 int sz;
753
754 sz = MIN(len, shctx->block_size - offset);
755 if (!chunk_memcat(tmp, (const char *)shblk->data + offset, sz))
756 return 0;
757
758 offset += sz;
759 len -= sz;
760 if (!len)
761 break;
762 offset = 0;
763 }
764 appctx->ctx.cache.offset = offset;
765 appctx->ctx.cache.next = shblk;
766 appctx->ctx.cache.sent += b_data(tmp);
767
768 /* 2. push these headers in the HTX message */
769 offset = 0;
770 while (offset < b_data(tmp)) {
771 struct htx_blk *blk;
772 enum htx_blk_type type;
773 uint32_t info, sz;
774
775 /* Read the header's info */
776 memcpy((char *)&info, b_peek(tmp, offset), 4);
777 type = (info >> 28);
778 sz = ((type == HTX_BLK_HDR)
779 ? (info & 0xff) + ((info >> 8) & 0xfffff)
780 : info & 0xfffffff);
781
782 /* Create the block with the right type and the right size */
783 blk = htx_add_blk(htx, type, sz);
784 if (!blk)
785 return 0;
786
787 /* Copy info and data */
788 blk->info = info;
789 memcpy(htx_get_blk_ptr(htx, blk), b_peek(tmp, offset+4), sz);
790
791 /* next header */
792 offset += 4 + sz;
793 }
794
795 /* 3. Append "age" header */
796 chunk_reset(tmp);
797 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
798 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
799 age = CACHE_ENTRY_MAX_AGE;
800 end = ultoa_o(age, b_head(tmp), b_size(tmp));
801 b_set_data(tmp, end - b_head(tmp));
802
803 if (!http_add_header(htx, ist("Age"), ist2(b_head(tmp), b_data(tmp))))
804 return 0;
805
806 return htx->data;
807}
808
809static size_t htx_cache_dump_data(struct appctx *appctx, struct htx *htx,
810 enum htx_blk_type type, unsigned int len)
811{
812 struct cache *cache = appctx->rule->arg.act.p[0];
813 struct shared_context *shctx = shctx_ptr(cache);
814 struct shared_block *shblk = appctx->ctx.cache.next;
815 uint32_t max = htx_free_data_space(htx);
816 unsigned int offset;
817 size_t total = 0;
818
819 offset = appctx->ctx.cache.offset;
820 if (len > max)
821 len = max;
822 if (!len)
823 goto end;
824
825 list_for_each_entry_from(shblk, &shctx->hot, list) {
826 struct ist data;
827 int sz;
828
829 sz = MIN(len, shctx->block_size - offset);
830 data = ist2((const char *)shblk->data + offset, sz);
831 if (type == HTX_BLK_DATA) {
832 if (!htx_add_data(htx, data))
833 break;
834 }
835 else { /* HTX_BLK_TLR */
836 if (!htx_add_trailer(htx, data))
837 break;
838 }
839
840 offset += sz;
841 len -= sz;
842 total += sz;
843 if (!len)
844 break;
845 offset = 0;
846 }
847 appctx->ctx.cache.offset = offset;
848 appctx->ctx.cache.next = shblk;
849 appctx->ctx.cache.sent += total;
850
851 end:
852 return total;
853}
854static void htx_cache_io_handler(struct appctx *appctx)
855{
856 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
857 struct shared_block *first = block_ptr(cache_ptr);
858 struct stream_interface *si = appctx->owner;
859 struct channel *req = si_oc(si);
860 struct channel *res = si_ic(si);
861 struct htx *req_htx, *res_htx;
862 struct buffer *errmsg;
863 size_t ret, total = 0;
864
865 res_htx = htxbuf(&res->buf);
866
867 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
868 goto out;
869
870 /* Check if the input buffer is avalaible. */
871 if (!b_size(&res->buf)) {
872 si_rx_room_blk(si);
873 goto out;
874 }
875
876 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
877 appctx->st0 = HTTP_CACHE_END;
878
879 if (appctx->st0 == HTX_CACHE_INIT) {
880 appctx->ctx.cache.next = block_ptr(cache_ptr);
881 appctx->ctx.cache.offset = sizeof(*cache_ptr);
882 appctx->ctx.cache.sent = 0;
883 appctx->st0 = HTX_CACHE_HEADER;
884 }
885
886 if (appctx->st0 == HTX_CACHE_HEADER) {
887 /* Headers must be dump at once. Otherwise it is an error */
888 ret = htx_cache_dump_headers(appctx, res_htx);
889 if (!ret)
890 goto error;
891
892 total += ret;
893 if (cache_ptr->data_len)
894 appctx->st0 = HTX_CACHE_DATA;
895 else if (first->len > sizeof(*cache_ptr) + appctx->ctx.cache.sent) {
896 /* Headers have benn sent (hrds_len) and there is no data
897 * (data_len == 0). So, all the remaining is the
898 * trailers */
899 appctx->st0 = HTX_CACHE_EOD;
900 }
901 else
902 appctx->st0 = HTX_CACHE_EOM;
903 }
904
905 if (appctx->st0 == HTX_CACHE_DATA) {
906 unsigned int len = cache_ptr->hdrs_len + cache_ptr->data_len - appctx->ctx.cache.sent;
907
908 ret = htx_cache_dump_data(appctx, res_htx, HTX_BLK_DATA, len);
909 if (!ret) {
910 si_rx_room_blk(si);
911 goto out;
912 }
913
914 total += ret;
915 if (cache_ptr->hdrs_len + cache_ptr->data_len == appctx->ctx.cache.sent) {
916 if (first->len > sizeof(*cache_ptr) + appctx->ctx.cache.sent) {
917 /* Headers and all data have been sent
918 * (hrds_len + data_len == sent). So, all the remaining
919 * is the trailers */
920 appctx->st0 = HTX_CACHE_EOD;
921 }
922 else
923 appctx->st0 = HTX_CACHE_EOM;
924 }
925 }
926
927 if (appctx->st0 == HTX_CACHE_EOD) {
928 if (!htx_add_endof(res_htx, HTX_BLK_EOD)) {
929 si_rx_room_blk(si);
930 goto out;
931 }
932
933 total++;
934 appctx->st0 = HTX_CACHE_TLR;
935 }
936
937 if (appctx->st0 == HTX_CACHE_TLR) {
938 unsigned int len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
939
940 ret = htx_cache_dump_data(appctx, res_htx, HTX_BLK_TLR, len);
941 if (!ret) {
942 si_rx_room_blk(si);
943 goto out;
944 }
945
946 total += ret;
947 if (first->len == sizeof(*cache_ptr) + appctx->ctx.cache.sent)
948 appctx->st0 = HTX_CACHE_EOM;
949 }
950
951 if (appctx->st0 == HTX_CACHE_EOM) {
952 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
953 si_rx_room_blk(si);
954 goto out;
955 }
956
957 total++;
958 appctx->st0 = HTX_CACHE_END;
959 }
960
961 end:
962 if (appctx->st0 == HTX_CACHE_END) {
963 /* eat the whole request */
964 req_htx = htxbuf(&req->buf);
965 htx_reset(req_htx);
966 htx_to_buf(req_htx, &req->buf);
967 co_set_data(req, 0);
968 res->flags |= CF_READ_NULL;
969 si_shutr(si);
970 }
971
972 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
973 si_shutw(si);
974
975 if (appctx->st0 == HTX_CACHE_END) {
976 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
977 si_shutr(si);
978 res->flags |= CF_READ_NULL;
979 }
980 }
981 out:
982 if (total) {
983 res->total += total;
984 res->flags |= CF_READ_PARTIAL;
985 }
986
987 /* we have left the request in the buffer for the case where we
988 * process a POST, and this automatically re-enables activity on
989 * read. It's better to indicate that we want to stop reading when
990 * we're sending, so that we know there's at most one direction
991 * deciding to wake the applet up. It saves it from looping when
992 * emitting large blocks into small TCP windows.
993 */
994 htx_to_buf(res_htx, &res->buf);
995 if (!channel_is_empty(res))
996 si_stop_get(si);
997 return;
998
999 error:
1000 /* Sent and HTTP error 500 */
1001 b_reset(&res->buf);
1002 errmsg = &htx_err_chunks[HTTP_ERR_500];
1003 res->buf.data = b_data(errmsg);
1004 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1005 res_htx = htx_from_buf(&res->buf);
1006
1007 total = res_htx->data;
1008 appctx->st0 = HTX_CACHE_END;
1009 goto end;
1010}
1011
1012
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001013/*
1014 * Append an "Age" header into <chn> channel for this <ce> cache entry.
Joseph Herlant8dae5b32018-11-15 14:07:53 -08001015 * This is the responsibility of the caller to insure there is enough
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001016 * data in the channel.
1017 *
1018 * Returns the number of bytes inserted if succeeded, 0 if failed.
1019 */
1020static int cache_channel_append_age_header(struct cache_entry *ce, struct channel *chn)
1021{
1022 unsigned int age;
1023
1024 age = MAX(0, (int)(now.tv_sec - ce->latest_validation)) + ce->age;
1025 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1026 age = CACHE_ENTRY_MAX_AGE;
1027
1028 chunk_reset(&trash);
1029 chunk_printf(&trash, "Age: %u", age);
1030
1031 return ci_insert_line2(chn, ce->eoh, trash.area, trash.data);
1032}
1033
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001034static int cache_channel_row_data_get(struct appctx *appctx, int len)
William Lallemand77c11972017-10-31 20:43:01 +01001035{
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001036 int ret, total;
William Lallemand77c11972017-10-31 20:43:01 +01001037 struct stream_interface *si = appctx->owner;
1038 struct channel *res = si_ic(si);
1039 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
William Lallemand77c11972017-10-31 20:43:01 +01001040 struct shared_context *shctx = shctx_ptr(cache);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001041 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1042 struct shared_block *blk, *next = appctx->ctx.cache.next;
1043 int offset;
1044
1045 total = 0;
1046 offset = 0;
1047
1048 if (!next) {
1049 offset = sizeof(struct cache_entry);
1050 next = block_ptr(cache_ptr);
1051 }
1052
1053 blk = next;
1054 list_for_each_entry_from(blk, &shctx->hot, list) {
1055 int sz;
1056
1057 if (len <= 0)
1058 break;
1059
1060 sz = MIN(len, shctx->block_size - offset);
1061
1062 ret = ci_putblk(res, (const char *)blk->data + offset, sz);
1063 if (unlikely(offset))
1064 offset = 0;
1065 if (ret <= 0) {
1066 if (ret == -3 || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001067 si_rx_room_blk(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001068 break;
1069 }
1070 return -1;
1071 }
1072
1073 total += sz;
1074 len -= sz;
1075 }
1076 appctx->ctx.cache.next = blk;
1077
1078 return total;
1079}
1080
1081static void http_cache_io_handler(struct appctx *appctx)
1082{
1083 struct stream_interface *si = appctx->owner;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001084 struct stream *s = si_strm(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001085 struct channel *res = si_ic(si);
1086 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
William Lallemand77c11972017-10-31 20:43:01 +01001087 struct shared_block *first = block_ptr(cache_ptr);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001088 unsigned int *sent = &appctx->ctx.cache.sent;
1089
1090 if (IS_HTX_STRM(s))
1091 return htx_cache_io_handler(appctx);
William Lallemand77c11972017-10-31 20:43:01 +01001092
1093 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1094 goto out;
1095
Joseph Herlant8dae5b32018-11-15 14:07:53 -08001096 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001097 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01001098 /* buf.size==0 means we failed to get a buffer and were
1099 * already subscribed to a wait list to get a buffer.
1100 */
William Lallemand77c11972017-10-31 20:43:01 +01001101 goto out;
1102 }
1103
1104 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
1105 appctx->st0 = HTTP_CACHE_END;
1106
1107 /* buffer are aligned there, should be fine */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001108 if (appctx->st0 == HTTP_CACHE_HEADER || appctx->st0 == HTTP_CACHE_INIT) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001109 int len = first->len - *sent - sizeof(struct cache_entry);
William Lallemand55e76742017-11-21 20:01:28 +01001110
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001111 if (len > 0) {
1112 int ret;
1113
1114 ret = cache_channel_row_data_get(appctx, len);
1115 if (ret == -1)
1116 appctx->st0 = HTTP_CACHE_END;
1117 else
1118 *sent += ret;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001119 if (appctx->st0 == HTTP_CACHE_INIT && *sent > cache_ptr->eoh &&
1120 cache_channel_append_age_header(cache_ptr, res))
1121 appctx->st0 = HTTP_CACHE_HEADER;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001122 }
1123 else {
1124 *sent = 0;
1125 appctx->st0 = HTTP_CACHE_FWD;
William Lallemand77c11972017-10-31 20:43:01 +01001126 }
William Lallemand77c11972017-10-31 20:43:01 +01001127 }
1128
1129 if (appctx->st0 == HTTP_CACHE_FWD) {
1130 /* eat the whole request */
Willy Tarreau178b9872018-06-19 07:13:36 +02001131 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 +01001132 res->flags |= CF_READ_NULL;
1133 si_shutr(si);
1134 }
1135
1136 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
1137 si_shutw(si);
1138out:
1139 ;
1140}
1141
William Lallemand41db4602017-10-30 11:15:51 +01001142enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1143 struct act_rule *rule, char **err)
1144{
1145 struct flt_conf *fconf;
1146 int cur_arg = *orig_arg;
1147 rule->action = ACT_CUSTOM;
1148 rule->action_ptr = http_action_store_cache;
1149
1150 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
1151 memprintf(err, "expects a cache name");
1152 return ACT_RET_PRS_ERR;
1153 }
1154
1155 /* check if a cache filter was already registered with this cache
1156 * name, if that's the case, must use it. */
1157 list_for_each_entry(fconf, &proxy->filter_configs, list) {
1158 if (fconf->id == cache_store_flt_id && !strcmp((char *)fconf->conf, args[cur_arg])) {
1159 rule->arg.act.p[0] = fconf->conf;
1160 (*orig_arg)++;
1161 /* filter already registered */
1162 return ACT_RET_PRS_OK;
1163 }
1164 }
1165
1166 rule->arg.act.p[0] = strdup(args[cur_arg]);
1167 if (!rule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001168 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +01001169 err++;
1170 goto err;
1171 }
1172 /* register a filter to fill the cache buffer */
1173 fconf = calloc(1, sizeof(*fconf));
1174 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001175 ha_alert("config: %s '%s': out of memory\n",
1176 proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +01001177 err++;
1178 goto err;
1179 }
1180 fconf->id = cache_store_flt_id;
1181 fconf->conf = rule->arg.act.p[0]; /* store the proxy name */
1182 fconf->ops = &cache_ops;
1183 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1184
1185 (*orig_arg)++;
1186
1187 return ACT_RET_PRS_OK;
1188
1189err:
Olivier Houchardfccf8402017-11-01 14:04:02 +01001190 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001191}
1192
William Lallemandf528fff2017-11-23 19:43:17 +01001193/* This produces a sha1 hash of the concatenation of the first
1194 * occurrence of the Host header followed by the path component if it
1195 * begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001196int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001197{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001198 struct http_txn *txn = s->txn;
William Lallemandf528fff2017-11-23 19:43:17 +01001199 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001200 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001201
William Lallemandf528fff2017-11-23 19:43:17 +01001202 trash = get_trash_chunk();
1203
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001204 if (IS_HTX_STRM(s)) {
1205 struct htx *htx = htxbuf(&s->req.buf);
1206 struct htx_sl *sl;
1207 struct http_hdr_ctx ctx;
1208 struct ist path;
William Lallemandf528fff2017-11-23 19:43:17 +01001209
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001210 ctx.blk = NULL;
1211 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1212 return 0;
1213 chunk_memcat(trash, ctx.value.ptr, ctx.value.len);
1214
1215 sl = http_find_stline(htx);
1216 path = http_get_path(htx_sl_req_uri(sl));
1217 if (!path.ptr)
1218 return 0;
1219 chunk_memcat(trash, path.ptr, path.len);
1220 }
1221 else {
1222 struct hdr_ctx ctx;
1223 char *path;
1224 char *end;
1225
1226 /* retrive the host */
1227 ctx.idx = 0;
1228 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx))
1229 return 0;
1230 chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen);
1231
1232 /* now retrieve the path */
1233 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1234 path = http_txn_get_path(txn);
1235 if (!path)
1236 return 0;
1237 chunk_strncat(trash, path, end - path);
1238 }
William Lallemandf528fff2017-11-23 19:43:17 +01001239
1240 /* hash everything */
1241 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001242 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001243 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1244
1245 return 1;
1246}
1247
William Lallemand41db4602017-10-30 11:15:51 +01001248enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1249 struct session *sess, struct stream *s, int flags)
1250{
William Lallemand77c11972017-10-31 20:43:01 +01001251
William Lallemand77c11972017-10-31 20:43:01 +01001252 struct cache_entry *res;
William Lallemand77c11972017-10-31 20:43:01 +01001253 struct cache *cache = (struct cache *)rule->arg.act.p[0];
1254
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001255 if (IS_HTX_STRM(s))
1256 htx_check_request_for_cacheability(s, &s->req);
1257 else
1258 check_request_for_cacheability(s, &s->req);
1259
Willy Tarreau504455c2017-12-22 17:47:35 +01001260 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1261 return ACT_RET_CONT;
1262
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001263 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001264 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001265
Willy Tarreau504455c2017-12-22 17:47:35 +01001266 if (s->txn->flags & TX_CACHE_IGNORE)
1267 return ACT_RET_CONT;
1268
William Lallemanda400a3a2017-11-20 19:13:12 +01001269 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001270 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001271 if (res) {
1272 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001273 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1274 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001275 s->target = &http_cache_applet.obj_type;
1276 if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
1277 appctx->st0 = HTTP_CACHE_INIT;
1278 appctx->rule = rule;
1279 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001280 appctx->ctx.cache.next = NULL;
1281 appctx->ctx.cache.sent = 0;
Olivier Houchardfccf8402017-11-01 14:04:02 +01001282 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001283 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001284 shctx_lock(shctx_ptr(cache));
1285 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1286 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001287 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001288 }
1289 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001290 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001291 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001292}
1293
1294
1295enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1296 struct act_rule *rule, char **err)
1297{
1298 int cur_arg = *orig_arg;
1299
1300 rule->action = ACT_CUSTOM;
1301 rule->action_ptr = http_action_req_cache_use;
1302
1303 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
1304 memprintf(err, "expects a cache name");
1305 return ACT_RET_PRS_ERR;
1306 }
1307
1308 rule->arg.act.p[0] = strdup(args[cur_arg]);
1309 if (!rule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001310 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +01001311 err++;
1312 goto err;
1313 }
1314
1315 (*orig_arg)++;
1316 return ACT_RET_PRS_OK;
1317
1318err:
Olivier Houchardfccf8402017-11-01 14:04:02 +01001319 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001320
1321}
1322
1323int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1324{
1325 int err_code = 0;
1326
1327 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1328
1329 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001330 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1331 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001332 err_code |= ERR_ALERT | ERR_ABORT;
1333 goto out;
1334 }
1335
1336 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1337 err_code |= ERR_ABORT;
1338 goto out;
1339 }
1340
1341 if (tmp_cache_config == NULL) {
1342 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1343 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001344 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001345 err_code |= ERR_ALERT | ERR_ABORT;
1346 goto out;
1347 }
1348
1349 strlcpy2(tmp_cache_config->id, args[1], 33);
1350 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001351 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1352 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001353 err_code |= ERR_WARN;
1354 }
William Lallemand49b44532017-11-24 18:53:43 +01001355 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001356 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001357 tmp_cache_config->maxobjsz = 0;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001358 tmp_cache_config->flags = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001359 }
1360 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001361 unsigned long int maxsize;
1362 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001363
1364 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1365 err_code |= ERR_ABORT;
1366 goto out;
1367 }
1368
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001369 maxsize = strtoul(args[1], &err, 10);
1370 if (err == args[1] || *err != '\0') {
1371 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1372 file, linenum, args[1]);
1373 err_code |= ERR_ABORT;
1374 goto out;
1375 }
1376
1377 if (maxsize > (UINT_MAX >> 20)) {
1378 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1379 file, linenum, args[1], UINT_MAX >> 20);
1380 err_code |= ERR_ABORT;
1381 goto out;
1382 }
1383
William Lallemand41db4602017-10-30 11:15:51 +01001384 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001385 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001386 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001387 } else if (strcmp(args[0], "max-age") == 0) {
1388 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1389 err_code |= ERR_ABORT;
1390 goto out;
1391 }
1392
1393 if (!*args[1]) {
1394 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1395 file, linenum, args[0]);
1396 err_code |= ERR_WARN;
1397 }
1398
1399 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001400 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001401 unsigned int maxobjsz;
1402 char *err;
1403
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001404 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1405 err_code |= ERR_ABORT;
1406 goto out;
1407 }
1408
1409 if (!*args[1]) {
1410 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1411 file, linenum, args[0]);
1412 err_code |= ERR_WARN;
1413 }
1414
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001415 maxobjsz = strtoul(args[1], &err, 10);
1416 if (err == args[1] || *err != '\0') {
1417 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1418 file, linenum, args[1]);
1419 err_code |= ERR_ABORT;
1420 goto out;
1421 }
1422 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001423 }
1424 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001425 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001426 err_code |= ERR_ALERT | ERR_FATAL;
1427 goto out;
1428 }
1429out:
1430 return err_code;
1431}
1432
1433/* once the cache section is parsed */
1434
1435int cfg_post_parse_section_cache()
1436{
1437 struct shared_context *shctx;
1438 int err_code = 0;
1439 int ret_shctx;
1440
1441 if (tmp_cache_config) {
1442 struct cache *cache;
1443
1444 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001445 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001446 err_code |= ERR_FATAL | ERR_ALERT;
1447 goto out;
1448 }
1449
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001450 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001451 /* Default max. file size is a 256th of the cache size. */
1452 tmp_cache_config->maxobjsz =
1453 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001454 }
1455 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1456 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1457 err_code |= ERR_FATAL | ERR_ALERT;
1458 goto out;
1459 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001460
1461 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE,
1462 tmp_cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001463
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001464 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001465 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001466 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001467 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001468 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001469
1470 err_code |= ERR_FATAL | ERR_ALERT;
1471 goto out;
1472 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001473 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +01001474 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
1475 cache = (struct cache *)shctx->data;
1476 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001477 LIST_ADDQ(&caches, &cache->list);
1478 }
1479out:
1480 free(tmp_cache_config);
1481 tmp_cache_config = NULL;
1482 return err_code;
1483
1484}
1485
1486/*
1487 * Resolve the cache name to a pointer once the file is completely read.
1488 */
1489int cfg_cache_postparser()
1490{
1491 struct act_rule *hresrule, *hrqrule;
1492 void *cache_ptr;
1493 struct cache *cache;
1494 struct proxy *curproxy = NULL;
1495 int err = 0;
1496 struct flt_conf *fconf;
1497
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001498 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
William Lallemand41db4602017-10-30 11:15:51 +01001499
1500 /* resolve the http response cache name to a ptr in the action rule */
1501 list_for_each_entry(hresrule, &curproxy->http_res_rules, list) {
1502 if (hresrule->action != ACT_CUSTOM ||
1503 hresrule->action_ptr != http_action_store_cache)
1504 continue;
1505
1506 cache_ptr = hresrule->arg.act.p[0];
1507
1508 list_for_each_entry(cache, &caches, list) {
1509 if (!strcmp(cache->id, cache_ptr)) {
1510 /* don't free there, it's still used in the filter conf */
1511 cache_ptr = cache;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001512 cache->flags |= ((curproxy->options2 & PR_O2_USE_HTX)
1513 ? CACHE_F_HTX
1514 : CACHE_F_LEGACY_HTTP);
William Lallemand41db4602017-10-30 11:15:51 +01001515 break;
1516 }
1517 }
1518
1519 if (cache_ptr == hresrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001520 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n",
1521 curproxy->id, (char *)hresrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001522 err++;
1523 }
1524
1525 hresrule->arg.act.p[0] = cache_ptr;
1526 }
1527
1528 /* resolve the http request cache name to a ptr in the action rule */
1529 list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) {
1530 if (hrqrule->action != ACT_CUSTOM ||
1531 hrqrule->action_ptr != http_action_req_cache_use)
1532 continue;
1533
1534 cache_ptr = hrqrule->arg.act.p[0];
1535
1536 list_for_each_entry(cache, &caches, list) {
1537 if (!strcmp(cache->id, cache_ptr)) {
1538 free(cache_ptr);
1539 cache_ptr = cache;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001540 cache->flags |= ((curproxy->options2 & PR_O2_USE_HTX)
1541 ? CACHE_F_HTX
1542 : CACHE_F_LEGACY_HTTP);
William Lallemand41db4602017-10-30 11:15:51 +01001543 break;
1544 }
1545 }
1546
1547 if (cache_ptr == hrqrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001548 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n",
1549 curproxy->id, (char *)hrqrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001550 err++;
1551 }
1552
1553 hrqrule->arg.act.p[0] = cache_ptr;
1554 }
1555
1556 /* resolve the cache name to a ptr in the filter config */
1557 list_for_each_entry(fconf, &curproxy->filter_configs, list) {
1558
William Lallemand9c54c532017-11-02 16:38:42 +01001559 if (fconf->id != cache_store_flt_id)
1560 continue;
1561
William Lallemand41db4602017-10-30 11:15:51 +01001562 cache_ptr = fconf->conf;
1563
1564 list_for_each_entry(cache, &caches, list) {
1565 if (!strcmp(cache->id, cache_ptr)) {
1566 /* there can be only one filter per cache, so we free it there */
1567 free(cache_ptr);
1568 cache_ptr = cache;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001569 cache->flags |= ((curproxy->options2 & PR_O2_USE_HTX)
1570 ? CACHE_F_HTX
1571 : CACHE_F_LEGACY_HTTP);
William Lallemand41db4602017-10-30 11:15:51 +01001572 break;
1573 }
1574 }
1575
1576 if (cache_ptr == fconf->conf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001577 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
1578 curproxy->id, (char *)fconf->conf);
William Lallemand41db4602017-10-30 11:15:51 +01001579 err++;
1580 }
1581 fconf->conf = cache_ptr;
1582 }
1583 }
Christopher Faulet1f672c52018-12-03 14:30:41 +01001584
1585 /* Check if the cache is used by HTX and legacy HTTP proxies in same
1586 * time
1587 */
1588 list_for_each_entry(cache, &caches, list) {
1589 if ((cache->flags & (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) == (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) {
1590 ha_alert("Cache '%s': cannot be used by HTX and legacy HTTP proxies in same time.\n",
1591 cache->id);
1592 err++;
1593 }
1594 }
1595
William Lallemand41db4602017-10-30 11:15:51 +01001596 return err;
1597}
1598
1599
1600struct flt_ops cache_ops = {
1601 .init = cache_store_init,
1602
William Lallemand4da3f8a2017-10-31 14:33:34 +01001603 /* Handle channels activity */
1604 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001605 .channel_end_analyze = cache_store_chn_end_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001606
1607 /* Filter HTTP requests and responses */
1608 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001609 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001610 .http_end = cache_store_http_end,
1611
1612 .http_forward_data = cache_store_http_forward_data,
1613
William Lallemand41db4602017-10-30 11:15:51 +01001614};
1615
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001616static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001617{
1618 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1619 return 1;
1620
1621 return 0;
1622}
1623
1624static int cli_io_handler_show_cache(struct appctx *appctx)
1625{
1626 struct cache* cache = appctx->ctx.cli.p0;
1627 struct stream_interface *si = appctx->owner;
1628
William Lallemand1f49a362017-11-21 20:01:26 +01001629 if (cache == NULL) {
1630 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1631 }
1632
1633 list_for_each_entry_from(cache, &caches, list) {
1634 struct eb32_node *node = NULL;
1635 unsigned int next_key;
1636 struct cache_entry *entry;
1637
William Lallemand1f49a362017-11-21 20:01:26 +01001638 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001639 if (!next_key) {
1640 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1641 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001642 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001643 return 0;
1644 }
1645 }
William Lallemand1f49a362017-11-21 20:01:26 +01001646
1647 appctx->ctx.cli.p0 = cache;
1648
1649 while (1) {
1650
1651 shctx_lock(shctx_ptr(cache));
1652 node = eb32_lookup_ge(&cache->entries, next_key);
1653 if (!node) {
1654 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001655 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001656 break;
1657 }
1658
1659 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001660 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 +01001661
1662 next_key = node->key + 1;
1663 appctx->ctx.cli.i0 = next_key;
1664
1665 shctx_unlock(shctx_ptr(cache));
1666
1667 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001668 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001669 return 0;
1670 }
1671 }
1672
1673 }
1674
1675 return 1;
1676
1677}
1678
1679static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001680 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1681 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001682}};
1683
Willy Tarreau0108d902018-11-25 19:14:37 +01001684INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001685
William Lallemand41db4602017-10-30 11:15:51 +01001686static struct action_kw_list http_res_actions = {
1687 .kw = {
1688 { "cache-store", parse_cache_store },
1689 { NULL, NULL }
1690 }
1691};
1692
Willy Tarreau0108d902018-11-25 19:14:37 +01001693INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1694
William Lallemand41db4602017-10-30 11:15:51 +01001695static struct action_kw_list http_req_actions = {
1696 .kw = {
1697 { "cache-use", parse_cache_use },
1698 { NULL, NULL }
1699 }
1700};
1701
Willy Tarreau0108d902018-11-25 19:14:37 +01001702INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1703
William Lallemand41db4602017-10-30 11:15:51 +01001704struct applet http_cache_applet = {
1705 .obj_type = OBJ_TYPE_APPLET,
1706 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001707 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001708 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001709};
1710
Willy Tarreaue6552512018-11-26 11:33:13 +01001711/* config parsers for this section */
1712REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
1713REGISTER_CONFIG_POSTPARSER("cache", cfg_cache_postparser);