blob: bb14867804ca405dba882afb65a2aee3dbe65ed5 [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>
William Lallemand41db4602017-10-30 11:15:51 +010027#include <proto/filters.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020028#include <proto/http_rules.h>
William Lallemand41db4602017-10-30 11:15:51 +010029#include <proto/proto_http.h>
30#include <proto/log.h>
31#include <proto/stream.h>
32#include <proto/stream_interface.h>
33#include <proto/shctx.h>
34
William Lallemand41db4602017-10-30 11:15:51 +010035
36#include <common/cfgparse.h>
37#include <common/hash.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010038#include <common/htx.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
Christopher Faulet27d93c32018-12-15 22:32:02 +010046#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010047 * the filter keyword) */
Christopher Fauletafd819c2018-12-11 08:57:45 +010048
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010049const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010050
Willy Tarreau2231b632019-03-29 18:26:52 +010051extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010052
53struct flt_ops cache_ops;
54
55struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010056 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010057 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010058 unsigned int maxage; /* max-age */
59 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020060 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010061 char id[33]; /* cache name */
Christopher Faulet1f672c52018-12-03 14:30:41 +010062 unsigned int flags; /* CACHE_F_* */
William Lallemand41db4602017-10-30 11:15:51 +010063};
64
Christopher Faulet95220e22018-12-07 17:34:39 +010065/* cache config for filters */
66struct cache_flt_conf {
67 union {
68 struct cache *cache; /* cache used by the filter */
69 char *name; /* cache name used during conf parsing */
70 } c;
71 unsigned int flags; /* CACHE_FLT_F_* */
72};
73
William Lallemand41db4602017-10-30 11:15:51 +010074/*
75 * cache ctx for filters
76 */
77struct cache_st {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010078 int hdrs_len; // field used in legacy mode only
William Lallemand41db4602017-10-30 11:15:51 +010079 struct shared_block *first_block;
80};
81
82struct cache_entry {
83 unsigned int latest_validation; /* latest validation date */
84 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020085 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010086 unsigned int eoh; /* Origin server end of headers offset. */ // field used in legacy mode only
87
William Lallemand41db4602017-10-30 11:15:51 +010088 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010089 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010090 unsigned char data[0];
91};
92
93#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010094#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010095
96static struct list caches = LIST_HEAD_INIT(caches);
William Lallemande4876e22019-08-28 15:22:49 +020097static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +010098static struct cache *tmp_cache_config = NULL;
99
Willy Tarreau8ceae722018-11-26 11:58:30 +0100100DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
101
William Lallemandf528fff2017-11-23 19:43:17 +0100102struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100103{
104 struct eb32_node *node;
105 struct cache_entry *entry;
106
William Lallemandf528fff2017-11-23 19:43:17 +0100107 node = eb32_lookup(&cache->entries, (*(unsigned int *)hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100108 if (!node)
109 return NULL;
110
111 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100112
113 /* if that's not the right node */
114 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
115 return NULL;
116
William Lallemand08727662017-11-21 20:01:27 +0100117 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100118 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100119 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100120 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100121 entry->eb.key = 0;
122 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100123 return NULL;
124
125}
126
127static inline struct shared_context *shctx_ptr(struct cache *cache)
128{
129 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
130}
131
William Lallemand77c11972017-10-31 20:43:01 +0100132static inline struct shared_block *block_ptr(struct cache_entry *entry)
133{
134 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
135}
136
137
138
William Lallemand41db4602017-10-30 11:15:51 +0100139static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100140cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100141{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100142 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100143 return 0;
144}
145
Christopher Faulet95220e22018-12-07 17:34:39 +0100146static void
147cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
148{
149 struct cache_flt_conf *cconf = fconf->conf;
150
151 free(cconf);
152}
153
William Lallemand4da3f8a2017-10-31 14:33:34 +0100154static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100155cache_store_check(struct proxy *px, struct flt_conf *fconf)
156{
157 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100158 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100159 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100160 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100161
William Lallemande4876e22019-08-28 15:22:49 +0200162 /* Find the cache corresponding to the name in the filter config. The
163 * cache will not be referenced now in the filter config because it is
164 * not fully allocated. This step will be performed during the cache
165 * post_check.
166 */
167 list_for_each_entry(cache, &caches_config, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +0100168 if (!strcmp(cache->id, cconf->c.name)) {
169 /* there can be only one filter per cache, so we free it there */
170 cache->flags |= ((px->options2 & PR_O2_USE_HTX)
171 ? CACHE_F_HTX
172 : CACHE_F_LEGACY_HTTP);
Christopher Faulet95220e22018-12-07 17:34:39 +0100173 goto found;
174 }
175 }
176
177 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
178 proxy_type_str(px), px->id, (char *)cconf->c.name);
179 return 1;
180
181 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100182 /* Here <cache> points on the cache the filter must use and <cconf>
183 * points on the cache filter configuration. */
184
185 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100186 * enabled and if it is after the cache. When the compression is before
187 * the cache, an error is returned. Also check if the cache filter must
188 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100189 list_for_each_entry(f, &px->filter_configs, list) {
190 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100191 /* The compression filter must be evaluated after the cache. */
192 if (comp) {
193 ha_alert("config: %s '%s': unable to enable the compression filter before "
194 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
195 return 1;
196 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100197 }
Christopher Faulet27d93c32018-12-15 22:32:02 +0100198 else if (f->id == http_comp_flt_id) {
Christopher Fauletafd819c2018-12-11 08:57:45 +0100199 if (!(px->options2 & PR_O2_USE_HTX)) {
200 ha_alert("config: %s '%s' : compression and cache filters cannot be "
201 "both enabled on non HTX proxy.\n",
202 proxy_type_str(px), px->id);
203 return 1;
204 }
Christopher Faulet27d93c32018-12-15 22:32:02 +0100205 comp = 1;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100206 }
Christopher Faulet27d93c32018-12-15 22:32:02 +0100207 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
208 /* Implicit declaration is only allowed with the
209 * compression. For other filters, an implicit
210 * declaration is required. */
211 ha_alert("config: %s '%s': require an explicit filter declaration "
212 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
213 return 1;
214 }
215
Christopher Fauletafd819c2018-12-11 08:57:45 +0100216 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100217 return 0;
218}
219
220static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100221cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
222{
223 if (!(chn->flags & CF_ISRESP))
224 return 1;
225
226 if (filter->ctx == NULL) {
227 struct cache_st *st;
228
Willy Tarreaubafbe012017-11-24 17:34:44 +0100229 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100230 if (st == NULL)
231 return -1;
232
233 st->hdrs_len = 0;
234 st->first_block = NULL;
235 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100236
237 /* Register post-analyzer on AN_RES_WAIT_HTTP */
238 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100239 }
240
William Lallemand4da3f8a2017-10-31 14:33:34 +0100241 return 1;
242}
243
244static int
William Lallemand49dc0482017-11-24 14:33:54 +0100245cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
246{
247 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100248 struct cache_flt_conf *cconf = FLT_CONF(filter);
249 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100250 struct shared_context *shctx = shctx_ptr(cache);
251
252 if (!(chn->flags & CF_ISRESP))
253 return 1;
254
255 /* Everything should be released in the http_end filter, but we need to do it
256 * there too, in case of errors */
257
258 if (st && st->first_block) {
259
260 shctx_lock(shctx);
261 shctx_row_dec_hot(shctx, st->first_block);
262 shctx_unlock(shctx);
263
264 }
265 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100266 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100267 filter->ctx = NULL;
268 }
269
270 return 1;
271}
272
Christopher Faulet839791a2019-01-07 16:12:07 +0100273static int
274cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
275 unsigned an_bit)
276{
277 struct http_txn *txn = s->txn;
278 struct http_msg *msg = &txn->rsp;
279 struct cache_st *st = filter->ctx;
280
281 if (an_bit != AN_RES_WAIT_HTTP)
282 goto end;
283
284 /* Here we need to check if any compression filter precedes the cache
285 * filter. This is only possible when the compression is configured in
286 * the frontend while the cache filter is configured on the
287 * backend. This case cannot be detected during HAProxy startup. So in
288 * such cases, the cache is disabled.
289 */
290 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
291 pool_free(pool_head_cache_st, st);
292 filter->ctx = NULL;
293 }
294
295 end:
296 return 1;
297}
William Lallemand49dc0482017-11-24 14:33:54 +0100298
299static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100300cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
301{
302 struct cache_st *st = filter->ctx;
303
William Lallemand4da3f8a2017-10-31 14:33:34 +0100304 if (!(msg->chn->flags & CF_ISRESP) || !st)
305 return 1;
306
Christopher Faulet67658c92018-12-06 21:59:39 +0100307 if (st->first_block) {
308 register_data_filter(s, msg->chn, filter);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100309 if (!IS_HTX_STRM(s))
310 st->hdrs_len = msg->sov;
Christopher Faulet67658c92018-12-06 21:59:39 +0100311 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100312 return 1;
313}
314
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200315static inline void disable_cache_entry(struct cache_st *st,
316 struct filter *filter, struct shared_context *shctx)
317{
318 struct cache_entry *object;
319
320 object = (struct cache_entry *)st->first_block->data;
321 filter->ctx = NULL; /* disable cache */
322 shctx_lock(shctx);
323 shctx_row_dec_hot(shctx, st->first_block);
324 object->eb.key = 0;
325 shctx_unlock(shctx);
326 pool_free(pool_head_cache_st, st);
327}
328
William Lallemand4da3f8a2017-10-31 14:33:34 +0100329static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100330cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
331 unsigned int offset, unsigned int len)
332{
Christopher Faulet95220e22018-12-07 17:34:39 +0100333 struct cache_flt_conf *cconf = FLT_CONF(filter);
334 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100335 struct cache_st *st = filter->ctx;
336 struct htx *htx = htxbuf(&msg->chn->buf);
337 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200338 struct shared_block *fb;
Christopher Faulet482eab42020-03-02 16:19:50 +0100339 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200340 unsigned int orig_len, to_forward;
341 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100342
343 if (!len)
344 return len;
345
346 if (!st->first_block) {
347 unregister_data_filter(s, msg->chn, filter);
348 return len;
349 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100350
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200351 chunk_reset(&trash);
352 orig_len = len;
353 to_forward = 0;
Christopher Faulet482eab42020-03-02 16:19:50 +0100354
355 htxret = htx_find_offset(htx, offset);
356 blk = htxret.blk;
357 offset = htxret.ret;
358 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100359 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200360 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100361 struct ist v;
362
363 switch (type) {
364 case HTX_BLK_UNUSED:
365 break;
366
367 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100368 v = htx_get_blk_value(htx, blk);
369 v.ptr += offset;
370 v.len -= offset;
371 if (v.len > len)
372 v.len = len;
373
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200374 info = (type << 28) + v.len;
375 chunk_memcat(&trash, (char *)&info, sizeof(info));
376 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100377 to_forward += v.len;
378 len -= v.len;
379 break;
380
381 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200382 /* Here offset must always be 0 because only
383 * DATA blocks can be partially transferred. */
384 if (offset)
385 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100386 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200387 goto end;
388
389 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
390 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100391 to_forward += sz;
392 len -= sz;
393 break;
394 }
395
396 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100397 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200398
399 end:
400 shctx_lock(shctx);
401 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
402 if (!fb) {
403 shctx_unlock(shctx);
404 goto no_cache;
405 }
406 shctx_unlock(shctx);
407
408 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
409 (unsigned char *)b_head(&trash), b_data(&trash));
410 if (ret < 0)
411 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100412
413 return to_forward;
414
415 no_cache:
416 disable_cache_entry(st, filter, shctx);
417 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200418 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100419}
420
421static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100422cache_store_http_forward_data(struct stream *s, struct filter *filter,
423 struct http_msg *msg, unsigned int len)
424{
425 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100426 struct cache_flt_conf *cconf = FLT_CONF(filter);
427 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100428 int ret;
429
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200430 ret = 0;
431
William Lallemand4da3f8a2017-10-31 14:33:34 +0100432 /*
433 * We need to skip the HTTP headers first, because we saved them in the
434 * http-response action.
435 */
Christopher Faulet67658c92018-12-06 21:59:39 +0100436 if (!(msg->chn->flags & CF_ISRESP) || !st) {
437 /* should never happen */
438 unregister_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100439 return len;
Christopher Faulet67658c92018-12-06 21:59:39 +0100440 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100441
442 if (!len) {
Joseph Herlant8dae5b32018-11-15 14:07:53 -0800443 /* Nothing to forward */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100444 ret = len;
445 }
William Lallemand10935bc2017-11-14 14:39:23 +0100446 else if (st->hdrs_len >= len) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100447 /* Forward part of headers */
448 ret = len;
449 st->hdrs_len -= len;
450 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100451 else {
William Lallemand10935bc2017-11-14 14:39:23 +0100452 /* Forward data */
Christopher Faulet67658c92018-12-06 21:59:39 +0100453 if (st->first_block) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200454 int to_append, append;
455 struct shared_block *fb;
456
457 to_append = MIN(ci_contig_data(msg->chn), len - st->hdrs_len);
458
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200459 shctx_lock(shctx);
460 fb = shctx_row_reserve_hot(shctx, st->first_block, to_append);
461 if (!fb) {
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100462 shctx_unlock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200463 disable_cache_entry(st, filter, shctx);
Christopher Faulet67658c92018-12-06 21:59:39 +0100464 unregister_data_filter(s, msg->chn, filter);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200465 return len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100466 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200467 shctx_unlock(shctx);
468
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200469 /* Skip remaining headers to fill the cache */
470 c_adv(msg->chn, st->hdrs_len);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200471 append = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
472 (unsigned char *)ci_head(msg->chn), to_append);
473 ret = st->hdrs_len + to_append - append;
474 /* Rewind the buffer to forward all data */
475 c_rew(msg->chn, st->hdrs_len);
476 st->hdrs_len = 0;
Christopher Faulet67658c92018-12-06 21:59:39 +0100477 if (ret < 0) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200478 disable_cache_entry(st, filter, shctx);
Christopher Faulet67658c92018-12-06 21:59:39 +0100479 unregister_data_filter(s, msg->chn, filter);
480 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100481 }
Christopher Faulet67658c92018-12-06 21:59:39 +0100482 else {
483 /* should never happen */
484 unregister_data_filter(s, msg->chn, filter);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200485 ret = len;
Christopher Faulet67658c92018-12-06 21:59:39 +0100486 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100487 }
488
489 if ((ret != len) ||
490 (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret))
491 task_wakeup(s->task, TASK_WOKEN_MSG);
492
493 return ret;
494}
495
496static int
497cache_store_http_end(struct stream *s, struct filter *filter,
498 struct http_msg *msg)
499{
500 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100501 struct cache_flt_conf *cconf = FLT_CONF(filter);
502 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100503 struct shared_context *shctx = shctx_ptr(cache);
504 struct cache_entry *object;
505
506 if (!(msg->chn->flags & CF_ISRESP))
507 return 1;
508
509 if (st && st->first_block) {
510
511 object = (struct cache_entry *)st->first_block->data;
512
513 /* does not need to test if the insertion worked, if it
514 * doesn't, the blocks will be reused anyway */
515
516 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100517 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
518 object->eb.key = 0;
519 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100520 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100521 shctx_row_dec_hot(shctx, st->first_block);
522 shctx_unlock(shctx);
523
524 }
525 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100526 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100527 filter->ctx = NULL;
528 }
529
530 return 1;
531}
532
533 /*
534 * This intends to be used when checking HTTP headers for some
535 * word=value directive. Return a pointer to the first character of value, if
536 * the word was not found or if there wasn't any value assigned ot it return NULL
537 */
538char *directive_value(const char *sample, int slen, const char *word, int wlen)
539{
540 int st = 0;
541
542 if (slen < wlen)
543 return 0;
544
545 while (wlen) {
546 char c = *sample ^ *word;
547 if (c && c != ('A' ^ 'a'))
548 return NULL;
549 sample++;
550 word++;
551 slen--;
552 wlen--;
553 }
554
555 while (slen) {
556 if (st == 0) {
557 if (*sample != '=')
558 return NULL;
559 sample++;
560 slen--;
561 st = 1;
562 continue;
563 } else {
564 return (char *)sample;
565 }
566 }
567
568 return NULL;
569}
570
571/*
572 * Return the maxage in seconds of an HTTP response.
573 * Compute the maxage using either:
574 * - the assigned max-age of the cache
575 * - the s-maxage directive
576 * - the max-age directive
577 * - (Expires - Data) headers
578 * - the default-max-age of the cache
579 *
580 */
William Lallemand49b44532017-11-24 18:53:43 +0100581int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100582{
William Lallemand4da3f8a2017-10-31 14:33:34 +0100583 int smaxage = -1;
584 int maxage = -1;
585
Christopher Fauleta63ae1e2019-07-15 20:49:46 +0200586
587 if (IS_HTX_STRM(s)) {
588 /* HTX mode */
589 struct htx *htx = htxbuf(&s->res.buf);
590 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100591
Christopher Fauleta63ae1e2019-07-15 20:49:46 +0200592 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
593 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100594
Christopher Fauleta63ae1e2019-07-15 20:49:46 +0200595 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
596 if (value) {
597 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100598
Christopher Fauleta63ae1e2019-07-15 20:49:46 +0200599 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
600 chunk_strncat(chk, "", 1);
601 maxage = atoi(chk->area);
602 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100603
Christopher Fauleta63ae1e2019-07-15 20:49:46 +0200604 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
605 if (value) {
606 struct buffer *chk = get_trash_chunk();
607
608 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
609 chunk_strncat(chk, "", 1);
610 smaxage = atoi(chk->area);
611 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100612 }
Christopher Fauleta63ae1e2019-07-15 20:49:46 +0200613 }
614 else {
615 /* Legacy mode */
616 struct http_txn *txn = s->txn;
617 struct hdr_ctx ctx;
618
619 ctx.idx = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100620
Christopher Fauleta63ae1e2019-07-15 20:49:46 +0200621 /* loop on the Cache-Control values */
622 while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) {
623 char *directive = ctx.line + ctx.val;
624 char *value;
625
626 value = directive_value(directive, ctx.vlen, "s-maxage", 8);
627 if (value) {
628 struct buffer *chk = get_trash_chunk();
629
630 chunk_strncat(chk, value, ctx.vlen - 8 + 1);
631 chunk_strncat(chk, "", 1);
632 maxage = atoi(chk->area);
633 }
634
635 value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
636 if (value) {
637 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100638
Christopher Fauleta63ae1e2019-07-15 20:49:46 +0200639 chunk_strncat(chk, value, ctx.vlen - 7 + 1);
640 chunk_strncat(chk, "", 1);
641 smaxage = atoi(chk->area);
642 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100643 }
644 }
645
646 /* TODO: Expires - Data */
647
648
649 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100650 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100651
652 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100653 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100654
William Lallemand49b44532017-11-24 18:53:43 +0100655 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100656
657}
658
659
William Lallemanda400a3a2017-11-20 19:13:12 +0100660static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
661{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200662 struct cache_entry *object = (struct cache_entry *)block->data;
663
664 if (first == block && object->eb.key)
665 eb32_delete(&object->eb);
666 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100667}
668
William Lallemand41db4602017-10-30 11:15:51 +0100669/*
670 * This fonction will store the headers of the response in a buffer and then
671 * register a filter to store the data
672 */
673enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200674 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100675{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200676 unsigned int age;
677 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100678 struct http_txn *txn = s->txn;
679 struct http_msg *msg = &txn->rsp;
680 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100681 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100682 struct cache_flt_conf *cconf = rule->arg.act.p[0];
683 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100684 struct cache_st *cache_ctx = NULL;
685 struct cache_entry *object, *old;
Willy Tarreauc9036c02019-01-11 19:38:25 +0100686 unsigned int key = *(unsigned int *)txn->cache_hash;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100687
William Lallemand4da3f8a2017-10-31 14:33:34 +0100688 /* Don't cache if the response came from a cache */
689 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
690 s->target == &http_cache_applet.obj_type) {
691 goto out;
692 }
693
694 /* cache only HTTP/1.1 */
695 if (!(txn->req.flags & HTTP_MSGF_VER_11))
696 goto out;
697
698 /* cache only GET method */
699 if (txn->meth != HTTP_METH_GET)
700 goto out;
701
Willy Tarreauc9036c02019-01-11 19:38:25 +0100702 /* cache key was not computed */
703 if (!key)
704 goto out;
705
William Lallemand4da3f8a2017-10-31 14:33:34 +0100706 /* cache only 200 status code */
707 if (txn->status != 200)
708 goto out;
709
Christopher Faulet839791a2019-01-07 16:12:07 +0100710 /* Find the corresponding filter instance for the current stream */
711 list_for_each_entry(filter, &s->strm_flt.filters, list) {
712 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
713 /* No filter ctx, don't cache anything */
714 if (!filter->ctx)
715 goto out;
716 cache_ctx = filter->ctx;
717 break;
718 }
719 }
720
721 /* from there, cache_ctx is always defined */
722
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100723 if (IS_HTX_STRM(s)) {
724 struct htx *htx = htxbuf(&s->res.buf);
725 struct http_hdr_ctx ctx;
Christopher Faulet683694e2019-09-03 22:22:12 +0200726 size_t hdrs_len = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100727 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100728
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100729 /* Do not cache too big objects. */
730 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
731 htx->data + htx->extra > shctx->max_obj_size)
732 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100733
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100734 /* Does not manage Vary at the moment. We will need a secondary key later for that */
735 ctx.blk = NULL;
736 if (http_find_header(htx, ist("Vary"), &ctx, 0))
737 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100738
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100739 htx_check_response_for_cacheability(s, &s->res);
740
741 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
742 goto out;
743
744 age = 0;
745 ctx.blk = NULL;
746 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
747 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
748 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
749 hdr_age = CACHE_ENTRY_MAX_AGE;
750 age = hdr_age;
751 }
752 http_remove_header(htx, &ctx);
753 }
754
755 chunk_reset(&trash);
Christopher Fauleta3f15502019-05-13 15:27:23 +0200756 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100757 struct htx_blk *blk = htx_get_blk(htx, pos);
Christopher Fauletafd819c2018-12-11 08:57:45 +0100758 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100759 uint32_t sz = htx_get_blksz(blk);
760
Christopher Faulet683694e2019-09-03 22:22:12 +0200761 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100762 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200763 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Fauletafd819c2018-12-11 08:57:45 +0100764 if (type == HTX_BLK_EOH)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100765 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100766 }
Christopher Faulet683694e2019-09-03 22:22:12 +0200767
768 /* Do not cache objects if the headers are too big. */
769 if (hdrs_len > htx->size - global.tune.maxrewrite)
770 goto out;
771
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100772 }
773 else {
774 struct hdr_ctx ctx;
775
776 /* Do not cache too big objects. */
777 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
778 msg->sov + msg->body_len > shctx->max_obj_size)
779 goto out;
780
781 /* Does not manage Vary at the moment. We will need a secondary key later for that */
782 ctx.idx = 0;
783 if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx))
784 goto out;
785
786 check_response_for_cacheability(s, &s->res);
787
788 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
789 goto out;
790
791 age = 0;
792 ctx.idx = 0;
793 if (http_find_header2("Age", 3, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
794 if (!strl2llrc(ctx.line + ctx.val, ctx.vlen, &hdr_age) && hdr_age > 0) {
795 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
796 hdr_age = CACHE_ENTRY_MAX_AGE;
797 age = hdr_age;
798 }
799 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200800 }
Christopher Faulet683694e2019-09-03 22:22:12 +0200801
802 /* Do not cache objects if the headers are too big. */
803 if (msg->sov > c_size(txn->rsp.chn) - global.tune.maxrewrite)
804 goto out;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200805 }
806
William Lallemand4da3f8a2017-10-31 14:33:34 +0100807 shctx_lock(shctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100808 if (IS_HTX_STRM(s))
809 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
810 else
811 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + msg->sov);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100812 if (!first) {
813 shctx_unlock(shctx);
814 goto out;
815 }
816 shctx_unlock(shctx);
817
Willy Tarreau1093a452018-04-06 19:02:25 +0200818 /* the received memory is not initialized, we need at least to mark
819 * the object as not indexed yet.
820 */
821 object = (struct cache_entry *)first->data;
822 object->eb.node.leaf_p = NULL;
823 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200824 object->age = age;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200825 if (!IS_HTX_STRM(s))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100826 object->eoh = msg->eoh;
Willy Tarreau1093a452018-04-06 19:02:25 +0200827
William Lallemand4da3f8a2017-10-31 14:33:34 +0100828 /* reserve space for the cache_entry structure */
829 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200830 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100831 /* cache the headers in a http action because it allows to chose what
832 * to cache, for example you might want to cache a response before
833 * modifying some HTTP headers, or on the contrary after modifying
834 * those headers.
835 */
836
837 /* does not need to be locked because it's in the "hot" list,
838 * copy the headers */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100839 if (IS_HTX_STRM(s)) {
840 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
841 goto out;
842 }
843 else {
844 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)ci_head(&s->res), msg->sov) < 0)
845 goto out;
846 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100847
848 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100849 if (cache_ctx) {
850 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100851
Willy Tarreauc9036c02019-01-11 19:38:25 +0100852 object->eb.key = key;
853
Christopher Faulet839791a2019-01-07 16:12:07 +0100854 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
855 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100856
Christopher Faulet839791a2019-01-07 16:12:07 +0100857 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100858
Christopher Faulet839791a2019-01-07 16:12:07 +0100859 old = entry_exist(cconf->c.cache, txn->cache_hash);
860 if (old) {
861 eb32_delete(&old->eb);
862 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100863 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100864 shctx_unlock(shctx);
865
866 /* store latest value and expiration time */
867 object->latest_validation = now.tv_sec;
868 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
869 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100870 }
871
872out:
873 /* if does not cache */
874 if (first) {
875 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100876 first->len = 0;
877 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100878 shctx_row_dec_hot(shctx, first);
879 shctx_unlock(shctx);
880 }
881
William Lallemand41db4602017-10-30 11:15:51 +0100882 return ACT_RET_CONT;
883}
884
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200885#define HTTP_CACHE_INIT 0 /* Initial state. */
886#define HTTP_CACHE_HEADER 1 /* Cache entry headers forwarded. */
887#define HTTP_CACHE_FWD 2 /* Cache entry completely forwarded. */
888#define HTTP_CACHE_END 3 /* Cache entry treatment terminated. */
William Lallemand77c11972017-10-31 20:43:01 +0100889
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100890#define HTX_CACHE_INIT 0 /* Initial state. */
891#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
892#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200893#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
894#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100895
William Lallemandecb73b12017-11-24 14:33:55 +0100896static void http_cache_applet_release(struct appctx *appctx)
897{
Christopher Faulet95220e22018-12-07 17:34:39 +0100898 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100899 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100900 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100901 struct shared_block *first = block_ptr(cache_ptr);
902
903 shctx_lock(shctx_ptr(cache));
904 shctx_row_dec_hot(shctx_ptr(cache), first);
905 shctx_unlock(shctx_ptr(cache));
906}
907
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200908
909static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
910 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100911{
Christopher Faulet95220e22018-12-07 17:34:39 +0100912 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
913 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200914 struct htx_blk *blk;
Christopher Faulet554b3bc2019-09-03 22:11:52 +0200915 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200916 unsigned int max, total;
917 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100918
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200919 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
920 if (!max)
921 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200922 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200923 ? (info & 0xff) + ((info >> 8) & 0xfffff)
924 : info & 0xfffffff);
925 if (blksz > max)
926 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100927
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200928 blk = htx_add_blk(htx, type, blksz);
929 if (!blk)
930 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100931
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200932 blk->info = info;
933 total = 4;
Christopher Faulet554b3bc2019-09-03 22:11:52 +0200934 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200935 while (blksz) {
936 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet554b3bc2019-09-03 22:11:52 +0200937 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200938 offset += max;
939 blksz -= max;
940 total += max;
Christopher Faulet554b3bc2019-09-03 22:11:52 +0200941 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200942 if (blksz || offset == shctx->block_size) {
943 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
944 offset = 0;
945 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100946 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200947 appctx->ctx.cache.offset = offset;
948 appctx->ctx.cache.next = shblk;
949 appctx->ctx.cache.sent += total;
950 return total;
951}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100952
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200953static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
954 uint32_t info, struct shared_block *shblk, unsigned int offset)
955{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100956
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200957 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
958 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
959 unsigned int max, total, rem_data;
960 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100961
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200962 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
963 if (!max)
964 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100965
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200966 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200967 if (appctx->ctx.cache.rem_data) {
968 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200969 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200970 }
971 else {
972 blksz = (info & 0xfffffff);
973 total = 4;
974 }
975 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200976 rem_data = blksz - max;
977 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100978 }
979
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200980 while (blksz) {
981 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100982
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200983 max = MIN(blksz, shctx->block_size - offset);
984 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
985 offset += sz;
986 blksz -= sz;
987 total += sz;
988 if (sz < max)
989 break;
990 if (blksz || offset == shctx->block_size) {
991 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
992 offset = 0;
993 }
994 }
995
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200996 appctx->ctx.cache.offset = offset;
997 appctx->ctx.cache.next = shblk;
998 appctx->ctx.cache.sent += total;
999 appctx->ctx.cache.rem_data = rem_data + blksz;
1000 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001001}
1002
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001003static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
1004 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001005{
Christopher Faulet95220e22018-12-07 17:34:39 +01001006 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1007 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001008 struct shared_block *shblk;
1009 unsigned int offset, sz;
1010 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001011
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001012 while (len) {
1013 enum htx_blk_type type;
1014 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001015
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001016 shblk = appctx->ctx.cache.next;
1017 offset = appctx->ctx.cache.offset;
1018 if (appctx->ctx.cache.rem_data) {
1019 type = HTX_BLK_DATA;
1020 info = 0;
1021 goto add_data_blk;
1022 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001023
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001024 /* Get info of the next HTX block. May be splitted on 2 shblk */
1025 sz = MIN(4, shctx->block_size - offset);
1026 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
1027 offset += sz;
1028 if (sz < 4) {
1029 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
1030 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
1031 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001032 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001033
1034 /* Get payload of the next HTX block and insert it. */
1035 type = (info >> 28);
1036 if (type != HTX_BLK_DATA)
1037 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
1038 else {
1039 add_data_blk:
1040 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001041 }
1042
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001043 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001044 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001045 total += ret;
1046 len -= ret;
1047
1048 if (appctx->ctx.cache.rem_data || type == mark)
1049 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001050 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001051
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001052 return total;
1053}
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001054
1055static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
1056{
1057 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1058 unsigned int age;
1059 char *end;
1060
1061 chunk_reset(&trash);
1062 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
1063 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1064 age = CACHE_ENTRY_MAX_AGE;
1065 end = ultoa_o(age, b_head(&trash), b_size(&trash));
1066 b_set_data(&trash, end - b_head(&trash));
1067 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
1068 return 0;
1069 return 1;
1070}
1071
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001072static void htx_cache_io_handler(struct appctx *appctx)
1073{
1074 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1075 struct shared_block *first = block_ptr(cache_ptr);
1076 struct stream_interface *si = appctx->owner;
1077 struct channel *req = si_oc(si);
1078 struct channel *res = si_ic(si);
1079 struct htx *req_htx, *res_htx;
1080 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001081 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001082 size_t ret, total = 0;
1083
1084 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001085 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001086
1087 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1088 goto out;
1089
1090 /* Check if the input buffer is avalaible. */
1091 if (!b_size(&res->buf)) {
1092 si_rx_room_blk(si);
1093 goto out;
1094 }
1095
Willy Tarreauefef3232018-12-16 00:37:45 +01001096 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +01001097 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001098
1099 if (appctx->st0 == HTX_CACHE_INIT) {
1100 appctx->ctx.cache.next = block_ptr(cache_ptr);
1101 appctx->ctx.cache.offset = sizeof(*cache_ptr);
1102 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001103 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001104 appctx->st0 = HTX_CACHE_HEADER;
1105 }
1106
1107 if (appctx->st0 == HTX_CACHE_HEADER) {
1108 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001109 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1110 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
1111 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
1112 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001113 goto error;
1114
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001115 /* Skip response body for HEAD requests */
1116 if (si_strm(si)->txn->meth == HTTP_METH_HEAD)
Christopher Fauletf0dd0372019-02-25 11:08:34 +01001117 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001118 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001119 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001120 }
1121
1122 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001123 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1124 if (len) {
1125 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
1126 if (ret < len) {
1127 si_rx_room_blk(si);
1128 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001129 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001130 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001131 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001132 }
1133
1134 if (appctx->st0 == HTX_CACHE_EOM) {
1135 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1136 si_rx_room_blk(si);
1137 goto out;
1138 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001139 appctx->st0 = HTX_CACHE_END;
1140 }
1141
1142 end:
Christopher Fauletadb36312019-02-25 11:40:49 +01001143 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001144 res->flags |= CF_READ_NULL;
1145 si_shutr(si);
1146 }
1147
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001148 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001149 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +01001150 if (total)
1151 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001152 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +01001153
1154 /* eat the whole request */
1155 if (co_data(req)) {
1156 req_htx = htx_from_buf(&req->buf);
1157 co_htx_skip(req, req_htx, co_data(req));
1158 htx_to_buf(req_htx, &req->buf);
1159 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001160 return;
1161
1162 error:
1163 /* Sent and HTTP error 500 */
1164 b_reset(&res->buf);
1165 errmsg = &htx_err_chunks[HTTP_ERR_500];
1166 res->buf.data = b_data(errmsg);
1167 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1168 res_htx = htx_from_buf(&res->buf);
1169
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001170 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001171 appctx->st0 = HTX_CACHE_END;
1172 goto end;
1173}
1174
1175
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001176/*
1177 * Append an "Age" header into <chn> channel for this <ce> cache entry.
Joseph Herlant8dae5b32018-11-15 14:07:53 -08001178 * This is the responsibility of the caller to insure there is enough
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001179 * data in the channel.
1180 *
1181 * Returns the number of bytes inserted if succeeded, 0 if failed.
1182 */
1183static int cache_channel_append_age_header(struct cache_entry *ce, struct channel *chn)
1184{
1185 unsigned int age;
1186
1187 age = MAX(0, (int)(now.tv_sec - ce->latest_validation)) + ce->age;
1188 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1189 age = CACHE_ENTRY_MAX_AGE;
1190
1191 chunk_reset(&trash);
1192 chunk_printf(&trash, "Age: %u", age);
1193
1194 return ci_insert_line2(chn, ce->eoh, trash.area, trash.data);
1195}
1196
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001197static int cache_channel_row_data_get(struct appctx *appctx, int len)
William Lallemand77c11972017-10-31 20:43:01 +01001198{
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001199 int ret, total;
William Lallemand77c11972017-10-31 20:43:01 +01001200 struct stream_interface *si = appctx->owner;
1201 struct channel *res = si_ic(si);
Christopher Faulet95220e22018-12-07 17:34:39 +01001202 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1203 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001204 struct shared_context *shctx = shctx_ptr(cache);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001205 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1206 struct shared_block *blk, *next = appctx->ctx.cache.next;
1207 int offset;
1208
1209 total = 0;
1210 offset = 0;
1211
1212 if (!next) {
1213 offset = sizeof(struct cache_entry);
1214 next = block_ptr(cache_ptr);
1215 }
1216
1217 blk = next;
1218 list_for_each_entry_from(blk, &shctx->hot, list) {
1219 int sz;
1220
1221 if (len <= 0)
1222 break;
1223
1224 sz = MIN(len, shctx->block_size - offset);
1225
1226 ret = ci_putblk(res, (const char *)blk->data + offset, sz);
1227 if (unlikely(offset))
1228 offset = 0;
1229 if (ret <= 0) {
1230 if (ret == -3 || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001231 si_rx_room_blk(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001232 break;
1233 }
1234 return -1;
1235 }
1236
1237 total += sz;
1238 len -= sz;
1239 }
1240 appctx->ctx.cache.next = blk;
1241
1242 return total;
1243}
1244
1245static void http_cache_io_handler(struct appctx *appctx)
1246{
1247 struct stream_interface *si = appctx->owner;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001248 struct stream *s = si_strm(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001249 struct channel *res = si_ic(si);
1250 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
William Lallemand77c11972017-10-31 20:43:01 +01001251 struct shared_block *first = block_ptr(cache_ptr);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001252 unsigned int *sent = &appctx->ctx.cache.sent;
1253
1254 if (IS_HTX_STRM(s))
1255 return htx_cache_io_handler(appctx);
William Lallemand77c11972017-10-31 20:43:01 +01001256
1257 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1258 goto out;
1259
Joseph Herlant8dae5b32018-11-15 14:07:53 -08001260 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001261 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01001262 /* buf.size==0 means we failed to get a buffer and were
1263 * already subscribed to a wait list to get a buffer.
1264 */
William Lallemand77c11972017-10-31 20:43:01 +01001265 goto out;
1266 }
1267
Willy Tarreauefef3232018-12-16 00:37:45 +01001268 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
William Lallemand77c11972017-10-31 20:43:01 +01001269 appctx->st0 = HTTP_CACHE_END;
1270
1271 /* buffer are aligned there, should be fine */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001272 if (appctx->st0 == HTTP_CACHE_HEADER || appctx->st0 == HTTP_CACHE_INIT) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001273 int len = first->len - *sent - sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001274 if (len > 0) {
1275 int ret;
1276
1277 ret = cache_channel_row_data_get(appctx, len);
1278 if (ret == -1)
1279 appctx->st0 = HTTP_CACHE_END;
1280 else
1281 *sent += ret;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001282 if (appctx->st0 == HTTP_CACHE_INIT && *sent > cache_ptr->eoh &&
1283 cache_channel_append_age_header(cache_ptr, res))
1284 appctx->st0 = HTTP_CACHE_HEADER;
Christopher Faulet61123912019-01-02 14:10:01 +01001285 else if (ret == len) {
1286 *sent = 0;
1287 appctx->st0 = HTTP_CACHE_FWD;
1288 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001289 }
1290 else {
1291 *sent = 0;
1292 appctx->st0 = HTTP_CACHE_FWD;
William Lallemand77c11972017-10-31 20:43:01 +01001293 }
William Lallemand77c11972017-10-31 20:43:01 +01001294 }
1295
Christopher Fauletadb36312019-02-25 11:40:49 +01001296 if (appctx->st0 == HTTP_CACHE_FWD)
1297 appctx->st0 = HTTP_CACHE_END;
1298
1299 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTTP_CACHE_END) {
William Lallemand77c11972017-10-31 20:43:01 +01001300 res->flags |= CF_READ_NULL;
1301 si_shutr(si);
1302 }
William Lallemand77c11972017-10-31 20:43:01 +01001303out:
Christopher Fauletadb36312019-02-25 11:40:49 +01001304 /* eat the whole request */
1305 if (co_data(si_oc(si)))
1306 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand77c11972017-10-31 20:43:01 +01001307}
1308
Christopher Faulet95220e22018-12-07 17:34:39 +01001309static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001310{
1311 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001312 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001313
Christopher Faulet95220e22018-12-07 17:34:39 +01001314 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001315 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001316 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001317 }
1318
1319 /* check if a cache filter was already registered with this cache
1320 * name, if that's the case, must use it. */
1321 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001322 if (fconf->id == cache_store_flt_id) {
1323 cconf = fconf->conf;
1324 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1325 rule->arg.act.p[0] = cconf;
1326 return 1;
1327 }
William Lallemand41db4602017-10-30 11:15:51 +01001328 }
1329 }
1330
Christopher Faulet95220e22018-12-07 17:34:39 +01001331 /* Create the filter cache config */
1332 cconf = calloc(1, sizeof(*cconf));
1333 if (!cconf) {
1334 memprintf(err, "out of memory\n");
1335 goto err;
1336 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001337 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001338 cconf->c.name = strdup(name);
1339 if (!cconf->c.name) {
1340 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001341 goto err;
1342 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001343
William Lallemand41db4602017-10-30 11:15:51 +01001344 /* register a filter to fill the cache buffer */
1345 fconf = calloc(1, sizeof(*fconf));
1346 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001347 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001348 goto err;
1349 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001350 fconf->id = cache_store_flt_id;
1351 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001352 fconf->ops = &cache_ops;
1353 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1354
Christopher Faulet95220e22018-12-07 17:34:39 +01001355 rule->arg.act.p[0] = cconf;
1356 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001357
Christopher Faulet95220e22018-12-07 17:34:39 +01001358 err:
1359 free(cconf);
1360 return 0;
1361}
1362
1363enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1364 struct act_rule *rule, char **err)
1365{
1366 rule->action = ACT_CUSTOM;
1367 rule->action_ptr = http_action_store_cache;
1368
1369 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1370 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001371
Christopher Faulet95220e22018-12-07 17:34:39 +01001372 (*orig_arg)++;
1373 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001374}
1375
William Lallemandf528fff2017-11-23 19:43:17 +01001376/* This produces a sha1 hash of the concatenation of the first
1377 * occurrence of the Host header followed by the path component if it
1378 * begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001379int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001380{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001381 struct http_txn *txn = s->txn;
William Lallemandf528fff2017-11-23 19:43:17 +01001382 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001383 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001384
William Lallemandf528fff2017-11-23 19:43:17 +01001385 trash = get_trash_chunk();
1386
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001387 if (IS_HTX_STRM(s)) {
1388 struct htx *htx = htxbuf(&s->req.buf);
1389 struct htx_sl *sl;
1390 struct http_hdr_ctx ctx;
1391 struct ist path;
William Lallemandf528fff2017-11-23 19:43:17 +01001392
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001393 ctx.blk = NULL;
1394 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1395 return 0;
1396 chunk_memcat(trash, ctx.value.ptr, ctx.value.len);
1397
Christopher Faulet297fbb42019-05-13 14:41:27 +02001398 sl = http_get_stline(htx);
Willy Tarreaue7756442019-10-07 14:06:34 +02001399 path = htx_sl_req_uri(sl); // whole uri
1400 if (!path.len || *path.ptr != '/')
1401 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001402 chunk_memcat(trash, path.ptr, path.len);
1403 }
1404 else {
1405 struct hdr_ctx ctx;
1406 char *path;
1407 char *end;
1408
1409 /* retrive the host */
1410 ctx.idx = 0;
1411 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx))
1412 return 0;
1413 chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen);
1414
1415 /* now retrieve the path */
1416 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaue7756442019-10-07 14:06:34 +02001417 path = ci_head(txn->req.chn) + txn->req.sl.rq.u;
1418 if (!txn->req.sl.rq.u_l || *path != '/')
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001419 return 0;
1420 chunk_strncat(trash, path, end - path);
1421 }
William Lallemandf528fff2017-11-23 19:43:17 +01001422
1423 /* hash everything */
1424 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001425 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001426 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1427
1428 return 1;
1429}
1430
William Lallemand41db4602017-10-30 11:15:51 +01001431enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1432 struct session *sess, struct stream *s, int flags)
1433{
William Lallemand77c11972017-10-31 20:43:01 +01001434
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001435 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001436 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001437 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1438 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001439
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001440 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1441 * and HEAD */
1442 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
1443 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
1444 txn->flags |= TX_CACHE_IGNORE;
1445
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001446 if (IS_HTX_STRM(s))
1447 htx_check_request_for_cacheability(s, &s->req);
1448 else
1449 check_request_for_cacheability(s, &s->req);
1450
Willy Tarreau504455c2017-12-22 17:47:35 +01001451 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1452 return ACT_RET_CONT;
1453
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001454 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001455 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001456
Willy Tarreau504455c2017-12-22 17:47:35 +01001457 if (s->txn->flags & TX_CACHE_IGNORE)
1458 return ACT_RET_CONT;
1459
Willy Tarreaua1214a52018-12-14 14:00:25 +01001460 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001461 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001462 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001463 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001464
William Lallemanda400a3a2017-11-20 19:13:12 +01001465 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001466 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001467 if (res) {
1468 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001469 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1470 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001471 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001472 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
William Lallemand77c11972017-10-31 20:43:01 +01001473 appctx->st0 = HTTP_CACHE_INIT;
1474 appctx->rule = rule;
1475 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001476 appctx->ctx.cache.next = NULL;
1477 appctx->ctx.cache.sent = 0;
Willy Tarreaua1214a52018-12-14 14:00:25 +01001478
1479 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001480 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001481 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001482 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001483 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001484 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001485 shctx_lock(shctx_ptr(cache));
1486 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1487 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001488 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001489 }
1490 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001491 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001492 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001493}
1494
1495
1496enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1497 struct act_rule *rule, char **err)
1498{
William Lallemand41db4602017-10-30 11:15:51 +01001499 rule->action = ACT_CUSTOM;
1500 rule->action_ptr = http_action_req_cache_use;
1501
Christopher Faulet95220e22018-12-07 17:34:39 +01001502 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001503 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001504
1505 (*orig_arg)++;
1506 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001507}
1508
1509int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1510{
1511 int err_code = 0;
1512
1513 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1514
1515 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001516 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1517 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001518 err_code |= ERR_ALERT | ERR_ABORT;
1519 goto out;
1520 }
1521
1522 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1523 err_code |= ERR_ABORT;
1524 goto out;
1525 }
1526
1527 if (tmp_cache_config == NULL) {
1528 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1529 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001530 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001531 err_code |= ERR_ALERT | ERR_ABORT;
1532 goto out;
1533 }
1534
1535 strlcpy2(tmp_cache_config->id, args[1], 33);
1536 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001537 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1538 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001539 err_code |= ERR_WARN;
1540 }
William Lallemand49b44532017-11-24 18:53:43 +01001541 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001542 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001543 tmp_cache_config->maxobjsz = 0;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001544 tmp_cache_config->flags = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001545 }
1546 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001547 unsigned long int maxsize;
1548 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001549
1550 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1551 err_code |= ERR_ABORT;
1552 goto out;
1553 }
1554
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001555 maxsize = strtoul(args[1], &err, 10);
1556 if (err == args[1] || *err != '\0') {
1557 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1558 file, linenum, args[1]);
1559 err_code |= ERR_ABORT;
1560 goto out;
1561 }
1562
1563 if (maxsize > (UINT_MAX >> 20)) {
1564 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1565 file, linenum, args[1], UINT_MAX >> 20);
1566 err_code |= ERR_ABORT;
1567 goto out;
1568 }
1569
William Lallemand41db4602017-10-30 11:15:51 +01001570 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001571 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001572 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001573 } else if (strcmp(args[0], "max-age") == 0) {
1574 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1575 err_code |= ERR_ABORT;
1576 goto out;
1577 }
1578
1579 if (!*args[1]) {
1580 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1581 file, linenum, args[0]);
1582 err_code |= ERR_WARN;
1583 }
1584
1585 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001586 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001587 unsigned int maxobjsz;
1588 char *err;
1589
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001590 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1591 err_code |= ERR_ABORT;
1592 goto out;
1593 }
1594
1595 if (!*args[1]) {
1596 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1597 file, linenum, args[0]);
1598 err_code |= ERR_WARN;
1599 }
1600
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001601 maxobjsz = strtoul(args[1], &err, 10);
1602 if (err == args[1] || *err != '\0') {
1603 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1604 file, linenum, args[1]);
1605 err_code |= ERR_ABORT;
1606 goto out;
1607 }
1608 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001609 }
1610 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001611 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001612 err_code |= ERR_ALERT | ERR_FATAL;
1613 goto out;
1614 }
1615out:
1616 return err_code;
1617}
1618
1619/* once the cache section is parsed */
1620
1621int cfg_post_parse_section_cache()
1622{
William Lallemand41db4602017-10-30 11:15:51 +01001623 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001624
1625 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001626
1627 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001628 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001629 err_code |= ERR_FATAL | ERR_ALERT;
1630 goto out;
1631 }
1632
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001633 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001634 /* Default max. file size is a 256th of the cache size. */
1635 tmp_cache_config->maxobjsz =
1636 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001637 }
1638 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1639 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1640 err_code |= ERR_FATAL | ERR_ALERT;
1641 goto out;
1642 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001643
William Lallemande4876e22019-08-28 15:22:49 +02001644 /* add to the list of cache to init and reinit tmp_cache_config
1645 * for next cache section, if any.
1646 */
1647 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1648 tmp_cache_config = NULL;
1649 return err_code;
1650 }
1651out:
1652 free(tmp_cache_config);
1653 tmp_cache_config = NULL;
1654 return err_code;
1655
1656}
1657
1658int post_check_cache()
1659{
1660 struct proxy *px;
1661 struct cache *back, *cache_config, *cache;
1662 struct shared_context *shctx;
1663 int ret_shctx;
1664 int err_code = 0;
1665
1666 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1667
1668 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1669 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001670
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001671 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001672 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001673 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001674 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001675 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001676
1677 err_code |= ERR_FATAL | ERR_ALERT;
1678 goto out;
1679 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001680 shctx->free_block = cache_free_blocks;
William Lallemande4876e22019-08-28 15:22:49 +02001681 /* the cache structure is stored in the shctx and added to the
1682 * caches list, we can remove the entry from the caches_config
1683 * list */
1684 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001685 cache = (struct cache *)shctx->data;
1686 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001687 LIST_ADDQ(&caches, &cache->list);
William Lallemande4876e22019-08-28 15:22:49 +02001688 LIST_DEL(&cache_config->list);
1689 free(cache_config);
1690
1691 /* Find all references for this cache in the existing filters
1692 * (over all proxies) and reference it in matching filters.
1693 */
1694 for (px = proxies_list; px; px = px->next) {
1695 struct flt_conf *fconf;
1696 struct cache_flt_conf *cconf;
1697
1698 list_for_each_entry(fconf, &px->filter_configs, list) {
1699 if (fconf->id != cache_store_flt_id)
1700 continue;
1701
1702 cconf = fconf->conf;
1703 if (!strcmp(cache->id, cconf->c.name)) {
1704 free(cconf->c.name);
1705 cconf->c.cache = cache;
1706 break;
1707 }
1708 }
1709 }
William Lallemand41db4602017-10-30 11:15:51 +01001710 }
William Lallemande4876e22019-08-28 15:22:49 +02001711
William Lallemand41db4602017-10-30 11:15:51 +01001712out:
William Lallemand41db4602017-10-30 11:15:51 +01001713 return err_code;
1714
1715}
1716
1717/*
1718 * Resolve the cache name to a pointer once the file is completely read.
1719 */
1720int cfg_cache_postparser()
1721{
William Lallemand41db4602017-10-30 11:15:51 +01001722 struct cache *cache;
William Lallemand41db4602017-10-30 11:15:51 +01001723 int err = 0;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001724
1725 /* Check if the cache is used by HTX and legacy HTTP proxies in same
1726 * time
1727 */
William Lallemande4876e22019-08-28 15:22:49 +02001728 list_for_each_entry(cache, &caches_config, list) {
Christopher Faulet1f672c52018-12-03 14:30:41 +01001729 if ((cache->flags & (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) == (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) {
1730 ha_alert("Cache '%s': cannot be used by HTX and legacy HTTP proxies in same time.\n",
1731 cache->id);
1732 err++;
1733 }
1734 }
1735
William Lallemand41db4602017-10-30 11:15:51 +01001736 return err;
1737}
1738
1739
1740struct flt_ops cache_ops = {
1741 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001742 .check = cache_store_check,
1743 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001744
William Lallemand4da3f8a2017-10-31 14:33:34 +01001745 /* Handle channels activity */
1746 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001747 .channel_end_analyze = cache_store_chn_end_analyze,
Christopher Faulet839791a2019-01-07 16:12:07 +01001748 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001749
1750 /* Filter HTTP requests and responses */
1751 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001752 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001753 .http_end = cache_store_http_end,
1754
1755 .http_forward_data = cache_store_http_forward_data,
1756
William Lallemand41db4602017-10-30 11:15:51 +01001757};
1758
Christopher Faulet99a17a22018-12-11 09:18:27 +01001759
1760
1761static int
1762parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1763 struct flt_conf *fconf, char **err, void *private)
1764{
1765 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001766 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001767 char *name = NULL;
1768 int pos = *cur_arg;
1769
Christopher Faulet8d2d04c2020-05-18 11:58:16 +02001770 /* Get the cache filter name. <pos> point on "cache" keyword */
1771 if (!*args[pos + 1]) {
Tim Duesterhusb2d43b82020-08-18 22:06:51 +02001772 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet8d2d04c2020-05-18 11:58:16 +02001773 goto error;
1774 }
1775 name = strdup(args[pos + 1]);
1776 if (!name) {
1777 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1778 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001779 }
Christopher Faulet8d2d04c2020-05-18 11:58:16 +02001780 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001781
1782 /* Check if an implicit filter with the same name already exists. If so,
1783 * we remove the implicit filter to use the explicit one. */
1784 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1785 if (f->id != cache_store_flt_id)
1786 continue;
1787
1788 cconf = f->conf;
1789 if (strcmp(name, cconf->c.name)) {
1790 cconf = NULL;
1791 continue;
1792 }
1793
1794 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1795 cconf = NULL;
1796 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1797 px->id, name);
Tim Duesterhusff6603e2020-01-18 01:46:18 +01001798 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001799 }
1800
1801 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1802 LIST_DEL(&f->list);
1803 free(f);
1804 free(name);
1805 break;
1806 }
1807
1808 /* No implicit cache filter found, create configuration for the explicit one */
1809 if (!cconf) {
1810 cconf = calloc(1, sizeof(*cconf));
1811 if (!cconf) {
1812 memprintf(err, "%s: out of memory", args[*cur_arg]);
1813 goto error;
1814 }
1815 cconf->c.name = name;
1816 }
1817
1818 cconf->flags = 0;
1819 fconf->id = cache_store_flt_id;
1820 fconf->conf = cconf;
1821 fconf->ops = &cache_ops;
1822
1823 *cur_arg = pos;
1824 return 0;
1825
1826 error:
1827 free(name);
1828 free(cconf);
1829 return -1;
1830}
1831
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001832static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001833{
1834 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1835 return 1;
1836
1837 return 0;
1838}
1839
1840static int cli_io_handler_show_cache(struct appctx *appctx)
1841{
1842 struct cache* cache = appctx->ctx.cli.p0;
1843 struct stream_interface *si = appctx->owner;
1844
William Lallemand1f49a362017-11-21 20:01:26 +01001845 if (cache == NULL) {
1846 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1847 }
1848
1849 list_for_each_entry_from(cache, &caches, list) {
1850 struct eb32_node *node = NULL;
1851 unsigned int next_key;
1852 struct cache_entry *entry;
1853
William Lallemand1f49a362017-11-21 20:01:26 +01001854 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001855 if (!next_key) {
1856 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1857 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001858 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001859 return 0;
1860 }
1861 }
William Lallemand1f49a362017-11-21 20:01:26 +01001862
1863 appctx->ctx.cli.p0 = cache;
1864
1865 while (1) {
1866
1867 shctx_lock(shctx_ptr(cache));
1868 node = eb32_lookup_ge(&cache->entries, next_key);
1869 if (!node) {
1870 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001871 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001872 break;
1873 }
1874
1875 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001876 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 +01001877
1878 next_key = node->key + 1;
1879 appctx->ctx.cli.i0 = next_key;
1880
1881 shctx_unlock(shctx_ptr(cache));
1882
1883 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001884 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001885 return 0;
1886 }
1887 }
1888
1889 }
1890
1891 return 1;
1892
1893}
1894
Christopher Faulet99a17a22018-12-11 09:18:27 +01001895/* Declare the filter parser for "cache" keyword */
1896static struct flt_kw_list filter_kws = { "CACHE", { }, {
1897 { "cache", parse_cache_flt, NULL },
1898 { NULL, NULL, NULL },
1899 }
1900};
1901
1902INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1903
William Lallemand1f49a362017-11-21 20:01:26 +01001904static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001905 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1906 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001907}};
1908
Willy Tarreau0108d902018-11-25 19:14:37 +01001909INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001910
William Lallemand41db4602017-10-30 11:15:51 +01001911static struct action_kw_list http_res_actions = {
1912 .kw = {
1913 { "cache-store", parse_cache_store },
1914 { NULL, NULL }
1915 }
1916};
1917
Willy Tarreau0108d902018-11-25 19:14:37 +01001918INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1919
William Lallemand41db4602017-10-30 11:15:51 +01001920static struct action_kw_list http_req_actions = {
1921 .kw = {
1922 { "cache-use", parse_cache_use },
1923 { NULL, NULL }
1924 }
1925};
1926
Willy Tarreau0108d902018-11-25 19:14:37 +01001927INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1928
Willy Tarreau2231b632019-03-29 18:26:52 +01001929struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001930 .obj_type = OBJ_TYPE_APPLET,
1931 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001932 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001933 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001934};
1935
Willy Tarreaue6552512018-11-26 11:33:13 +01001936/* config parsers for this section */
1937REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
1938REGISTER_CONFIG_POSTPARSER("cache", cfg_cache_postparser);
William Lallemande4876e22019-08-28 15:22:49 +02001939REGISTER_POST_CHECK(post_check_cache);