blob: 2c37ca13b53d8f4aa459930c1f2c8fd4988fa3b1 [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>
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +010031#include <haproxy/sample.h>
Willy Tarreau334099c2020-06-03 18:38:48 +020032#include <haproxy/shctx.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020033#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020034#include <haproxy/stream_interface.h>
William Lallemand41db4602017-10-30 11:15:51 +010035
Christopher Faulet27d93c32018-12-15 22:32:02 +010036#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010037 * the filter keyword) */
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +020038#define CACHE_FLT_INIT 0x00000002 /* Whether the cache name was freed. */
Christopher Fauletafd819c2018-12-11 08:57:45 +010039
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010040const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010041
Willy Tarreau2231b632019-03-29 18:26:52 +010042extern struct applet http_cache_applet;
William Lallemand41db4602017-10-30 11:15:51 +010043
44struct flt_ops cache_ops;
45
46struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010047 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010048 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010049 unsigned int maxage; /* max-age */
50 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020051 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010052 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010053};
54
Christopher Faulet95220e22018-12-07 17:34:39 +010055/* cache config for filters */
56struct cache_flt_conf {
57 union {
58 struct cache *cache; /* cache used by the filter */
59 char *name; /* cache name used during conf parsing */
60 } c;
61 unsigned int flags; /* CACHE_FLT_F_* */
62};
63
William Lallemand41db4602017-10-30 11:15:51 +010064/*
65 * cache ctx for filters
66 */
67struct cache_st {
William Lallemand41db4602017-10-30 11:15:51 +010068 struct shared_block *first_block;
69};
70
71struct cache_entry {
72 unsigned int latest_validation; /* latest validation date */
73 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020074 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010075
William Lallemand41db4602017-10-30 11:15:51 +010076 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010077 char hash[20];
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +020078
79 unsigned int etag_length; /* Length of the ETag value (if one was found in the response). */
80 unsigned int etag_offset; /* Offset of the ETag value in the data buffer. */
81
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +020082 time_t last_modified; /* Origin server "Last-Modified" header value converted in
83 * seconds since epoch. If no "Last-Modified"
84 * header is found, use "Date" header value,
85 * otherwise use reception time. This field will
86 * be used in case of an "If-Modified-Since"-based
87 * conditional request. */
88
William Lallemand41db4602017-10-30 11:15:51 +010089 unsigned char data[0];
90};
91
92#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010093#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010094
95static struct list caches = LIST_HEAD_INIT(caches);
William Lallemandd1d1e222019-08-28 15:22:49 +020096static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */
William Lallemand41db4602017-10-30 11:15:51 +010097static struct cache *tmp_cache_config = NULL;
98
Willy Tarreau8ceae722018-11-26 11:58:30 +010099DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
100
William Lallemandf528fff2017-11-23 19:43:17 +0100101struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100102{
103 struct eb32_node *node;
104 struct cache_entry *entry;
105
Willy Tarreau8b507582020-02-25 09:35:07 +0100106 node = eb32_lookup(&cache->entries, read_u32(hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100107 if (!node)
108 return NULL;
109
110 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100111
112 /* if that's not the right node */
113 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
114 return NULL;
115
William Lallemand08727662017-11-21 20:01:27 +0100116 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100117 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100118 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100119 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100120 entry->eb.key = 0;
121 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100122 return NULL;
123
124}
125
126static inline struct shared_context *shctx_ptr(struct cache *cache)
127{
128 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
129}
130
William Lallemand77c11972017-10-31 20:43:01 +0100131static inline struct shared_block *block_ptr(struct cache_entry *entry)
132{
133 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
134}
135
136
137
William Lallemand41db4602017-10-30 11:15:51 +0100138static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100139cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100140{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100141 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100142 return 0;
143}
144
Christopher Faulet95220e22018-12-07 17:34:39 +0100145static void
146cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
147{
148 struct cache_flt_conf *cconf = fconf->conf;
149
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +0200150 if (!(cconf->flags & CACHE_FLT_INIT))
151 free(cconf->c.name);
Christopher Faulet95220e22018-12-07 17:34:39 +0100152 free(cconf);
153}
154
William Lallemand4da3f8a2017-10-31 14:33:34 +0100155static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100156cache_store_check(struct proxy *px, struct flt_conf *fconf)
157{
158 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100159 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100160 struct cache *cache;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100161 int comp = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100162
William Lallemandd1d1e222019-08-28 15:22:49 +0200163 /* Find the cache corresponding to the name in the filter config. The
164 * cache will not be referenced now in the filter config because it is
165 * not fully allocated. This step will be performed during the cache
166 * post_check.
167 */
168 list_for_each_entry(cache, &caches_config, list) {
169 if (!strcmp(cache->id, cconf->c.name))
Christopher Faulet95220e22018-12-07 17:34:39 +0100170 goto found;
Christopher Faulet95220e22018-12-07 17:34:39 +0100171 }
172
173 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
174 proxy_type_str(px), px->id, (char *)cconf->c.name);
175 return 1;
176
177 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100178 /* Here <cache> points on the cache the filter must use and <cconf>
179 * points on the cache filter configuration. */
180
181 /* Check all filters for proxy <px> to know if the compression is
Christopher Faulet27d93c32018-12-15 22:32:02 +0100182 * enabled and if it is after the cache. When the compression is before
183 * the cache, an error is returned. Also check if the cache filter must
184 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100185 list_for_each_entry(f, &px->filter_configs, list) {
186 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100187 /* The compression filter must be evaluated after the cache. */
188 if (comp) {
189 ha_alert("config: %s '%s': unable to enable the compression filter before "
190 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
191 return 1;
192 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100193 }
Christopher Faulet8f7fe1c2019-07-15 15:08:25 +0200194 else if (f->id == http_comp_flt_id)
Christopher Faulet27d93c32018-12-15 22:32:02 +0100195 comp = 1;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200196 else if (f->id == fcgi_flt_id)
197 continue;
Christopher Faulet27d93c32018-12-15 22:32:02 +0100198 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
199 /* Implicit declaration is only allowed with the
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200200 * compression and fcgi. For other filters, an implicit
Christopher Faulet27d93c32018-12-15 22:32:02 +0100201 * declaration is required. */
202 ha_alert("config: %s '%s': require an explicit filter declaration "
203 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
204 return 1;
205 }
206
Christopher Fauletafd819c2018-12-11 08:57:45 +0100207 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100208 return 0;
209}
210
211static int
Christopher Faulet65554e12020-03-06 14:52:06 +0100212cache_store_strm_init(struct stream *s, struct filter *filter)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100213{
Christopher Faulet65554e12020-03-06 14:52:06 +0100214 struct cache_st *st;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100215
Christopher Faulet65554e12020-03-06 14:52:06 +0100216 st = pool_alloc_dirty(pool_head_cache_st);
217 if (st == NULL)
218 return -1;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100219
Christopher Faulet65554e12020-03-06 14:52:06 +0100220 st->first_block = NULL;
221 filter->ctx = st;
Christopher Faulet839791a2019-01-07 16:12:07 +0100222
Christopher Faulet65554e12020-03-06 14:52:06 +0100223 /* Register post-analyzer on AN_RES_WAIT_HTTP */
224 filter->post_analyzers |= AN_RES_WAIT_HTTP;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100225 return 1;
226}
227
Christopher Faulet65554e12020-03-06 14:52:06 +0100228static void
229cache_store_strm_deinit(struct stream *s, struct filter *filter)
William Lallemand49dc0482017-11-24 14:33:54 +0100230{
231 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100232 struct cache_flt_conf *cconf = FLT_CONF(filter);
233 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100234 struct shared_context *shctx = shctx_ptr(cache);
235
William Lallemand49dc0482017-11-24 14:33:54 +0100236 /* Everything should be released in the http_end filter, but we need to do it
237 * there too, in case of errors */
William Lallemand49dc0482017-11-24 14:33:54 +0100238 if (st && st->first_block) {
William Lallemand49dc0482017-11-24 14:33:54 +0100239 shctx_lock(shctx);
240 shctx_row_dec_hot(shctx, st->first_block);
241 shctx_unlock(shctx);
William Lallemand49dc0482017-11-24 14:33:54 +0100242 }
243 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100244 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100245 filter->ctx = NULL;
246 }
William Lallemand49dc0482017-11-24 14:33:54 +0100247}
248
Christopher Faulet839791a2019-01-07 16:12:07 +0100249static int
250cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn,
251 unsigned an_bit)
252{
253 struct http_txn *txn = s->txn;
254 struct http_msg *msg = &txn->rsp;
255 struct cache_st *st = filter->ctx;
256
257 if (an_bit != AN_RES_WAIT_HTTP)
258 goto end;
259
260 /* Here we need to check if any compression filter precedes the cache
261 * filter. This is only possible when the compression is configured in
262 * the frontend while the cache filter is configured on the
263 * backend. This case cannot be detected during HAProxy startup. So in
264 * such cases, the cache is disabled.
265 */
266 if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) {
267 pool_free(pool_head_cache_st, st);
268 filter->ctx = NULL;
269 }
270
271 end:
272 return 1;
273}
William Lallemand49dc0482017-11-24 14:33:54 +0100274
275static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100276cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
277{
278 struct cache_st *st = filter->ctx;
279
William Lallemand4da3f8a2017-10-31 14:33:34 +0100280 if (!(msg->chn->flags & CF_ISRESP) || !st)
281 return 1;
282
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200283 if (st->first_block)
Christopher Faulet67658c92018-12-06 21:59:39 +0100284 register_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100285 return 1;
286}
287
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200288static inline void disable_cache_entry(struct cache_st *st,
289 struct filter *filter, struct shared_context *shctx)
290{
291 struct cache_entry *object;
292
293 object = (struct cache_entry *)st->first_block->data;
294 filter->ctx = NULL; /* disable cache */
295 shctx_lock(shctx);
296 shctx_row_dec_hot(shctx, st->first_block);
297 object->eb.key = 0;
298 shctx_unlock(shctx);
299 pool_free(pool_head_cache_st, st);
300}
301
William Lallemand4da3f8a2017-10-31 14:33:34 +0100302static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100303cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
304 unsigned int offset, unsigned int len)
305{
Christopher Faulet95220e22018-12-07 17:34:39 +0100306 struct cache_flt_conf *cconf = FLT_CONF(filter);
307 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100308 struct cache_st *st = filter->ctx;
309 struct htx *htx = htxbuf(&msg->chn->buf);
310 struct htx_blk *blk;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200311 struct shared_block *fb;
Christopher Faulet497c7592020-03-02 16:19:50 +0100312 struct htx_ret htxret;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200313 unsigned int orig_len, to_forward;
314 int ret;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100315
316 if (!len)
317 return len;
318
319 if (!st->first_block) {
320 unregister_data_filter(s, msg->chn, filter);
321 return len;
322 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100323
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200324 chunk_reset(&trash);
325 orig_len = len;
326 to_forward = 0;
Christopher Faulet497c7592020-03-02 16:19:50 +0100327
328 htxret = htx_find_offset(htx, offset);
329 blk = htxret.blk;
330 offset = htxret.ret;
331 for (; blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100332 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200333 uint32_t info, sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100334 struct ist v;
335
336 switch (type) {
337 case HTX_BLK_UNUSED:
338 break;
339
340 case HTX_BLK_DATA:
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100341 v = htx_get_blk_value(htx, blk);
342 v.ptr += offset;
343 v.len -= offset;
344 if (v.len > len)
345 v.len = len;
346
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200347 info = (type << 28) + v.len;
348 chunk_memcat(&trash, (char *)&info, sizeof(info));
349 chunk_memcat(&trash, v.ptr, v.len);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100350 to_forward += v.len;
351 len -= v.len;
352 break;
353
354 default:
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200355 /* Here offset must always be 0 because only
356 * DATA blocks can be partially transferred. */
357 if (offset)
358 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100359 if (sz > len)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200360 goto end;
361
362 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
363 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100364 to_forward += sz;
365 len -= sz;
366 break;
367 }
368
369 offset = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100370 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200371
372 end:
373 shctx_lock(shctx);
374 fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data);
375 if (!fb) {
376 shctx_unlock(shctx);
377 goto no_cache;
378 }
379 shctx_unlock(shctx);
380
381 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
382 (unsigned char *)b_head(&trash), b_data(&trash));
383 if (ret < 0)
384 goto no_cache;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100385
386 return to_forward;
387
388 no_cache:
389 disable_cache_entry(st, filter, shctx);
390 unregister_data_filter(s, msg->chn, filter);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200391 return orig_len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100392}
393
394static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100395cache_store_http_end(struct stream *s, struct filter *filter,
396 struct http_msg *msg)
397{
398 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100399 struct cache_flt_conf *cconf = FLT_CONF(filter);
400 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100401 struct shared_context *shctx = shctx_ptr(cache);
402 struct cache_entry *object;
403
404 if (!(msg->chn->flags & CF_ISRESP))
405 return 1;
406
407 if (st && st->first_block) {
408
409 object = (struct cache_entry *)st->first_block->data;
410
411 /* does not need to test if the insertion worked, if it
412 * doesn't, the blocks will be reused anyway */
413
414 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100415 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
416 object->eb.key = 0;
417 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100418 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100419 shctx_row_dec_hot(shctx, st->first_block);
420 shctx_unlock(shctx);
421
422 }
423 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100424 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100425 filter->ctx = NULL;
426 }
427
428 return 1;
429}
430
431 /*
432 * This intends to be used when checking HTTP headers for some
433 * word=value directive. Return a pointer to the first character of value, if
434 * the word was not found or if there wasn't any value assigned ot it return NULL
435 */
436char *directive_value(const char *sample, int slen, const char *word, int wlen)
437{
438 int st = 0;
439
440 if (slen < wlen)
441 return 0;
442
443 while (wlen) {
444 char c = *sample ^ *word;
445 if (c && c != ('A' ^ 'a'))
446 return NULL;
447 sample++;
448 word++;
449 slen--;
450 wlen--;
451 }
452
453 while (slen) {
454 if (st == 0) {
455 if (*sample != '=')
456 return NULL;
457 sample++;
458 slen--;
459 st = 1;
460 continue;
461 } else {
462 return (char *)sample;
463 }
464 }
465
466 return NULL;
467}
468
469/*
470 * Return the maxage in seconds of an HTTP response.
471 * Compute the maxage using either:
472 * - the assigned max-age of the cache
473 * - the s-maxage directive
474 * - the max-age directive
475 * - (Expires - Data) headers
476 * - the default-max-age of the cache
477 *
478 */
William Lallemand49b44532017-11-24 18:53:43 +0100479int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100480{
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200481 struct htx *htx = htxbuf(&s->res.buf);
482 struct http_hdr_ctx ctx = { .blk = NULL };
William Lallemand4da3f8a2017-10-31 14:33:34 +0100483 int smaxage = -1;
484 int maxage = -1;
485
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200486 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
487 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100488
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200489 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
490 if (value) {
491 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100492
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200493 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
494 chunk_strncat(chk, "", 1);
495 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100496 }
497
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200498 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
499 if (value) {
500 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200501
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200502 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
503 chunk_strncat(chk, "", 1);
504 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100505 }
506 }
507
508 /* TODO: Expires - Data */
509
510
511 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100512 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100513
514 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100515 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100516
William Lallemand49b44532017-11-24 18:53:43 +0100517 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100518
519}
520
521
William Lallemanda400a3a2017-11-20 19:13:12 +0100522static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
523{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200524 struct cache_entry *object = (struct cache_entry *)block->data;
525
526 if (first == block && object->eb.key)
527 eb32_delete(&object->eb);
528 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100529}
530
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200531
532/* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the
533 * date value should be compared to a date determined by in a previous response (for
534 * the same entity). This date could either be the "Last-Modified" value, or the "Date"
535 * value of the response's reception time (by decreasing order of priority). */
536static time_t get_last_modified_time(struct htx *htx)
537{
538 time_t last_modified = 0;
539 struct http_hdr_ctx ctx = { .blk = NULL };
540 struct tm tm = {};
541
542 if (http_find_header(htx, ist("last-modified"), &ctx, 1)) {
543 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
544 last_modified = my_timegm(&tm);
545 }
546 }
547
548 if (!last_modified) {
549 ctx.blk = NULL;
550 if (http_find_header(htx, ist("date"), &ctx, 1)) {
551 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
552 last_modified = my_timegm(&tm);
553 }
554 }
555 }
556
557 /* Fallback on the current time if no "Last-Modified" or "Date" header
558 * was found. */
559 if (!last_modified)
560 last_modified = now.tv_sec;
561
562 return last_modified;
563}
564
William Lallemand41db4602017-10-30 11:15:51 +0100565/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500566 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100567 * register a filter to store the data
568 */
569enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200570 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100571{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200572 unsigned int age;
573 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100574 struct http_txn *txn = s->txn;
575 struct http_msg *msg = &txn->rsp;
576 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100577 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100578 struct cache_flt_conf *cconf = rule->arg.act.p[0];
579 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100580 struct cache_st *cache_ctx = NULL;
581 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100582 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200583 struct htx *htx;
584 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200585 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200586 int32_t pos;
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200587 unsigned int etag_length = 0;
588 unsigned int etag_offset = 0;
589 struct ist header_name = IST_NULL;
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200590 time_t last_modified = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100591
William Lallemand4da3f8a2017-10-31 14:33:34 +0100592 /* Don't cache if the response came from a cache */
593 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
594 s->target == &http_cache_applet.obj_type) {
595 goto out;
596 }
597
598 /* cache only HTTP/1.1 */
599 if (!(txn->req.flags & HTTP_MSGF_VER_11))
600 goto out;
601
Willy Tarreau6905d182019-10-01 17:59:17 +0200602 /* cache only GET method */
603 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100604 goto out;
605
Willy Tarreauc9036c02019-01-11 19:38:25 +0100606 /* cache key was not computed */
607 if (!key)
608 goto out;
609
William Lallemand4da3f8a2017-10-31 14:33:34 +0100610 /* cache only 200 status code */
611 if (txn->status != 200)
612 goto out;
613
Christopher Faulet839791a2019-01-07 16:12:07 +0100614 /* Find the corresponding filter instance for the current stream */
615 list_for_each_entry(filter, &s->strm_flt.filters, list) {
616 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
617 /* No filter ctx, don't cache anything */
618 if (!filter->ctx)
619 goto out;
620 cache_ctx = filter->ctx;
621 break;
622 }
623 }
624
625 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200626 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100627
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200628 /* Do not cache too big objects. */
629 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
630 htx->data + htx->extra > shctx->max_obj_size)
631 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100632
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200633 /* Does not manage Vary at the moment. We will need a secondary key later for that */
634 ctx.blk = NULL;
635 if (http_find_header(htx, ist("Vary"), &ctx, 0))
636 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100637
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200638 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100639
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200640 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
641 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100642
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200643 age = 0;
644 ctx.blk = NULL;
645 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
646 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
647 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
648 hdr_age = CACHE_ENTRY_MAX_AGE;
649 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100650 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200651 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100652 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100653
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200654 /* Build a last-modified time that will be stored in the cache_entry and
655 * compared to a future If-Modified-Since client header. */
656 last_modified = get_last_modified_time(htx);
657
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200658 chunk_reset(&trash);
659 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
660 struct htx_blk *blk = htx_get_blk(htx, pos);
661 enum htx_blk_type type = htx_get_blk_type(blk);
662 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100663
Christopher Fauletb0667472019-09-03 22:22:12 +0200664 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200665 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
666 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200667
668 /* Look for optional ETag header.
669 * We need to store the offset of the ETag value in order for
670 * future conditional requests to be able to perform ETag
671 * comparisons. */
672 if (type == HTX_BLK_HDR) {
673 header_name = htx_get_blk_name(htx, blk);
674 if (isteq(header_name, ist("etag"))) {
675 etag_length = sz - istlen(header_name);
676 etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name);
677 }
678 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200679 if (type == HTX_BLK_EOH)
680 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200681 }
682
Christopher Fauletb0667472019-09-03 22:22:12 +0200683 /* Do not cache objects if the headers are too big. */
684 if (hdrs_len > htx->size - global.tune.maxrewrite)
685 goto out;
686
William Lallemand4da3f8a2017-10-31 14:33:34 +0100687 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200688 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100689 if (!first) {
690 shctx_unlock(shctx);
691 goto out;
692 }
693 shctx_unlock(shctx);
694
Willy Tarreau1093a452018-04-06 19:02:25 +0200695 /* the received memory is not initialized, we need at least to mark
696 * the object as not indexed yet.
697 */
698 object = (struct cache_entry *)first->data;
699 object->eb.node.leaf_p = NULL;
700 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200701 object->age = age;
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200702 object->last_modified = last_modified;
Willy Tarreau1093a452018-04-06 19:02:25 +0200703
William Lallemand4da3f8a2017-10-31 14:33:34 +0100704 /* reserve space for the cache_entry structure */
705 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200706 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100707 /* cache the headers in a http action because it allows to chose what
708 * to cache, for example you might want to cache a response before
709 * modifying some HTTP headers, or on the contrary after modifying
710 * those headers.
711 */
712
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200713 /* Write the ETag information in the cache_entry if needed. */
714 object->etag_length = etag_length;
715 object->etag_offset = etag_offset;
716
William Lallemand4da3f8a2017-10-31 14:33:34 +0100717 /* does not need to be locked because it's in the "hot" list,
718 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200719 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
720 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100721
722 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100723 if (cache_ctx) {
724 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100725
Willy Tarreauc9036c02019-01-11 19:38:25 +0100726 object->eb.key = key;
727
Christopher Faulet839791a2019-01-07 16:12:07 +0100728 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
729 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100730
Christopher Faulet839791a2019-01-07 16:12:07 +0100731 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100732
Christopher Faulet839791a2019-01-07 16:12:07 +0100733 old = entry_exist(cconf->c.cache, txn->cache_hash);
734 if (old) {
735 eb32_delete(&old->eb);
736 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100737 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100738 shctx_unlock(shctx);
739
740 /* store latest value and expiration time */
741 object->latest_validation = now.tv_sec;
742 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
743 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100744 }
745
746out:
747 /* if does not cache */
748 if (first) {
749 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100750 first->len = 0;
751 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100752 shctx_row_dec_hot(shctx, first);
753 shctx_unlock(shctx);
754 }
755
William Lallemand41db4602017-10-30 11:15:51 +0100756 return ACT_RET_CONT;
757}
758
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100759#define HTX_CACHE_INIT 0 /* Initial state. */
760#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
761#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200762#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
763#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100764
William Lallemandecb73b12017-11-24 14:33:55 +0100765static void http_cache_applet_release(struct appctx *appctx)
766{
Christopher Faulet95220e22018-12-07 17:34:39 +0100767 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100768 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100769 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100770 struct shared_block *first = block_ptr(cache_ptr);
771
772 shctx_lock(shctx_ptr(cache));
773 shctx_row_dec_hot(shctx_ptr(cache), first);
774 shctx_unlock(shctx_ptr(cache));
775}
776
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200777
778static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
779 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100780{
Christopher Faulet95220e22018-12-07 17:34:39 +0100781 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
782 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200783 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200784 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200785 unsigned int max, total;
786 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100787
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200788 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
789 if (!max)
790 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200791 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200792 ? (info & 0xff) + ((info >> 8) & 0xfffff)
793 : info & 0xfffffff);
794 if (blksz > max)
795 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100796
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200797 blk = htx_add_blk(htx, type, blksz);
798 if (!blk)
799 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100800
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200801 blk->info = info;
802 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200803 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200804 while (blksz) {
805 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200806 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200807 offset += max;
808 blksz -= max;
809 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200810 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200811 if (blksz || offset == shctx->block_size) {
812 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
813 offset = 0;
814 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100815 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200816 appctx->ctx.cache.offset = offset;
817 appctx->ctx.cache.next = shblk;
818 appctx->ctx.cache.sent += total;
819 return total;
820}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100821
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200822static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
823 uint32_t info, struct shared_block *shblk, unsigned int offset)
824{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100825
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200826 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
827 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
828 unsigned int max, total, rem_data;
829 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100830
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200831 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
832 if (!max)
833 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100834
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200835 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200836 if (appctx->ctx.cache.rem_data) {
837 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200838 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200839 }
840 else {
841 blksz = (info & 0xfffffff);
842 total = 4;
843 }
844 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200845 rem_data = blksz - max;
846 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100847 }
848
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200849 while (blksz) {
850 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100851
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200852 max = MIN(blksz, shctx->block_size - offset);
853 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
854 offset += sz;
855 blksz -= sz;
856 total += sz;
857 if (sz < max)
858 break;
859 if (blksz || offset == shctx->block_size) {
860 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
861 offset = 0;
862 }
863 }
864
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200865 appctx->ctx.cache.offset = offset;
866 appctx->ctx.cache.next = shblk;
867 appctx->ctx.cache.sent += total;
868 appctx->ctx.cache.rem_data = rem_data + blksz;
869 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100870}
871
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200872static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
873 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100874{
Christopher Faulet95220e22018-12-07 17:34:39 +0100875 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
876 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200877 struct shared_block *shblk;
878 unsigned int offset, sz;
879 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100880
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200881 while (len) {
882 enum htx_blk_type type;
883 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100884
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200885 shblk = appctx->ctx.cache.next;
886 offset = appctx->ctx.cache.offset;
887 if (appctx->ctx.cache.rem_data) {
888 type = HTX_BLK_DATA;
889 info = 0;
890 goto add_data_blk;
891 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100892
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500893 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200894 sz = MIN(4, shctx->block_size - offset);
895 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
896 offset += sz;
897 if (sz < 4) {
898 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
899 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
900 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100901 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200902
903 /* Get payload of the next HTX block and insert it. */
904 type = (info >> 28);
905 if (type != HTX_BLK_DATA)
906 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
907 else {
908 add_data_blk:
909 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100910 }
911
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200912 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100913 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200914 total += ret;
915 len -= ret;
916
917 if (appctx->ctx.cache.rem_data || type == mark)
918 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100919 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100920
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100921 return total;
922}
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200923
924static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
925{
926 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
927 unsigned int age;
928 char *end;
929
930 chunk_reset(&trash);
931 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
932 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
933 age = CACHE_ENTRY_MAX_AGE;
934 end = ultoa_o(age, b_head(&trash), b_size(&trash));
935 b_set_data(&trash, end - b_head(&trash));
936 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
937 return 0;
938 return 1;
939}
940
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200941static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100942{
943 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
944 struct shared_block *first = block_ptr(cache_ptr);
945 struct stream_interface *si = appctx->owner;
946 struct channel *req = si_oc(si);
947 struct channel *res = si_ic(si);
948 struct htx *req_htx, *res_htx;
949 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200950 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100951 size_t ret, total = 0;
952
953 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200954 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100955
956 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
957 goto out;
958
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500959 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100960 if (!b_size(&res->buf)) {
961 si_rx_room_blk(si);
962 goto out;
963 }
964
Willy Tarreauefef3232018-12-16 00:37:45 +0100965 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100966 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100967
968 if (appctx->st0 == HTX_CACHE_INIT) {
969 appctx->ctx.cache.next = block_ptr(cache_ptr);
970 appctx->ctx.cache.offset = sizeof(*cache_ptr);
971 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200972 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100973 appctx->st0 = HTX_CACHE_HEADER;
974 }
975
976 if (appctx->st0 == HTX_CACHE_HEADER) {
977 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200978 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
979 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
980 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
981 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100982 goto error;
983
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +0200984 /* In case of a conditional request, we might want to send a
985 * "304 Not Modified" response instead of the stored data. */
Tim Duesterhuse0142342020-10-22 21:15:06 +0200986 if (appctx->ctx.cache.send_notmodified) {
987 if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) {
988 /* If replacing the status code fails we need to send the full response. */
989 appctx->ctx.cache.send_notmodified = 0;
990 }
991 }
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +0200992
993 /* Skip response body for HEAD requests or in case of "304 Not
994 * Modified" response. */
995 if (si_strm(si)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified)
Christopher Fauletf0dd0372019-02-25 11:08:34 +0100996 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100997 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200998 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100999 }
1000
1001 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001002 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1003 if (len) {
1004 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
1005 if (ret < len) {
1006 si_rx_room_blk(si);
1007 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001008 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001009 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001010 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001011 }
1012
1013 if (appctx->st0 == HTX_CACHE_EOM) {
Christopher Faulet810df062020-07-22 16:20:34 +02001014 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001015 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1016 si_rx_room_blk(si);
1017 goto out;
1018 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001019 appctx->st0 = HTX_CACHE_END;
1020 }
1021
1022 end:
Christopher Fauletadb36312019-02-25 11:40:49 +01001023 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001024 res->flags |= CF_READ_NULL;
1025 si_shutr(si);
1026 }
1027
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001028 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001029 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +01001030 if (total)
1031 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001032 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +01001033
1034 /* eat the whole request */
1035 if (co_data(req)) {
1036 req_htx = htx_from_buf(&req->buf);
1037 co_htx_skip(req, req_htx, co_data(req));
1038 htx_to_buf(req_htx, &req->buf);
1039 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001040 return;
1041
1042 error:
1043 /* Sent and HTTP error 500 */
1044 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +02001045 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001046 res->buf.data = b_data(errmsg);
1047 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1048 res_htx = htx_from_buf(&res->buf);
1049
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001050 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001051 appctx->st0 = HTX_CACHE_END;
1052 goto end;
1053}
1054
1055
Christopher Faulet95220e22018-12-07 17:34:39 +01001056static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001057{
1058 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001059 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001060
Christopher Faulet95220e22018-12-07 17:34:39 +01001061 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001062 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001063 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001064 }
1065
1066 /* check if a cache filter was already registered with this cache
1067 * name, if that's the case, must use it. */
1068 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001069 if (fconf->id == cache_store_flt_id) {
1070 cconf = fconf->conf;
1071 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1072 rule->arg.act.p[0] = cconf;
1073 return 1;
1074 }
William Lallemand41db4602017-10-30 11:15:51 +01001075 }
1076 }
1077
Christopher Faulet95220e22018-12-07 17:34:39 +01001078 /* Create the filter cache config */
1079 cconf = calloc(1, sizeof(*cconf));
1080 if (!cconf) {
1081 memprintf(err, "out of memory\n");
1082 goto err;
1083 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001084 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001085 cconf->c.name = strdup(name);
1086 if (!cconf->c.name) {
1087 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001088 goto err;
1089 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001090
William Lallemand41db4602017-10-30 11:15:51 +01001091 /* register a filter to fill the cache buffer */
1092 fconf = calloc(1, sizeof(*fconf));
1093 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001094 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001095 goto err;
1096 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001097 fconf->id = cache_store_flt_id;
1098 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001099 fconf->ops = &cache_ops;
1100 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1101
Christopher Faulet95220e22018-12-07 17:34:39 +01001102 rule->arg.act.p[0] = cconf;
1103 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001104
Christopher Faulet95220e22018-12-07 17:34:39 +01001105 err:
1106 free(cconf);
1107 return 0;
1108}
1109
1110enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1111 struct act_rule *rule, char **err)
1112{
1113 rule->action = ACT_CUSTOM;
1114 rule->action_ptr = http_action_store_cache;
1115
1116 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1117 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001118
Christopher Faulet95220e22018-12-07 17:34:39 +01001119 (*orig_arg)++;
1120 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001121}
1122
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001123/* This produces a sha1 hash of the concatenation of the HTTP method,
1124 * the first occurrence of the Host header followed by the path component
1125 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001126int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001127{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001128 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001129 struct htx *htx = htxbuf(&s->req.buf);
1130 struct htx_sl *sl;
1131 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001132 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001133 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001134 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001135
William Lallemandf528fff2017-11-23 19:43:17 +01001136 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001137 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001138
1139 switch (txn->meth) {
1140 case HTTP_METH_HEAD:
1141 case HTTP_METH_GET:
1142 chunk_memcat(trash, "GET", 3);
1143 break;
1144 default:
1145 return 0;
1146 }
1147
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001148 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001149 uri = htx_sl_req_uri(sl); // whole uri
1150 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001151 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001152
1153 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1154 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1155 * URIs are almost always sent in absolute form with their scheme. In
1156 * this case, the scheme is almost always "https". In order to support
1157 * sharing of cache objects between H1 and H2, we'll hash the absolute
1158 * URI whenever known, or prepend "https://" + the Host header for
1159 * relative URIs. The difference will only appear on absolute HTTP/1
1160 * requests sent to an origin server, which practically is never met in
1161 * the real world so we don't care about the ability to share the same
1162 * key here.URIs are normalized from the absolute URI to an origin form as
1163 * well.
1164 */
1165 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001166 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001167 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1168 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001169 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001170 }
1171
1172 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001173
1174 /* hash everything */
1175 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001176 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001177 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1178
1179 return 1;
1180}
1181
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001182/* Looks for "If-None-Match" headers in the request and compares their value
1183 * with the one that might have been stored in the cache_entry. If any of them
1184 * matches, a "304 Not Modified" response should be sent instead of the cached
1185 * data.
1186 * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001187 * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2).
1188 *
1189 * If no "If-None-Match" header was found, look for an "If-Modified-Since"
1190 * header and compare its value (date) to the one stored in the cache_entry.
1191 * If the request's date is later than the cached one, we also send a
1192 * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2).
1193 *
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001194 * Returns 1 if "304 Not Modified" should be sent, 0 otherwise.
1195 */
1196static int should_send_notmodified_response(struct cache *cache, struct htx *htx,
1197 struct cache_entry *entry)
1198{
1199 int retval = 0;
1200
1201 struct http_hdr_ctx ctx = { .blk = NULL };
1202 struct ist cache_entry_etag = IST_NULL;
1203 struct buffer *etag_buffer = NULL;
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001204 int if_none_match_found = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001205
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001206 struct tm tm = {};
1207 time_t if_modified_since = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001208
1209 /* If we find a "If-None-Match" header in the request, rebuild the
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001210 * cache_entry's ETag in order to perform comparisons.
1211 * There could be multiple "if-none-match" header lines. */
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001212 while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) {
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001213 if_none_match_found = 1;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001214
1215 /* A '*' matches everything. */
1216 if (isteq(ctx.value, ist("*")) != 0) {
1217 retval = 1;
1218 break;
1219 }
1220
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001221 /* No need to rebuild an etag if none was stored in the cache. */
1222 if (entry->etag_length == 0)
1223 break;
1224
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001225 /* Rebuild the stored ETag. */
1226 if (etag_buffer == NULL) {
1227 etag_buffer = get_trash_chunk();
1228
1229 if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry),
1230 (unsigned char*)b_orig(etag_buffer),
1231 entry->etag_offset, entry->etag_length) == 0) {
1232 cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length);
1233 } else {
1234 /* We could not rebuild the ETag in one go, we
1235 * won't send a "304 Not Modified" response. */
1236 break;
1237 }
1238 }
1239
1240 if (http_compare_etags(cache_entry_etag, ctx.value) == 1) {
1241 retval = 1;
1242 break;
1243 }
1244 }
1245
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001246 /* If the request did not contain an "If-None-Match" header, we look for
1247 * an "If-Modified-Since" header (see RFC 7232#3.3). */
1248 if (retval == 0 && if_none_match_found == 0) {
1249 ctx.blk = NULL;
1250 if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) {
1251 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
1252 if_modified_since = my_timegm(&tm);
1253
1254 /* We send a "304 Not Modified" response if the
1255 * entry's last modified date is earlier than
1256 * the one found in the "If-Modified-Since"
1257 * header. */
1258 retval = (entry->last_modified <= if_modified_since);
1259 }
1260 }
1261 }
1262
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001263 return retval;
1264}
1265
William Lallemand41db4602017-10-30 11:15:51 +01001266enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1267 struct session *sess, struct stream *s, int flags)
1268{
William Lallemand77c11972017-10-31 20:43:01 +01001269
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001270 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001271 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001272 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1273 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001274
Willy Tarreau6905d182019-10-01 17:59:17 +02001275 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1276 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001277 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001278 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001279 txn->flags |= TX_CACHE_IGNORE;
1280
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001281 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001282
Willy Tarreau504455c2017-12-22 17:47:35 +01001283 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1284 return ACT_RET_CONT;
1285
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001286 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001287 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001288
Willy Tarreau504455c2017-12-22 17:47:35 +01001289 if (s->txn->flags & TX_CACHE_IGNORE)
1290 return ACT_RET_CONT;
1291
Willy Tarreaua1214a52018-12-14 14:00:25 +01001292 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001293 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001294 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001295 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001296
William Lallemanda400a3a2017-11-20 19:13:12 +01001297 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001298 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001299 if (res) {
1300 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001301 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1302 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001303 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001304 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001305 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001306 appctx->rule = rule;
1307 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001308 appctx->ctx.cache.next = NULL;
1309 appctx->ctx.cache.sent = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001310 appctx->ctx.cache.send_notmodified =
1311 should_send_notmodified_response(cache, htxbuf(&s->req.buf), res);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001312
1313 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001314 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001315 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001316 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001317 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001318 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001319 shctx_lock(shctx_ptr(cache));
1320 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1321 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001322 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001323 }
1324 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001325 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001326 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001327}
1328
1329
1330enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1331 struct act_rule *rule, char **err)
1332{
William Lallemand41db4602017-10-30 11:15:51 +01001333 rule->action = ACT_CUSTOM;
1334 rule->action_ptr = http_action_req_cache_use;
1335
Christopher Faulet95220e22018-12-07 17:34:39 +01001336 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001337 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001338
1339 (*orig_arg)++;
1340 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001341}
1342
1343int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1344{
1345 int err_code = 0;
1346
1347 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1348
1349 if (!*args[1]) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001350 ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001351 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001352 err_code |= ERR_ALERT | ERR_ABORT;
1353 goto out;
1354 }
1355
1356 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1357 err_code |= ERR_ABORT;
1358 goto out;
1359 }
1360
1361 if (tmp_cache_config == NULL) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001362 struct cache *cache_config;
1363
William Lallemand41db4602017-10-30 11:15:51 +01001364 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1365 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001366 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001367 err_code |= ERR_ALERT | ERR_ABORT;
1368 goto out;
1369 }
1370
1371 strlcpy2(tmp_cache_config->id, args[1], 33);
1372 if (strlen(args[1]) > 32) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001373 ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001374 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001375 err_code |= ERR_WARN;
1376 }
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001377
1378 list_for_each_entry(cache_config, &caches_config, list) {
1379 if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
1380 ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
1381 file, linenum, tmp_cache_config->id);
1382 err_code |= ERR_ALERT | ERR_ABORT;
1383 goto out;
1384 }
1385 }
1386
William Lallemand49b44532017-11-24 18:53:43 +01001387 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001388 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001389 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001390 }
1391 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001392 unsigned long int maxsize;
1393 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001394
1395 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1396 err_code |= ERR_ABORT;
1397 goto out;
1398 }
1399
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001400 maxsize = strtoul(args[1], &err, 10);
1401 if (err == args[1] || *err != '\0') {
1402 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1403 file, linenum, args[1]);
1404 err_code |= ERR_ABORT;
1405 goto out;
1406 }
1407
1408 if (maxsize > (UINT_MAX >> 20)) {
1409 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1410 file, linenum, args[1], UINT_MAX >> 20);
1411 err_code |= ERR_ABORT;
1412 goto out;
1413 }
1414
William Lallemand41db4602017-10-30 11:15:51 +01001415 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001416 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001417 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001418 } else if (strcmp(args[0], "max-age") == 0) {
1419 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1420 err_code |= ERR_ABORT;
1421 goto out;
1422 }
1423
1424 if (!*args[1]) {
1425 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1426 file, linenum, args[0]);
1427 err_code |= ERR_WARN;
1428 }
1429
1430 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001431 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001432 unsigned int maxobjsz;
1433 char *err;
1434
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001435 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1436 err_code |= ERR_ABORT;
1437 goto out;
1438 }
1439
1440 if (!*args[1]) {
1441 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1442 file, linenum, args[0]);
1443 err_code |= ERR_WARN;
1444 }
1445
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001446 maxobjsz = strtoul(args[1], &err, 10);
1447 if (err == args[1] || *err != '\0') {
1448 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1449 file, linenum, args[1]);
1450 err_code |= ERR_ABORT;
1451 goto out;
1452 }
1453 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001454 }
1455 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001456 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001457 err_code |= ERR_ALERT | ERR_FATAL;
1458 goto out;
1459 }
1460out:
1461 return err_code;
1462}
1463
1464/* once the cache section is parsed */
1465
1466int cfg_post_parse_section_cache()
1467{
William Lallemand41db4602017-10-30 11:15:51 +01001468 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001469
1470 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001471
1472 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001473 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001474 err_code |= ERR_FATAL | ERR_ALERT;
1475 goto out;
1476 }
1477
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001478 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001479 /* Default max. file size is a 256th of the cache size. */
1480 tmp_cache_config->maxobjsz =
1481 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001482 }
1483 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1484 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1485 err_code |= ERR_FATAL | ERR_ALERT;
1486 goto out;
1487 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001488
William Lallemandd1d1e222019-08-28 15:22:49 +02001489 /* add to the list of cache to init and reinit tmp_cache_config
1490 * for next cache section, if any.
1491 */
1492 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1493 tmp_cache_config = NULL;
1494 return err_code;
1495 }
1496out:
1497 free(tmp_cache_config);
1498 tmp_cache_config = NULL;
1499 return err_code;
1500
1501}
1502
1503int post_check_cache()
1504{
1505 struct proxy *px;
1506 struct cache *back, *cache_config, *cache;
1507 struct shared_context *shctx;
1508 int ret_shctx;
1509 int err_code = 0;
1510
1511 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1512
1513 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1514 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001515
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001516 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001517 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001518 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001519 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001520 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001521
1522 err_code |= ERR_FATAL | ERR_ALERT;
1523 goto out;
1524 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001525 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001526 /* the cache structure is stored in the shctx and added to the
1527 * caches list, we can remove the entry from the caches_config
1528 * list */
1529 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001530 cache = (struct cache *)shctx->data;
1531 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001532 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001533 LIST_DEL(&cache_config->list);
1534 free(cache_config);
1535
1536 /* Find all references for this cache in the existing filters
1537 * (over all proxies) and reference it in matching filters.
1538 */
1539 for (px = proxies_list; px; px = px->next) {
1540 struct flt_conf *fconf;
1541 struct cache_flt_conf *cconf;
1542
1543 list_for_each_entry(fconf, &px->filter_configs, list) {
1544 if (fconf->id != cache_store_flt_id)
1545 continue;
1546
1547 cconf = fconf->conf;
1548 if (!strcmp(cache->id, cconf->c.name)) {
1549 free(cconf->c.name);
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +02001550 cconf->flags |= CACHE_FLT_INIT;
William Lallemandd1d1e222019-08-28 15:22:49 +02001551 cconf->c.cache = cache;
1552 break;
1553 }
1554 }
1555 }
William Lallemand41db4602017-10-30 11:15:51 +01001556 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001557
William Lallemand41db4602017-10-30 11:15:51 +01001558out:
William Lallemand41db4602017-10-30 11:15:51 +01001559 return err_code;
1560
William Lallemand41db4602017-10-30 11:15:51 +01001561}
1562
William Lallemand41db4602017-10-30 11:15:51 +01001563struct flt_ops cache_ops = {
1564 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001565 .check = cache_store_check,
1566 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001567
Christopher Faulet65554e12020-03-06 14:52:06 +01001568 /* Handle stream init/deinit */
1569 .attach = cache_store_strm_init,
1570 .detach = cache_store_strm_deinit,
1571
William Lallemand4da3f8a2017-10-31 14:33:34 +01001572 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001573 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001574
1575 /* Filter HTTP requests and responses */
1576 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001577 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001578 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001579};
1580
Christopher Faulet99a17a22018-12-11 09:18:27 +01001581
1582
1583static int
1584parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1585 struct flt_conf *fconf, char **err, void *private)
1586{
1587 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001588 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001589 char *name = NULL;
1590 int pos = *cur_arg;
1591
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001592 /* Get the cache filter name. <pos> point on "cache" keyword */
1593 if (!*args[pos + 1]) {
Tim Duesterhusea969f62020-08-18 22:06:51 +02001594 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001595 goto error;
1596 }
1597 name = strdup(args[pos + 1]);
1598 if (!name) {
1599 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1600 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001601 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001602 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001603
1604 /* Check if an implicit filter with the same name already exists. If so,
1605 * we remove the implicit filter to use the explicit one. */
1606 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1607 if (f->id != cache_store_flt_id)
1608 continue;
1609
1610 cconf = f->conf;
1611 if (strcmp(name, cconf->c.name)) {
1612 cconf = NULL;
1613 continue;
1614 }
1615
1616 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1617 cconf = NULL;
1618 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1619 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01001620 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001621 }
1622
1623 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1624 LIST_DEL(&f->list);
1625 free(f);
1626 free(name);
1627 break;
1628 }
1629
1630 /* No implicit cache filter found, create configuration for the explicit one */
1631 if (!cconf) {
1632 cconf = calloc(1, sizeof(*cconf));
1633 if (!cconf) {
1634 memprintf(err, "%s: out of memory", args[*cur_arg]);
1635 goto error;
1636 }
1637 cconf->c.name = name;
1638 }
1639
1640 cconf->flags = 0;
1641 fconf->id = cache_store_flt_id;
1642 fconf->conf = cconf;
1643 fconf->ops = &cache_ops;
1644
1645 *cur_arg = pos;
1646 return 0;
1647
1648 error:
1649 free(name);
1650 free(cconf);
1651 return -1;
1652}
1653
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001654static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001655{
1656 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1657 return 1;
1658
1659 return 0;
1660}
1661
1662static int cli_io_handler_show_cache(struct appctx *appctx)
1663{
1664 struct cache* cache = appctx->ctx.cli.p0;
1665 struct stream_interface *si = appctx->owner;
1666
William Lallemand1f49a362017-11-21 20:01:26 +01001667 if (cache == NULL) {
1668 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1669 }
1670
1671 list_for_each_entry_from(cache, &caches, list) {
1672 struct eb32_node *node = NULL;
1673 unsigned int next_key;
1674 struct cache_entry *entry;
1675
William Lallemand1f49a362017-11-21 20:01:26 +01001676 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001677 if (!next_key) {
1678 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1679 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001680 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001681 return 0;
1682 }
1683 }
William Lallemand1f49a362017-11-21 20:01:26 +01001684
1685 appctx->ctx.cli.p0 = cache;
1686
1687 while (1) {
1688
1689 shctx_lock(shctx_ptr(cache));
1690 node = eb32_lookup_ge(&cache->entries, next_key);
1691 if (!node) {
1692 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001693 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001694 break;
1695 }
1696
1697 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01001698 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 +01001699
1700 next_key = node->key + 1;
1701 appctx->ctx.cli.i0 = next_key;
1702
1703 shctx_unlock(shctx_ptr(cache));
1704
1705 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001706 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001707 return 0;
1708 }
1709 }
1710
1711 }
1712
1713 return 1;
1714
1715}
1716
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01001717
1718/*
1719 * boolean, returns true if response was built out of a cache entry.
1720 */
1721static int
1722smp_fetch_res_cache_hit(const struct arg *args, struct sample *smp,
1723 const char *kw, void *private)
1724{
1725 smp->data.type = SMP_T_BOOL;
1726 smp->data.u.sint = (smp->strm ? (smp->strm->target == &http_cache_applet.obj_type) : 0);
1727
1728 return 1;
1729}
1730
1731/*
1732 * string, returns cache name (if response came from a cache).
1733 */
1734static int
1735smp_fetch_res_cache_name(const struct arg *args, struct sample *smp,
1736 const char *kw, void *private)
1737{
1738 struct appctx *appctx = NULL;
1739
1740 struct cache_flt_conf *cconf = NULL;
1741 struct cache *cache = NULL;
1742
1743 if (!smp->strm || smp->strm->target != &http_cache_applet.obj_type)
1744 return 0;
1745
1746 /* Get appctx from the stream_interface. */
1747 appctx = si_appctx(&smp->strm->si[1]);
1748 if (appctx && appctx->rule) {
1749 cconf = appctx->rule->arg.act.p[0];
1750 if (cconf) {
1751 cache = cconf->c.cache;
1752
1753 smp->data.type = SMP_T_STR;
1754 smp->flags = SMP_F_CONST;
1755 smp->data.u.str.area = cache->id;
1756 smp->data.u.str.data = strlen(cache->id);
1757 return 1;
1758 }
1759 }
1760
1761 return 0;
1762}
1763
Christopher Faulet99a17a22018-12-11 09:18:27 +01001764/* Declare the filter parser for "cache" keyword */
1765static struct flt_kw_list filter_kws = { "CACHE", { }, {
1766 { "cache", parse_cache_flt, NULL },
1767 { NULL, NULL, NULL },
1768 }
1769};
1770
1771INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1772
William Lallemand1f49a362017-11-21 20:01:26 +01001773static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001774 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1775 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001776}};
1777
Willy Tarreau0108d902018-11-25 19:14:37 +01001778INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001779
William Lallemand41db4602017-10-30 11:15:51 +01001780static struct action_kw_list http_res_actions = {
1781 .kw = {
1782 { "cache-store", parse_cache_store },
1783 { NULL, NULL }
1784 }
1785};
1786
Willy Tarreau0108d902018-11-25 19:14:37 +01001787INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1788
William Lallemand41db4602017-10-30 11:15:51 +01001789static struct action_kw_list http_req_actions = {
1790 .kw = {
1791 { "cache-use", parse_cache_use },
1792 { NULL, NULL }
1793 }
1794};
1795
Willy Tarreau0108d902018-11-25 19:14:37 +01001796INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1797
Willy Tarreau2231b632019-03-29 18:26:52 +01001798struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001799 .obj_type = OBJ_TYPE_APPLET,
1800 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001801 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001802 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001803};
1804
Willy Tarreaue6552512018-11-26 11:33:13 +01001805/* config parsers for this section */
1806REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02001807REGISTER_POST_CHECK(post_check_cache);
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01001808
1809
1810/* Note: must not be declared <const> as its list will be overwritten */
1811static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
1812 { "res.cache_hit", smp_fetch_res_cache_hit, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
1813 { "res.cache_name", smp_fetch_res_cache_name, 0, NULL, SMP_T_STR, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
1814 { /* END */ },
1815 }
1816};
1817
1818INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);