blob: a98ca66e62fca9711a744b0e75ef90792004193b [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
Willy Tarreaub2551052020-06-09 09:07:15 +020013#include <import/eb32tree.h>
14#include <import/sha1.h>
15
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020018#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020019#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020020#include <haproxy/cli.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020021#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020022#include <haproxy/filters.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/hash.h>
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +020024#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020025#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020027#include <haproxy/http_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/htx.h>
29#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020030#include <haproxy/proxy.h>
Willy Tarreau334099c2020-06-03 18:38:48 +020031#include <haproxy/shctx.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020032#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020033#include <haproxy/stream_interface.h>
William Lallemand41db4602017-10-30 11:15:51 +010034
Christopher Faulet27d93c32018-12-15 22:32:02 +010035#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010036 * the filter keyword) */
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +020037#define CACHE_FLT_INIT 0x00000002 /* Whether the cache name was freed. */
Christopher Fauletafd819c2018-12-11 08:57:45 +010038
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010039const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010040
Willy Tarreau2231b632019-03-29 18:26:52 +010041extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010042
43struct flt_ops cache_ops;
44
45struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010046 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010047 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010048 unsigned int maxage; /* max-age */
49 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020050 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010051 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010052};
53
Christopher Faulet95220e22018-12-07 17:34:39 +010054/* cache config for filters */
55struct cache_flt_conf {
56 union {
57 struct cache *cache; /* cache used by the filter */
58 char *name; /* cache name used during conf parsing */
59 } c;
60 unsigned int flags; /* CACHE_FLT_F_* */
61};
62
William Lallemand41db4602017-10-30 11:15:51 +010063/*
64 * cache ctx for filters
65 */
66struct cache_st {
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
William Lallemand41db4602017-10-30 11:15:51 +010075 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010076 char hash[20];
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +020077
78 unsigned int etag_length; /* Length of the ETag value (if one was found in the response). */
79 unsigned int etag_offset; /* Offset of the ETag value in the data buffer. */
80
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +020081 time_t last_modified; /* Origin server "Last-Modified" header value converted in
82 * seconds since epoch. If no "Last-Modified"
83 * header is found, use "Date" header value,
84 * otherwise use reception time. This field will
85 * be used in case of an "If-Modified-Since"-based
86 * conditional request. */
87
William Lallemand41db4602017-10-30 11:15:51 +010088 unsigned char data[0];
89};
90
91#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010092#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010093
94static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +020095static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +010096static struct cache *tmp_cache_config = NULL;
97
Willy Tarreau8ceae722018-11-26 11:58:30 +010098DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
99
William Lallemandf528fff2017-11-23 19:43:17 +0100100struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100101{
102 struct eb32_node *node;
103 struct cache_entry *entry;
104
Willy Tarreau8b507582020-02-25 09:35:07 +0100105 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100106 if (!node)
107 return NULL;
108
109 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100110
111 /* if that's not the right node */
112 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
113 return NULL;
114
William Lallemand08727662017-11-21 20:01:27 +0100115 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100116 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100117 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100118 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100119 entry->eb.key = 0;
120 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100121 return NULL;
122
123}
124
125static inline struct shared_context *shctx_ptr(struct cache *cache)
126{
127 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
128}
129
William Lallemand77c11972017-10-31 20:43:01 +0100130static inline struct shared_block *block_ptr(struct cache_entry *entry)
131{
132 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
133}
134
135
136
William Lallemand41db4602017-10-30 11:15:51 +0100137static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100138cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100139{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100140 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100141 return 0;
142}
143
Christopher Faulet95220e22018-12-07 17:34:39 +0100144static void
145cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
146{
147 struct cache_flt_conf *cconf = fconf->conf;
148
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +0200149 if (!(cconf->flags & CACHE_FLT_INIT))
150 free(cconf->c.name);
Christopher Faulet95220e22018-12-07 17:34:39 +0100151 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 Lallemandd1d1e222019-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) {
168 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100169 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100170 }
171
172 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
173 proxy_type_str(px), px->id, (char *)cconf->c.name);
174 return 1;
175
176 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100177 /* Here <cache> points on the cache the filter must use and <cconf>
178 * points on the cache filter configuration. */
179
180 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100181 * enabled and if it is after the cache. When the compression is before
182 * the cache, an error is returned. Also check if the cache filter must
183 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100184 list_for_each_entry(f, &px->filter_configs, list) {
185 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100186 /* The compression filter must be evaluated after the cache. */
187 if (comp) {
188 ha_alert("config: %s '%s': unable to enable the compression filter before "
189 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
190 return 1;
191 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100192 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200193 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100194 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200195 else if (f->id == fcgi_flt_id)
196 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100197 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
198 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200199 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100200 * declaration is required. */
201 ha_alert("config: %s '%s': require an explicit filter declaration "
202 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
203 return 1;
204 }
205
Christopher Fauletafd819c2018-12-11 08:57:45 +0100206 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100207 return 0;
208}
209
210static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100211cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100212{
Christopher Faulet65554e12020-03-06 14:52:06 +0100213 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100214
Christopher Faulet65554e12020-03-06 14:52:06 +0100215 st = pool_alloc_dirty(pool_head_cache_st);
216 if (st == NULL)
217 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100218
Christopher Faulet65554e12020-03-06 14:52:06 +0100219 st->first_block = NULL;
220 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100221
Christopher Faulet65554e12020-03-06 14:52:06 +0100222 /* Register post-analyzer on AN_RES_WAIT_HTTP */
223 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100224 return 1;
225}
226
Christopher Faulet65554e12020-03-06 14:52:06 +0100227static void
228cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100229{
230 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100231 struct cache_flt_conf *cconf = FLT_CONF(filter);
232 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100233 struct shared_context *shctx = shctx_ptr(cache);
234
William Lallemand49dc0482017-11-24 14:33:54 +0100235 /* Everything should be released in the http_end filter, but we need to do it
236 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100237 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100238 shctx_lock(shctx);
239 shctx_row_dec_hot(shctx, st->first_block);
240 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100241 }
242 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100243 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100244 filter->ctx = NULL;
245 }
William Lallemand49dc0482017-11-24 14:33:54 +0100246}
247
Christopher Faulet839791a2019-01-07 16:12:07 +0100248static int
249cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
250 unsigned an_bit)
251{
252 struct http_txn *txn = s->txn;
253 struct http_msg *msg = &txn->rsp;
254 struct cache_st *st = filter->ctx;
255
256 if (an_bit != AN_RES_WAIT_HTTP)
257 goto end;
258
259 /* Here we need to check if any compression filter precedes the cache
260 * filter. This is only possible when the compression is configured in
261 * the frontend while the cache filter is configured on the
262 * backend. This case cannot be detected during HAProxy startup. So in
263 * such cases, the cache is disabled.
264 */
265 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
266 pool_free(pool_head_cache_st, st);
267 filter->ctx = NULL;
268 }
269
270 end:
271 return 1;
272}
William Lallemand49dc0482017-11-24 14:33:54 +0100273
274static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100275cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
276{
277 struct cache_st *st = filter->ctx;
278
William Lallemand4da3f8a2017-10-31 14:33:34 +0100279 if (!(msg->chn->flags & CF_ISRESP) || !st)
280 return 1;
281
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200282 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100283 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100284 return 1;
285}
286
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200287static inline void disable_cache_entry(struct cache_st *st,
288 struct filter *filter, struct shared_context *shctx)
289{
290 struct cache_entry *object;
291
292 object = (struct cache_entry *)st->first_block->data;
293 filter->ctx = NULL; /* disable cache */
294 shctx_lock(shctx);
295 shctx_row_dec_hot(shctx, st->first_block);
296 object->eb.key = 0;
297 shctx_unlock(shctx);
298 pool_free(pool_head_cache_st, st);
299}
300
William Lallemand4da3f8a2017-10-31 14:33:34 +0100301static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100302cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
303 unsigned int offset, unsigned int len)
304{
Christopher Faulet95220e22018-12-07 17:34:39 +0100305 struct cache_flt_conf *cconf = FLT_CONF(filter);
306 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100307 struct cache_st *st = filter->ctx;
308 struct htx *htx = htxbuf(&msg->chn->buf);
309 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200310 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100311 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200312 unsigned int orig_len, to_forward;
313 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100314
315 if (!len)
316 return len;
317
318 if (!st->first_block) {
319 unregister_data_filter(s, msg->chn, filter);
320 return len;
321 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100322
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200323 chunk_reset(&trash);
324 orig_len = len;
325 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100326
327 htxret = htx_find_offset(htx, offset);
328 blk = htxret.blk;
329 offset = htxret.ret;
330 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100331 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200332 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100333 struct ist v;
334
335 switch (type) {
336 case HTX_BLK_UNUSED:
337 break;
338
339 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100340 v = htx_get_blk_value(htx, blk);
341 v.ptr += offset;
342 v.len -= offset;
343 if (v.len > len)
344 v.len = len;
345
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200346 info = (type << 28) + v.len;
347 chunk_memcat(&trash, (char *)&info, sizeof(info));
348 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100349 to_forward += v.len;
350 len -= v.len;
351 break;
352
353 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200354 /* Here offset must always be 0 because only
355 * DATA blocks can be partially transferred. */
356 if (offset)
357 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100358 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200359 goto end;
360
361 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
362 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100363 to_forward += sz;
364 len -= sz;
365 break;
366 }
367
368 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100369 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200370
371 end:
372 shctx_lock(shctx);
373 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
374 if (!fb) {
375 shctx_unlock(shctx);
376 goto no_cache;
377 }
378 shctx_unlock(shctx);
379
380 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
381 (unsigned char *)b_head(&trash), b_data(&trash));
382 if (ret < 0)
383 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100384
385 return to_forward;
386
387 no_cache:
388 disable_cache_entry(st, filter, shctx);
389 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200390 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100391}
392
393static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100394cache_store_http_end(struct stream *s, struct filter *filter,
395 struct http_msg *msg)
396{
397 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100398 struct cache_flt_conf *cconf = FLT_CONF(filter);
399 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100400 struct shared_context *shctx = shctx_ptr(cache);
401 struct cache_entry *object;
402
403 if (!(msg->chn->flags & CF_ISRESP))
404 return 1;
405
406 if (st && st->first_block) {
407
408 object = (struct cache_entry *)st->first_block->data;
409
410 /* does not need to test if the insertion worked, if it
411 * doesn't, the blocks will be reused anyway */
412
413 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100414 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
415 object->eb.key = 0;
416 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100417 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100418 shctx_row_dec_hot(shctx, st->first_block);
419 shctx_unlock(shctx);
420
421 }
422 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100423 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100424 filter->ctx = NULL;
425 }
426
427 return 1;
428}
429
430 /*
431 * This intends to be used when checking HTTP headers for some
432 * word=value directive. Return a pointer to the first character of value, if
433 * the word was not found or if there wasn't any value assigned ot it return NULL
434 */
435char *directive_value(const char *sample, int slen, const char *word, int wlen)
436{
437 int st = 0;
438
439 if (slen < wlen)
440 return 0;
441
442 while (wlen) {
443 char c = *sample ^ *word;
444 if (c && c != ('A' ^ 'a'))
445 return NULL;
446 sample++;
447 word++;
448 slen--;
449 wlen--;
450 }
451
452 while (slen) {
453 if (st == 0) {
454 if (*sample != '=')
455 return NULL;
456 sample++;
457 slen--;
458 st = 1;
459 continue;
460 } else {
461 return (char *)sample;
462 }
463 }
464
465 return NULL;
466}
467
468/*
469 * Return the maxage in seconds of an HTTP response.
470 * Compute the maxage using either:
471 * - the assigned max-age of the cache
472 * - the s-maxage directive
473 * - the max-age directive
474 * - (Expires - Data) headers
475 * - the default-max-age of the cache
476 *
477 */
William Lallemand49b44532017-11-24 18:53:43 +0100478int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100479{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200480 struct htx *htx = htxbuf(&s->res.buf);
481 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100482 int smaxage = -1;
483 int maxage = -1;
484
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200485 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
486 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100487
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200488 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
489 if (value) {
490 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100491
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200492 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
493 chunk_strncat(chk, "", 1);
494 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100495 }
496
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200497 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
498 if (value) {
499 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200500
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200501 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
502 chunk_strncat(chk, "", 1);
503 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100504 }
505 }
506
507 /* TODO: Expires - Data */
508
509
510 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100511 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100512
513 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100514 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100515
William Lallemand49b44532017-11-24 18:53:43 +0100516 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100517
518}
519
520
William Lallemanda400a3a2017-11-20 19:13:12 +0100521static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
522{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200523 struct cache_entry *object = (struct cache_entry *)block->data;
524
525 if (first == block && object->eb.key)
526 eb32_delete(&object->eb);
527 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100528}
529
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200530
531/* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the
532 * date value should be compared to a date determined by in a previous response (for
533 * the same entity). This date could either be the "Last-Modified" value, or the "Date"
534 * value of the response's reception time (by decreasing order of priority). */
535static time_t get_last_modified_time(struct htx *htx)
536{
537 time_t last_modified = 0;
538 struct http_hdr_ctx ctx = { .blk = NULL };
539 struct tm tm = {};
540
541 if (http_find_header(htx, ist("last-modified"), &ctx, 1)) {
542 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
543 last_modified = my_timegm(&tm);
544 }
545 }
546
547 if (!last_modified) {
548 ctx.blk = NULL;
549 if (http_find_header(htx, ist("date"), &ctx, 1)) {
550 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
551 last_modified = my_timegm(&tm);
552 }
553 }
554 }
555
556 /* Fallback on the current time if no "Last-Modified" or "Date" header
557 * was found. */
558 if (!last_modified)
559 last_modified = now.tv_sec;
560
561 return last_modified;
562}
563
William Lallemand41db4602017-10-30 11:15:51 +0100564/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500565 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100566 * register a filter to store the data
567 */
568enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200569 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100570{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200571 unsigned int age;
572 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100573 struct http_txn *txn = s->txn;
574 struct http_msg *msg = &txn->rsp;
575 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100576 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100577 struct cache_flt_conf *cconf = rule->arg.act.p[0];
578 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100579 struct cache_st *cache_ctx = NULL;
580 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100581 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200582 struct htx *htx;
583 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200584 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200585 int32_t pos;
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200586 unsigned int etag_length = 0;
587 unsigned int etag_offset = 0;
588 struct ist header_name = IST_NULL;
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200589 time_t last_modified = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100590
William Lallemand4da3f8a2017-10-31 14:33:34 +0100591 /* Don't cache if the response came from a cache */
592 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
593 s->target == &http_cache_applet.obj_type) {
594 goto out;
595 }
596
597 /* cache only HTTP/1.1 */
598 if (!(txn->req.flags & HTTP_MSGF_VER_11))
599 goto out;
600
Willy Tarreau6905d182019-10-01 17:59:17 +0200601 /* cache only GET method */
602 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100603 goto out;
604
Willy Tarreauc9036c02019-01-11 19:38:25 +0100605 /* cache key was not computed */
606 if (!key)
607 goto out;
608
William Lallemand4da3f8a2017-10-31 14:33:34 +0100609 /* cache only 200 status code */
610 if (txn->status != 200)
611 goto out;
612
Christopher Faulet839791a2019-01-07 16:12:07 +0100613 /* Find the corresponding filter instance for the current stream */
614 list_for_each_entry(filter, &s->strm_flt.filters, list) {
615 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
616 /* No filter ctx, don't cache anything */
617 if (!filter->ctx)
618 goto out;
619 cache_ctx = filter->ctx;
620 break;
621 }
622 }
623
624 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200625 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100626
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200627 /* Do not cache too big objects. */
628 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
629 htx->data + htx->extra > shctx->max_obj_size)
630 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100631
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200632 /* Does not manage Vary at the moment. We will need a secondary key later for that */
633 ctx.blk = NULL;
634 if (http_find_header(htx, ist("Vary"), &ctx, 0))
635 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100636
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200637 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100638
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200639 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
640 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100641
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200642 age = 0;
643 ctx.blk = NULL;
644 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
645 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
646 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
647 hdr_age = CACHE_ENTRY_MAX_AGE;
648 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100649 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200650 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100651 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100652
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200653 /* Build a last-modified time that will be stored in the cache_entry and
654 * compared to a future If-Modified-Since client header. */
655 last_modified = get_last_modified_time(htx);
656
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200657 chunk_reset(&trash);
658 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
659 struct htx_blk *blk = htx_get_blk(htx, pos);
660 enum htx_blk_type type = htx_get_blk_type(blk);
661 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100662
Christopher Fauletb0667472019-09-03 22:22:12 +0200663 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200664 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
665 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200666
667 /* Look for optional ETag header.
668 * We need to store the offset of the ETag value in order for
669 * future conditional requests to be able to perform ETag
670 * comparisons. */
671 if (type == HTX_BLK_HDR) {
672 header_name = htx_get_blk_name(htx, blk);
673 if (isteq(header_name, ist("etag"))) {
674 etag_length = sz - istlen(header_name);
675 etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name);
676 }
677 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200678 if (type == HTX_BLK_EOH)
679 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200680 }
681
Christopher Fauletb0667472019-09-03 22:22:12 +0200682 /* Do not cache objects if the headers are too big. */
683 if (hdrs_len > htx->size - global.tune.maxrewrite)
684 goto out;
685
William Lallemand4da3f8a2017-10-31 14:33:34 +0100686 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200687 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100688 if (!first) {
689 shctx_unlock(shctx);
690 goto out;
691 }
692 shctx_unlock(shctx);
693
Willy Tarreau1093a452018-04-06 19:02:25 +0200694 /* the received memory is not initialized, we need at least to mark
695 * the object as not indexed yet.
696 */
697 object = (struct cache_entry *)first->data;
698 object->eb.node.leaf_p = NULL;
699 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200700 object->age = age;
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200701 object->last_modified = last_modified;
Willy Tarreau1093a452018-04-06 19:02:25 +0200702
William Lallemand4da3f8a2017-10-31 14:33:34 +0100703 /* reserve space for the cache_entry structure */
704 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200705 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100706 /* cache the headers in a http action because it allows to chose what
707 * to cache, for example you might want to cache a response before
708 * modifying some HTTP headers, or on the contrary after modifying
709 * those headers.
710 */
711
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200712 /* Write the ETag information in the cache_entry if needed. */
713 object->etag_length = etag_length;
714 object->etag_offset = etag_offset;
715
William Lallemand4da3f8a2017-10-31 14:33:34 +0100716 /* does not need to be locked because it's in the "hot" list,
717 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200718 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
719 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100720
721 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100722 if (cache_ctx) {
723 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100724
Willy Tarreauc9036c02019-01-11 19:38:25 +0100725 object->eb.key = key;
726
Christopher Faulet839791a2019-01-07 16:12:07 +0100727 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
728 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100729
Christopher Faulet839791a2019-01-07 16:12:07 +0100730 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100731
Christopher Faulet839791a2019-01-07 16:12:07 +0100732 old = entry_exist(cconf->c.cache, txn->cache_hash);
733 if (old) {
734 eb32_delete(&old->eb);
735 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100736 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100737 shctx_unlock(shctx);
738
739 /* store latest value and expiration time */
740 object->latest_validation = now.tv_sec;
741 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
742 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100743 }
744
745out:
746 /* if does not cache */
747 if (first) {
748 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100749 first->len = 0;
750 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100751 shctx_row_dec_hot(shctx, first);
752 shctx_unlock(shctx);
753 }
754
William Lallemand41db4602017-10-30 11:15:51 +0100755 return ACT_RET_CONT;
756}
757
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100758#define HTX_CACHE_INIT 0 /* Initial state. */
759#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
760#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200761#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
762#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100763
William Lallemandecb73b12017-11-24 14:33:55 +0100764static void http_cache_applet_release(struct appctx *appctx)
765{
Christopher Faulet95220e22018-12-07 17:34:39 +0100766 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100767 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100768 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100769 struct shared_block *first = block_ptr(cache_ptr);
770
771 shctx_lock(shctx_ptr(cache));
772 shctx_row_dec_hot(shctx_ptr(cache), first);
773 shctx_unlock(shctx_ptr(cache));
774}
775
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200776
777static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
778 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100779{
Christopher Faulet95220e22018-12-07 17:34:39 +0100780 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
781 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200782 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200783 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200784 unsigned int max, total;
785 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100786
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200787 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
788 if (!max)
789 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200790 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200791 ? (info & 0xff) + ((info >> 8) & 0xfffff)
792 : info & 0xfffffff);
793 if (blksz > max)
794 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100795
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200796 blk = htx_add_blk(htx, type, blksz);
797 if (!blk)
798 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100799
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200800 blk->info = info;
801 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200802 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200803 while (blksz) {
804 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200805 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200806 offset += max;
807 blksz -= max;
808 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200809 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200810 if (blksz || offset == shctx->block_size) {
811 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
812 offset = 0;
813 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100814 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200815 appctx->ctx.cache.offset = offset;
816 appctx->ctx.cache.next = shblk;
817 appctx->ctx.cache.sent += total;
818 return total;
819}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100820
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200821static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
822 uint32_t info, struct shared_block *shblk, unsigned int offset)
823{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100824
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200825 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
826 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
827 unsigned int max, total, rem_data;
828 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100829
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200830 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
831 if (!max)
832 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100833
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200834 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200835 if (appctx->ctx.cache.rem_data) {
836 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200837 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200838 }
839 else {
840 blksz = (info & 0xfffffff);
841 total = 4;
842 }
843 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200844 rem_data = blksz - max;
845 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100846 }
847
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200848 while (blksz) {
849 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100850
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200851 max = MIN(blksz, shctx->block_size - offset);
852 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
853 offset += sz;
854 blksz -= sz;
855 total += sz;
856 if (sz < max)
857 break;
858 if (blksz || offset == shctx->block_size) {
859 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
860 offset = 0;
861 }
862 }
863
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200864 appctx->ctx.cache.offset = offset;
865 appctx->ctx.cache.next = shblk;
866 appctx->ctx.cache.sent += total;
867 appctx->ctx.cache.rem_data = rem_data + blksz;
868 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100869}
870
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200871static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
872 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100873{
Christopher Faulet95220e22018-12-07 17:34:39 +0100874 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
875 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200876 struct shared_block *shblk;
877 unsigned int offset, sz;
878 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100879
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200880 while (len) {
881 enum htx_blk_type type;
882 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100883
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200884 shblk = appctx->ctx.cache.next;
885 offset = appctx->ctx.cache.offset;
886 if (appctx->ctx.cache.rem_data) {
887 type = HTX_BLK_DATA;
888 info = 0;
889 goto add_data_blk;
890 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100891
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500892 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200893 sz = MIN(4, shctx->block_size - offset);
894 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
895 offset += sz;
896 if (sz < 4) {
897 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
898 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
899 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100900 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200901
902 /* Get payload of the next HTX block and insert it. */
903 type = (info >> 28);
904 if (type != HTX_BLK_DATA)
905 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
906 else {
907 add_data_blk:
908 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100909 }
910
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200911 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100912 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200913 total += ret;
914 len -= ret;
915
916 if (appctx->ctx.cache.rem_data || type == mark)
917 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100918 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100919
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100920 return total;
921}
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200922
923static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
924{
925 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
926 unsigned int age;
927 char *end;
928
929 chunk_reset(&trash);
930 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
931 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
932 age = CACHE_ENTRY_MAX_AGE;
933 end = ultoa_o(age, b_head(&trash), b_size(&trash));
934 b_set_data(&trash, end - b_head(&trash));
935 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
936 return 0;
937 return 1;
938}
939
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200940static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100941{
942 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
943 struct shared_block *first = block_ptr(cache_ptr);
944 struct stream_interface *si = appctx->owner;
945 struct channel *req = si_oc(si);
946 struct channel *res = si_ic(si);
947 struct htx *req_htx, *res_htx;
948 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200949 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100950 size_t ret, total = 0;
951
952 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200953 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100954
955 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
956 goto out;
957
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500958 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100959 if (!b_size(&res->buf)) {
960 si_rx_room_blk(si);
961 goto out;
962 }
963
Willy Tarreauefef3232018-12-16 00:37:45 +0100964 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100965 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100966
967 if (appctx->st0 == HTX_CACHE_INIT) {
968 appctx->ctx.cache.next = block_ptr(cache_ptr);
969 appctx->ctx.cache.offset = sizeof(*cache_ptr);
970 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200971 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100972 appctx->st0 = HTX_CACHE_HEADER;
973 }
974
975 if (appctx->st0 == HTX_CACHE_HEADER) {
976 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200977 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
978 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
979 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
980 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100981 goto error;
982
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +0200983 /* In case of a conditional request, we might want to send a
984 * "304 Not Modified" response instead of the stored data. */
Tim Duesterhuse0142342020-10-22 21:15:06 +0200985 if (appctx->ctx.cache.send_notmodified) {
986 if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) {
987 /* If replacing the status code fails we need to send the full response. */
988 appctx->ctx.cache.send_notmodified = 0;
989 }
990 }
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +0200991
992 /* Skip response body for HEAD requests or in case of "304 Not
993 * Modified" response. */
994 if (si_strm(si)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified)
Christopher Fauletf0dd0372019-02-25 11:08:34 +0100995 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100996 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200997 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100998 }
999
1000 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001001 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1002 if (len) {
1003 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
1004 if (ret < len) {
1005 si_rx_room_blk(si);
1006 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001007 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001008 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001009 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001010 }
1011
1012 if (appctx->st0 == HTX_CACHE_EOM) {
Christopher Faulet810df062020-07-22 16:20:34 +02001013 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001014 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1015 si_rx_room_blk(si);
1016 goto out;
1017 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001018 appctx->st0 = HTX_CACHE_END;
1019 }
1020
1021 end:
Christopher Fauletadb36312019-02-25 11:40:49 +01001022 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001023 res->flags |= CF_READ_NULL;
1024 si_shutr(si);
1025 }
1026
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001027 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001028 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +01001029 if (total)
1030 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001031 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +01001032
1033 /* eat the whole request */
1034 if (co_data(req)) {
1035 req_htx = htx_from_buf(&req->buf);
1036 co_htx_skip(req, req_htx, co_data(req));
1037 htx_to_buf(req_htx, &req->buf);
1038 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001039 return;
1040
1041 error:
1042 /* Sent and HTTP error 500 */
1043 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +02001044 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001045 res->buf.data = b_data(errmsg);
1046 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1047 res_htx = htx_from_buf(&res->buf);
1048
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001049 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001050 appctx->st0 = HTX_CACHE_END;
1051 goto end;
1052}
1053
1054
Christopher Faulet95220e22018-12-07 17:34:39 +01001055static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001056{
1057 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001058 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001059
Christopher Faulet95220e22018-12-07 17:34:39 +01001060 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001061 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001062 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001063 }
1064
1065 /* check if a cache filter was already registered with this cache
1066 * name, if that's the case, must use it. */
1067 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001068 if (fconf->id == cache_store_flt_id) {
1069 cconf = fconf->conf;
1070 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1071 rule->arg.act.p[0] = cconf;
1072 return 1;
1073 }
William Lallemand41db4602017-10-30 11:15:51 +01001074 }
1075 }
1076
Christopher Faulet95220e22018-12-07 17:34:39 +01001077 /* Create the filter cache config */
1078 cconf = calloc(1, sizeof(*cconf));
1079 if (!cconf) {
1080 memprintf(err, "out of memory\n");
1081 goto err;
1082 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001083 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001084 cconf->c.name = strdup(name);
1085 if (!cconf->c.name) {
1086 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001087 goto err;
1088 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001089
William Lallemand41db4602017-10-30 11:15:51 +01001090 /* register a filter to fill the cache buffer */
1091 fconf = calloc(1, sizeof(*fconf));
1092 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001093 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001094 goto err;
1095 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001096 fconf->id = cache_store_flt_id;
1097 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001098 fconf->ops = &cache_ops;
1099 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1100
Christopher Faulet95220e22018-12-07 17:34:39 +01001101 rule->arg.act.p[0] = cconf;
1102 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001103
Christopher Faulet95220e22018-12-07 17:34:39 +01001104 err:
1105 free(cconf);
1106 return 0;
1107}
1108
1109enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1110 struct act_rule *rule, char **err)
1111{
1112 rule->action = ACT_CUSTOM;
1113 rule->action_ptr = http_action_store_cache;
1114
1115 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1116 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001117
Christopher Faulet95220e22018-12-07 17:34:39 +01001118 (*orig_arg)++;
1119 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001120}
1121
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001122/* This produces a sha1 hash of the concatenation of the HTTP method,
1123 * the first occurrence of the Host header followed by the path component
1124 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001125int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001126{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001127 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001128 struct htx *htx = htxbuf(&s->req.buf);
1129 struct htx_sl *sl;
1130 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001131 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001132 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001133 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001134
William Lallemandf528fff2017-11-23 19:43:17 +01001135 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001136 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001137
1138 switch (txn->meth) {
1139 case HTTP_METH_HEAD:
1140 case HTTP_METH_GET:
1141 chunk_memcat(trash, "GET", 3);
1142 break;
1143 default:
1144 return 0;
1145 }
1146
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001147 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001148 uri = htx_sl_req_uri(sl); // whole uri
1149 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001150 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001151
1152 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1153 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1154 * URIs are almost always sent in absolute form with their scheme. In
1155 * this case, the scheme is almost always "https". In order to support
1156 * sharing of cache objects between H1 and H2, we'll hash the absolute
1157 * URI whenever known, or prepend "https://" + the Host header for
1158 * relative URIs. The difference will only appear on absolute HTTP/1
1159 * requests sent to an origin server, which practically is never met in
1160 * the real world so we don't care about the ability to share the same
1161 * key here.URIs are normalized from the absolute URI to an origin form as
1162 * well.
1163 */
1164 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001165 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001166 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1167 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001168 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001169 }
1170
1171 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001172
1173 /* hash everything */
1174 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001175 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001176 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1177
1178 return 1;
1179}
1180
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001181/* Looks for "If-None-Match" headers in the request and compares their value
1182 * with the one that might have been stored in the cache_entry. If any of them
1183 * matches, a "304 Not Modified" response should be sent instead of the cached
1184 * data.
1185 * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001186 * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2).
1187 *
1188 * If no "If-None-Match" header was found, look for an "If-Modified-Since"
1189 * header and compare its value (date) to the one stored in the cache_entry.
1190 * If the request's date is later than the cached one, we also send a
1191 * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2).
1192 *
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001193 * Returns 1 if "304 Not Modified" should be sent, 0 otherwise.
1194 */
1195static int should_send_notmodified_response(struct cache *cache, struct htx *htx,
1196 struct cache_entry *entry)
1197{
1198 int retval = 0;
1199
1200 struct http_hdr_ctx ctx = { .blk = NULL };
1201 struct ist cache_entry_etag = IST_NULL;
1202 struct buffer *etag_buffer = NULL;
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001203 int if_none_match_found = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001204
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001205 struct tm tm = {};
1206 time_t if_modified_since = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001207
1208 /* If we find a "If-None-Match" header in the request, rebuild the
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001209 * cache_entry's ETag in order to perform comparisons.
1210 * There could be multiple "if-none-match" header lines. */
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001211 while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) {
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001212 if_none_match_found = 1;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001213
1214 /* A '*' matches everything. */
1215 if (isteq(ctx.value, ist("*")) != 0) {
1216 retval = 1;
1217 break;
1218 }
1219
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001220 /* No need to rebuild an etag if none was stored in the cache. */
1221 if (entry->etag_length == 0)
1222 break;
1223
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001224 /* Rebuild the stored ETag. */
1225 if (etag_buffer == NULL) {
1226 etag_buffer = get_trash_chunk();
1227
1228 if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry),
1229 (unsigned char*)b_orig(etag_buffer),
1230 entry->etag_offset, entry->etag_length) == 0) {
1231 cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length);
1232 } else {
1233 /* We could not rebuild the ETag in one go, we
1234 * won't send a "304 Not Modified" response. */
1235 break;
1236 }
1237 }
1238
1239 if (http_compare_etags(cache_entry_etag, ctx.value) == 1) {
1240 retval = 1;
1241 break;
1242 }
1243 }
1244
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001245 /* If the request did not contain an "If-None-Match" header, we look for
1246 * an "If-Modified-Since" header (see RFC 7232#3.3). */
1247 if (retval == 0 && if_none_match_found == 0) {
1248 ctx.blk = NULL;
1249 if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) {
1250 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
1251 if_modified_since = my_timegm(&tm);
1252
1253 /* We send a "304 Not Modified" response if the
1254 * entry's last modified date is earlier than
1255 * the one found in the "If-Modified-Since"
1256 * header. */
1257 retval = (entry->last_modified <= if_modified_since);
1258 }
1259 }
1260 }
1261
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001262 return retval;
1263}
1264
William Lallemand41db4602017-10-30 11:15:51 +01001265enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1266 struct session *sess, struct stream *s, int flags)
1267{
William Lallemand77c11972017-10-31 20:43:01 +01001268
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001269 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001270 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001271 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1272 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001273
Willy Tarreau6905d182019-10-01 17:59:17 +02001274 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1275 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001276 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001277 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001278 txn->flags |= TX_CACHE_IGNORE;
1279
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001280 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001281
Willy Tarreau504455c2017-12-22 17:47:35 +01001282 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1283 return ACT_RET_CONT;
1284
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001285 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001286 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001287
Willy Tarreau504455c2017-12-22 17:47:35 +01001288 if (s->txn->flags & TX_CACHE_IGNORE)
1289 return ACT_RET_CONT;
1290
Willy Tarreaua1214a52018-12-14 14:00:25 +01001291 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001292 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001293 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001294 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001295
William Lallemanda400a3a2017-11-20 19:13:12 +01001296 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001297 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001298 if (res) {
1299 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001300 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1301 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001302 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001303 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001304 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001305 appctx->rule = rule;
1306 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001307 appctx->ctx.cache.next = NULL;
1308 appctx->ctx.cache.sent = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001309 appctx->ctx.cache.send_notmodified =
1310 should_send_notmodified_response(cache, htxbuf(&s->req.buf), res);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001311
1312 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001313 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001314 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001315 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001316 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001317 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001318 shctx_lock(shctx_ptr(cache));
1319 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1320 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001321 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001322 }
1323 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001324 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001325 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001326}
1327
1328
1329enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1330 struct act_rule *rule, char **err)
1331{
William Lallemand41db4602017-10-30 11:15:51 +01001332 rule->action = ACT_CUSTOM;
1333 rule->action_ptr = http_action_req_cache_use;
1334
Christopher Faulet95220e22018-12-07 17:34:39 +01001335 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001336 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001337
1338 (*orig_arg)++;
1339 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001340}
1341
1342int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1343{
1344 int err_code = 0;
1345
1346 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1347
1348 if (!*args[1]) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001349 ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001350 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001351 err_code |= ERR_ALERT | ERR_ABORT;
1352 goto out;
1353 }
1354
1355 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1356 err_code |= ERR_ABORT;
1357 goto out;
1358 }
1359
1360 if (tmp_cache_config == NULL) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001361 struct cache *cache_config;
1362
William Lallemand41db4602017-10-30 11:15:51 +01001363 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1364 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001365 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001366 err_code |= ERR_ALERT | ERR_ABORT;
1367 goto out;
1368 }
1369
1370 strlcpy2(tmp_cache_config->id, args[1], 33);
1371 if (strlen(args[1]) > 32) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001372 ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001373 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001374 err_code |= ERR_WARN;
1375 }
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001376
1377 list_for_each_entry(cache_config, &caches_config, list) {
1378 if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
1379 ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
1380 file, linenum, tmp_cache_config->id);
1381 err_code |= ERR_ALERT | ERR_ABORT;
1382 goto out;
1383 }
1384 }
1385
William Lallemand49b44532017-11-24 18:53:43 +01001386 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001387 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001388 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001389 }
1390 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001391 unsigned long int maxsize;
1392 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001393
1394 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1395 err_code |= ERR_ABORT;
1396 goto out;
1397 }
1398
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001399 maxsize = strtoul(args[1], &err, 10);
1400 if (err == args[1] || *err != '\0') {
1401 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1402 file, linenum, args[1]);
1403 err_code |= ERR_ABORT;
1404 goto out;
1405 }
1406
1407 if (maxsize > (UINT_MAX >> 20)) {
1408 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1409 file, linenum, args[1], UINT_MAX >> 20);
1410 err_code |= ERR_ABORT;
1411 goto out;
1412 }
1413
William Lallemand41db4602017-10-30 11:15:51 +01001414 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001415 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001416 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001417 } else if (strcmp(args[0], "max-age") == 0) {
1418 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1419 err_code |= ERR_ABORT;
1420 goto out;
1421 }
1422
1423 if (!*args[1]) {
1424 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1425 file, linenum, args[0]);
1426 err_code |= ERR_WARN;
1427 }
1428
1429 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001430 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001431 unsigned int maxobjsz;
1432 char *err;
1433
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001434 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1435 err_code |= ERR_ABORT;
1436 goto out;
1437 }
1438
1439 if (!*args[1]) {
1440 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1441 file, linenum, args[0]);
1442 err_code |= ERR_WARN;
1443 }
1444
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001445 maxobjsz = strtoul(args[1], &err, 10);
1446 if (err == args[1] || *err != '\0') {
1447 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1448 file, linenum, args[1]);
1449 err_code |= ERR_ABORT;
1450 goto out;
1451 }
1452 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001453 }
1454 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001455 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001456 err_code |= ERR_ALERT | ERR_FATAL;
1457 goto out;
1458 }
1459out:
1460 return err_code;
1461}
1462
1463/* once the cache section is parsed */
1464
1465int cfg_post_parse_section_cache()
1466{
William Lallemand41db4602017-10-30 11:15:51 +01001467 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001468
1469 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001470
1471 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001472 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001473 err_code |= ERR_FATAL | ERR_ALERT;
1474 goto out;
1475 }
1476
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001477 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001478 /* Default max. file size is a 256th of the cache size. */
1479 tmp_cache_config->maxobjsz =
1480 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001481 }
1482 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1483 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1484 err_code |= ERR_FATAL | ERR_ALERT;
1485 goto out;
1486 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001487
William Lallemandd1d1e222019-08-28 15:22:49 +02001488 /* add to the list of cache to init and reinit tmp_cache_config
1489 * for next cache section, if any.
1490 */
1491 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1492 tmp_cache_config = NULL;
1493 return err_code;
1494 }
1495out:
1496 free(tmp_cache_config);
1497 tmp_cache_config = NULL;
1498 return err_code;
1499
1500}
1501
1502int post_check_cache()
1503{
1504 struct proxy *px;
1505 struct cache *back, *cache_config, *cache;
1506 struct shared_context *shctx;
1507 int ret_shctx;
1508 int err_code = 0;
1509
1510 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1511
1512 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1513 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001514
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001515 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001516 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001517 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001518 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001519 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001520
1521 err_code |= ERR_FATAL | ERR_ALERT;
1522 goto out;
1523 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001524 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001525 /* the cache structure is stored in the shctx and added to the
1526 * caches list, we can remove the entry from the caches_config
1527 * list */
1528 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001529 cache = (struct cache *)shctx->data;
1530 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001531 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001532 LIST_DEL(&cache_config->list);
1533 free(cache_config);
1534
1535 /* Find all references for this cache in the existing filters
1536 * (over all proxies) and reference it in matching filters.
1537 */
1538 for (px = proxies_list; px; px = px->next) {
1539 struct flt_conf *fconf;
1540 struct cache_flt_conf *cconf;
1541
1542 list_for_each_entry(fconf, &px->filter_configs, list) {
1543 if (fconf->id != cache_store_flt_id)
1544 continue;
1545
1546 cconf = fconf->conf;
1547 if (!strcmp(cache->id, cconf->c.name)) {
1548 free(cconf->c.name);
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +02001549 cconf->flags |= CACHE_FLT_INIT;
William Lallemandd1d1e222019-08-28 15:22:49 +02001550 cconf->c.cache = cache;
1551 break;
1552 }
1553 }
1554 }
William Lallemand41db4602017-10-30 11:15:51 +01001555 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001556
William Lallemand41db4602017-10-30 11:15:51 +01001557out:
William Lallemand41db4602017-10-30 11:15:51 +01001558 return err_code;
1559
William Lallemand41db4602017-10-30 11:15:51 +01001560}
1561
William Lallemand41db4602017-10-30 11:15:51 +01001562struct flt_ops cache_ops = {
1563 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001564 .check = cache_store_check,
1565 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001566
Christopher Faulet65554e12020-03-06 14:52:06 +01001567 /* Handle stream init/deinit */
1568 .attach = cache_store_strm_init,
1569 .detach = cache_store_strm_deinit,
1570
William Lallemand4da3f8a2017-10-31 14:33:34 +01001571 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001572 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001573
1574 /* Filter HTTP requests and responses */
1575 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001576 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001577 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001578};
1579
Christopher Faulet99a17a22018-12-11 09:18:27 +01001580
1581
1582static int
1583parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1584 struct flt_conf *fconf, char **err, void *private)
1585{
1586 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001587 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001588 char *name = NULL;
1589 int pos = *cur_arg;
1590
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001591 /* Get the cache filter name. <pos> point on "cache" keyword */
1592 if (!*args[pos + 1]) {
Tim Duesterhusea969f62020-08-18 22:06:51 +02001593 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001594 goto error;
1595 }
1596 name = strdup(args[pos + 1]);
1597 if (!name) {
1598 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1599 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001600 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001601 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001602
1603 /* Check if an implicit filter with the same name already exists. If so,
1604 * we remove the implicit filter to use the explicit one. */
1605 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1606 if (f->id != cache_store_flt_id)
1607 continue;
1608
1609 cconf = f->conf;
1610 if (strcmp(name, cconf->c.name)) {
1611 cconf = NULL;
1612 continue;
1613 }
1614
1615 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1616 cconf = NULL;
1617 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1618 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01001619 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001620 }
1621
1622 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1623 LIST_DEL(&f->list);
1624 free(f);
1625 free(name);
1626 break;
1627 }
1628
1629 /* No implicit cache filter found, create configuration for the explicit one */
1630 if (!cconf) {
1631 cconf = calloc(1, sizeof(*cconf));
1632 if (!cconf) {
1633 memprintf(err, "%s: out of memory", args[*cur_arg]);
1634 goto error;
1635 }
1636 cconf->c.name = name;
1637 }
1638
1639 cconf->flags = 0;
1640 fconf->id = cache_store_flt_id;
1641 fconf->conf = cconf;
1642 fconf->ops = &cache_ops;
1643
1644 *cur_arg = pos;
1645 return 0;
1646
1647 error:
1648 free(name);
1649 free(cconf);
1650 return -1;
1651}
1652
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001653static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001654{
1655 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1656 return 1;
1657
1658 return 0;
1659}
1660
1661static int cli_io_handler_show_cache(struct appctx *appctx)
1662{
1663 struct cache* cache = appctx->ctx.cli.p0;
1664 struct stream_interface *si = appctx->owner;
1665
William Lallemand1f49a362017-11-21 20:01:26 +01001666 if (cache == NULL) {
1667 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1668 }
1669
1670 list_for_each_entry_from(cache, &caches, list) {
1671 struct eb32_node *node = NULL;
1672 unsigned int next_key;
1673 struct cache_entry *entry;
1674
William Lallemand1f49a362017-11-21 20:01:26 +01001675 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001676 if (!next_key) {
1677 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1678 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001679 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001680 return 0;
1681 }
1682 }
William Lallemand1f49a362017-11-21 20:01:26 +01001683
1684 appctx->ctx.cli.p0 = cache;
1685
1686 while (1) {
1687
1688 shctx_lock(shctx_ptr(cache));
1689 node = eb32_lookup_ge(&cache->entries, next_key);
1690 if (!node) {
1691 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001692 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001693 break;
1694 }
1695
1696 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01001697 chunk_printf(&trash, "%p hash:%u size:%u (%u blocks), refcount:%u, expire:%d\n", entry, read_u32(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 +01001698
1699 next_key = node->key + 1;
1700 appctx->ctx.cli.i0 = next_key;
1701
1702 shctx_unlock(shctx_ptr(cache));
1703
1704 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001705 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001706 return 0;
1707 }
1708 }
1709
1710 }
1711
1712 return 1;
1713
1714}
1715
Christopher Faulet99a17a22018-12-11 09:18:27 +01001716/* Declare the filter parser for "cache" keyword */
1717static struct flt_kw_list filter_kws = { "CACHE", { }, {
1718 { "cache", parse_cache_flt, NULL },
1719 { NULL, NULL, NULL },
1720 }
1721};
1722
1723INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1724
William Lallemand1f49a362017-11-21 20:01:26 +01001725static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001726 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1727 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001728}};
1729
Willy Tarreau0108d902018-11-25 19:14:37 +01001730INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001731
William Lallemand41db4602017-10-30 11:15:51 +01001732static struct action_kw_list http_res_actions = {
1733 .kw = {
1734 { "cache-store", parse_cache_store },
1735 { NULL, NULL }
1736 }
1737};
1738
Willy Tarreau0108d902018-11-25 19:14:37 +01001739INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1740
William Lallemand41db4602017-10-30 11:15:51 +01001741static struct action_kw_list http_req_actions = {
1742 .kw = {
1743 { "cache-use", parse_cache_use },
1744 { NULL, NULL }
1745 }
1746};
1747
Willy Tarreau0108d902018-11-25 19:14:37 +01001748INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1749
Willy Tarreau2231b632019-03-29 18:26:52 +01001750struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001751 .obj_type = OBJ_TYPE_APPLET,
1752 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001753 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001754 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001755};
1756
Willy Tarreaue6552512018-11-26 11:33:13 +01001757/* config parsers for this section */
1758REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02001759REGISTER_POST_CHECK(post_check_cache);