blob: a73f2668f1ee3f6c9abed0c5eb9c054cf7fbcf96 [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;
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100485 int expires = -1;
486 struct tm tm = {};
487 time_t expires_val = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100488
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200489 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
490 char *value;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100491
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200492 value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
493 if (value) {
494 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100495
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200496 chunk_strncat(chk, value, ctx.value.len - 8 + 1);
497 chunk_strncat(chk, "", 1);
498 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100499 }
500
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200501 value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7);
502 if (value) {
503 struct buffer *chk = get_trash_chunk();
Christopher Faulet5f2c49f2019-07-15 20:49:46 +0200504
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200505 chunk_strncat(chk, value, ctx.value.len - 7 + 1);
506 chunk_strncat(chk, "", 1);
507 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100508 }
509 }
510
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100511 /* Look for Expires header if no s-maxage or max-age Cache-Control data
512 * was found. */
513 if (maxage == -1 && smaxage == -1) {
514 ctx.blk = NULL;
515 if (http_find_header(htx, ist("expires"), &ctx, 1)) {
516 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
517 expires_val = my_timegm(&tm);
518 /* A request having an expiring date earlier
519 * than the current date should be considered as
520 * stale. */
521 expires = (expires_val >= now.tv_sec) ?
522 (expires_val - now.tv_sec) : 0;
523 }
524 else {
525 /* Following RFC 7234#5.3, an invalid date
526 * format must be treated as a date in the past
527 * so the cache entry must be seen as already
528 * expired. */
529 expires = 0;
530 }
531 }
532 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100533
534
535 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100536 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100537
538 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100539 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100540
Remi Tricot-Le Bretona6476112020-10-28 17:52:53 +0100541 if (expires >= 0)
542 return MIN(expires, cache->maxage);
543
William Lallemand49b44532017-11-24 18:53:43 +0100544 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100545
546}
547
548
William Lallemanda400a3a2017-11-20 19:13:12 +0100549static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
550{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200551 struct cache_entry *object = (struct cache_entry *)block->data;
552
553 if (first == block && object->eb.key)
554 eb32_delete(&object->eb);
555 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100556}
557
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200558
559/* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the
560 * date value should be compared to a date determined by in a previous response (for
561 * the same entity). This date could either be the "Last-Modified" value, or the "Date"
562 * value of the response's reception time (by decreasing order of priority). */
563static time_t get_last_modified_time(struct htx *htx)
564{
565 time_t last_modified = 0;
566 struct http_hdr_ctx ctx = { .blk = NULL };
567 struct tm tm = {};
568
569 if (http_find_header(htx, ist("last-modified"), &ctx, 1)) {
570 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
571 last_modified = my_timegm(&tm);
572 }
573 }
574
575 if (!last_modified) {
576 ctx.blk = NULL;
577 if (http_find_header(htx, ist("date"), &ctx, 1)) {
578 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
579 last_modified = my_timegm(&tm);
580 }
581 }
582 }
583
584 /* Fallback on the current time if no "Last-Modified" or "Date" header
585 * was found. */
586 if (!last_modified)
587 last_modified = now.tv_sec;
588
589 return last_modified;
590}
591
William Lallemand41db4602017-10-30 11:15:51 +0100592/*
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500593 * This function will store the headers of the response in a buffer and then
William Lallemand41db4602017-10-30 11:15:51 +0100594 * register a filter to store the data
595 */
596enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200597 struct session *sess, struct stream *s, int flags)
William Lallemand41db4602017-10-30 11:15:51 +0100598{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200599 unsigned int age;
600 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100601 struct http_txn *txn = s->txn;
602 struct http_msg *msg = &txn->rsp;
603 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100604 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100605 struct cache_flt_conf *cconf = rule->arg.act.p[0];
606 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet839791a2019-01-07 16:12:07 +0100607 struct cache_st *cache_ctx = NULL;
608 struct cache_entry *object, *old;
Willy Tarreau8b507582020-02-25 09:35:07 +0100609 unsigned int key = read_u32(txn->cache_hash);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200610 struct htx *htx;
611 struct http_hdr_ctx ctx;
Christopher Fauletb0667472019-09-03 22:22:12 +0200612 size_t hdrs_len = 0;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200613 int32_t pos;
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200614 unsigned int etag_length = 0;
615 unsigned int etag_offset = 0;
616 struct ist header_name = IST_NULL;
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200617 time_t last_modified = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100618
William Lallemand4da3f8a2017-10-31 14:33:34 +0100619 /* Don't cache if the response came from a cache */
620 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
621 s->target == &http_cache_applet.obj_type) {
622 goto out;
623 }
624
625 /* cache only HTTP/1.1 */
626 if (!(txn->req.flags & HTTP_MSGF_VER_11))
627 goto out;
628
Willy Tarreau6905d182019-10-01 17:59:17 +0200629 /* cache only GET method */
630 if (txn->meth != HTTP_METH_GET)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100631 goto out;
632
Willy Tarreauc9036c02019-01-11 19:38:25 +0100633 /* cache key was not computed */
634 if (!key)
635 goto out;
636
William Lallemand4da3f8a2017-10-31 14:33:34 +0100637 /* cache only 200 status code */
638 if (txn->status != 200)
639 goto out;
640
Christopher Faulet839791a2019-01-07 16:12:07 +0100641 /* Find the corresponding filter instance for the current stream */
642 list_for_each_entry(filter, &s->strm_flt.filters, list) {
643 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
644 /* No filter ctx, don't cache anything */
645 if (!filter->ctx)
646 goto out;
647 cache_ctx = filter->ctx;
648 break;
649 }
650 }
651
652 /* from there, cache_ctx is always defined */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200653 htx = htxbuf(&s->res.buf);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100654
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200655 /* Do not cache too big objects. */
656 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
657 htx->data + htx->extra > shctx->max_obj_size)
658 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100659
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200660 /* Does not manage Vary at the moment. We will need a secondary key later for that */
661 ctx.blk = NULL;
662 if (http_find_header(htx, ist("Vary"), &ctx, 0))
663 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100664
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200665 http_check_response_for_cacheability(s, &s->res);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100666
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200667 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
668 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100669
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200670 age = 0;
671 ctx.blk = NULL;
672 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
673 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
674 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
675 hdr_age = CACHE_ENTRY_MAX_AGE;
676 age = hdr_age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100677 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200678 http_remove_header(htx, &ctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100679 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100680
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200681 /* Build a last-modified time that will be stored in the cache_entry and
682 * compared to a future If-Modified-Since client header. */
683 last_modified = get_last_modified_time(htx);
684
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200685 chunk_reset(&trash);
686 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
687 struct htx_blk *blk = htx_get_blk(htx, pos);
688 enum htx_blk_type type = htx_get_blk_type(blk);
689 uint32_t sz = htx_get_blksz(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100690
Christopher Fauletb0667472019-09-03 22:22:12 +0200691 hdrs_len += sizeof(*blk) + sz;
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200692 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
693 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200694
695 /* Look for optional ETag header.
696 * We need to store the offset of the ETag value in order for
697 * future conditional requests to be able to perform ETag
698 * comparisons. */
699 if (type == HTX_BLK_HDR) {
700 header_name = htx_get_blk_name(htx, blk);
701 if (isteq(header_name, ist("etag"))) {
702 etag_length = sz - istlen(header_name);
703 etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name);
704 }
705 }
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200706 if (type == HTX_BLK_EOH)
707 break;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200708 }
709
Christopher Fauletb0667472019-09-03 22:22:12 +0200710 /* Do not cache objects if the headers are too big. */
711 if (hdrs_len > htx->size - global.tune.maxrewrite)
712 goto out;
713
William Lallemand4da3f8a2017-10-31 14:33:34 +0100714 shctx_lock(shctx);
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200715 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100716 if (!first) {
717 shctx_unlock(shctx);
718 goto out;
719 }
720 shctx_unlock(shctx);
721
Willy Tarreau1093a452018-04-06 19:02:25 +0200722 /* the received memory is not initialized, we need at least to mark
723 * the object as not indexed yet.
724 */
725 object = (struct cache_entry *)first->data;
726 object->eb.node.leaf_p = NULL;
727 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200728 object->age = age;
Remi Tricot Le Breton27091b42020-10-23 10:51:27 +0200729 object->last_modified = last_modified;
Willy Tarreau1093a452018-04-06 19:02:25 +0200730
William Lallemand4da3f8a2017-10-31 14:33:34 +0100731 /* reserve space for the cache_entry structure */
732 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200733 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100734 /* cache the headers in a http action because it allows to chose what
735 * to cache, for example you might want to cache a response before
736 * modifying some HTTP headers, or on the contrary after modifying
737 * those headers.
738 */
739
Remi Tricot-Le Bretondbb65b52020-10-22 10:40:04 +0200740 /* Write the ETag information in the cache_entry if needed. */
741 object->etag_length = etag_length;
742 object->etag_offset = etag_offset;
743
William Lallemand4da3f8a2017-10-31 14:33:34 +0100744 /* does not need to be locked because it's in the "hot" list,
745 * copy the headers */
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200746 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
747 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100748
749 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet839791a2019-01-07 16:12:07 +0100750 if (cache_ctx) {
751 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100752
Willy Tarreauc9036c02019-01-11 19:38:25 +0100753 object->eb.key = key;
754
Christopher Faulet839791a2019-01-07 16:12:07 +0100755 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
756 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100757
Christopher Faulet839791a2019-01-07 16:12:07 +0100758 shctx_lock(shctx);
Christopher Faulet95220e22018-12-07 17:34:39 +0100759
Christopher Faulet839791a2019-01-07 16:12:07 +0100760 old = entry_exist(cconf->c.cache, txn->cache_hash);
761 if (old) {
762 eb32_delete(&old->eb);
763 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100764 }
Christopher Faulet839791a2019-01-07 16:12:07 +0100765 shctx_unlock(shctx);
766
767 /* store latest value and expiration time */
768 object->latest_validation = now.tv_sec;
769 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
770 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100771 }
772
773out:
774 /* if does not cache */
775 if (first) {
776 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100777 first->len = 0;
778 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100779 shctx_row_dec_hot(shctx, first);
780 shctx_unlock(shctx);
781 }
782
William Lallemand41db4602017-10-30 11:15:51 +0100783 return ACT_RET_CONT;
784}
785
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100786#define HTX_CACHE_INIT 0 /* Initial state. */
787#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
788#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200789#define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */
790#define HTX_CACHE_END 4 /* Cache entry treatment terminated */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100791
William Lallemandecb73b12017-11-24 14:33:55 +0100792static void http_cache_applet_release(struct appctx *appctx)
793{
Christopher Faulet95220e22018-12-07 17:34:39 +0100794 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100795 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100796 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100797 struct shared_block *first = block_ptr(cache_ptr);
798
799 shctx_lock(shctx_ptr(cache));
800 shctx_row_dec_hot(shctx_ptr(cache), first);
801 shctx_unlock(shctx_ptr(cache));
802}
803
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200804
805static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type,
806 uint32_t info, struct shared_block *shblk, unsigned int offset)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100807{
Christopher Faulet95220e22018-12-07 17:34:39 +0100808 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
809 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200810 struct htx_blk *blk;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200811 char *ptr;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200812 unsigned int max, total;
813 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100814
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200815 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
816 if (!max)
817 return 0;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200818 blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR)
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200819 ? (info & 0xff) + ((info >> 8) & 0xfffff)
820 : info & 0xfffffff);
821 if (blksz > max)
822 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100823
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200824 blk = htx_add_blk(htx, type, blksz);
825 if (!blk)
826 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100827
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200828 blk->info = info;
829 total = 4;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200830 ptr = htx_get_blk_ptr(htx, blk);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200831 while (blksz) {
832 max = MIN(blksz, shctx->block_size - offset);
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200833 memcpy(ptr, (const char *)shblk->data + offset, max);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200834 offset += max;
835 blksz -= max;
836 total += max;
Christopher Faulet15a4ce82019-09-03 22:11:52 +0200837 ptr += max;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200838 if (blksz || offset == shctx->block_size) {
839 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
840 offset = 0;
841 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100842 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200843 appctx->ctx.cache.offset = offset;
844 appctx->ctx.cache.next = shblk;
845 appctx->ctx.cache.sent += total;
846 return total;
847}
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100848
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200849static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx,
850 uint32_t info, struct shared_block *shblk, unsigned int offset)
851{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100852
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200853 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
854 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
855 unsigned int max, total, rem_data;
856 uint32_t blksz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100857
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200858 max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx));
859 if (!max)
860 return 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100861
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200862 rem_data = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200863 if (appctx->ctx.cache.rem_data) {
864 blksz = appctx->ctx.cache.rem_data;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200865 total = 0;
Christopher Fauletbda83972019-06-11 09:58:09 +0200866 }
867 else {
868 blksz = (info & 0xfffffff);
869 total = 4;
870 }
871 if (blksz > max) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200872 rem_data = blksz - max;
873 blksz = max;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100874 }
875
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200876 while (blksz) {
877 size_t sz;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100878
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200879 max = MIN(blksz, shctx->block_size - offset);
880 sz = htx_add_data(htx, ist2(shblk->data + offset, max));
881 offset += sz;
882 blksz -= sz;
883 total += sz;
884 if (sz < max)
885 break;
886 if (blksz || offset == shctx->block_size) {
887 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
888 offset = 0;
889 }
890 }
891
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200892 appctx->ctx.cache.offset = offset;
893 appctx->ctx.cache.next = shblk;
894 appctx->ctx.cache.sent += total;
895 appctx->ctx.cache.rem_data = rem_data + blksz;
896 return total;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100897}
898
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200899static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len,
900 enum htx_blk_type mark)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100901{
Christopher Faulet95220e22018-12-07 17:34:39 +0100902 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
903 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200904 struct shared_block *shblk;
905 unsigned int offset, sz;
906 unsigned int ret, total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100907
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200908 while (len) {
909 enum htx_blk_type type;
910 uint32_t info;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100911
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200912 shblk = appctx->ctx.cache.next;
913 offset = appctx->ctx.cache.offset;
914 if (appctx->ctx.cache.rem_data) {
915 type = HTX_BLK_DATA;
916 info = 0;
917 goto add_data_blk;
918 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100919
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500920 /* Get info of the next HTX block. May be split on 2 shblk */
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200921 sz = MIN(4, shctx->block_size - offset);
922 memcpy((char *)&info, (const char *)shblk->data + offset, sz);
923 offset += sz;
924 if (sz < 4) {
925 shblk = LIST_NEXT(&shblk->list, typeof(shblk), list);
926 memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz);
927 offset = (4 - sz);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100928 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200929
930 /* Get payload of the next HTX block and insert it. */
931 type = (info >> 28);
932 if (type != HTX_BLK_DATA)
933 ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset);
934 else {
935 add_data_blk:
936 ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100937 }
938
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200939 if (!ret)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100940 break;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200941 total += ret;
942 len -= ret;
943
944 if (appctx->ctx.cache.rem_data || type == mark)
945 break;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100946 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100947
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100948 return total;
949}
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200950
951static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx)
952{
953 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
954 unsigned int age;
955 char *end;
956
957 chunk_reset(&trash);
958 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
959 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
960 age = CACHE_ENTRY_MAX_AGE;
961 end = ultoa_o(age, b_head(&trash), b_size(&trash));
962 b_set_data(&trash, end - b_head(&trash));
963 if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash))))
964 return 0;
965 return 1;
966}
967
Christopher Faulet95e7ea32019-07-15 21:01:29 +0200968static void http_cache_io_handler(struct appctx *appctx)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100969{
970 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
971 struct shared_block *first = block_ptr(cache_ptr);
972 struct stream_interface *si = appctx->owner;
973 struct channel *req = si_oc(si);
974 struct channel *res = si_ic(si);
975 struct htx *req_htx, *res_htx;
976 struct buffer *errmsg;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200977 unsigned int len;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100978 size_t ret, total = 0;
979
980 res_htx = htxbuf(&res->buf);
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200981 total = res_htx->data;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100982
983 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
984 goto out;
985
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500986 /* Check if the input buffer is available. */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100987 if (!b_size(&res->buf)) {
988 si_rx_room_blk(si);
989 goto out;
990 }
991
Willy Tarreauefef3232018-12-16 00:37:45 +0100992 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100993 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100994
995 if (appctx->st0 == HTX_CACHE_INIT) {
996 appctx->ctx.cache.next = block_ptr(cache_ptr);
997 appctx->ctx.cache.offset = sizeof(*cache_ptr);
998 appctx->ctx.cache.sent = 0;
Christopher Faulet8f3c2562019-06-03 22:19:18 +0200999 appctx->ctx.cache.rem_data = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001000 appctx->st0 = HTX_CACHE_HEADER;
1001 }
1002
1003 if (appctx->st0 == HTX_CACHE_HEADER) {
1004 /* Headers must be dump at once. Otherwise it is an error */
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001005 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1006 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH);
1007 if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) ||
1008 !htx_cache_add_age_hdr(appctx, res_htx))
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001009 goto error;
1010
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001011 /* In case of a conditional request, we might want to send a
1012 * "304 Not Modified" response instead of the stored data. */
Tim Duesterhuse0142342020-10-22 21:15:06 +02001013 if (appctx->ctx.cache.send_notmodified) {
1014 if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) {
1015 /* If replacing the status code fails we need to send the full response. */
1016 appctx->ctx.cache.send_notmodified = 0;
1017 }
1018 }
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001019
1020 /* Skip response body for HEAD requests or in case of "304 Not
1021 * Modified" response. */
1022 if (si_strm(si)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified)
Christopher Fauletf0dd0372019-02-25 11:08:34 +01001023 appctx->st0 = HTX_CACHE_EOM;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001024 else
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001025 appctx->st0 = HTX_CACHE_DATA;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001026 }
1027
1028 if (appctx->st0 == HTX_CACHE_DATA) {
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001029 len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1030 if (len) {
1031 ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM);
1032 if (ret < len) {
1033 si_rx_room_blk(si);
1034 goto out;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001035 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001036 }
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001037 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001038 }
1039
1040 if (appctx->st0 == HTX_CACHE_EOM) {
Christopher Faulet810df062020-07-22 16:20:34 +02001041 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001042 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1043 si_rx_room_blk(si);
1044 goto out;
1045 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001046 appctx->st0 = HTX_CACHE_END;
1047 }
1048
1049 end:
Christopher Fauletadb36312019-02-25 11:40:49 +01001050 if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001051 res->flags |= CF_READ_NULL;
1052 si_shutr(si);
1053 }
1054
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001055 out:
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001056 total = res_htx->data - total;
Christopher Faulet61123912019-01-02 14:10:01 +01001057 if (total)
1058 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001059 htx_to_buf(res_htx, &res->buf);
Christopher Fauletadb36312019-02-25 11:40:49 +01001060
1061 /* eat the whole request */
1062 if (co_data(req)) {
1063 req_htx = htx_from_buf(&req->buf);
1064 co_htx_skip(req, req_htx, co_data(req));
1065 htx_to_buf(req_htx, &req->buf);
1066 }
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001067 return;
1068
1069 error:
1070 /* Sent and HTTP error 500 */
1071 b_reset(&res->buf);
Christopher Fauletf7346382019-07-17 22:02:08 +02001072 errmsg = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001073 res->buf.data = b_data(errmsg);
1074 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1075 res_htx = htx_from_buf(&res->buf);
1076
Christopher Faulet8f3c2562019-06-03 22:19:18 +02001077 total = 0;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001078 appctx->st0 = HTX_CACHE_END;
1079 goto end;
1080}
1081
1082
Christopher Faulet95220e22018-12-07 17:34:39 +01001083static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001084{
1085 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001086 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001087
Christopher Faulet95220e22018-12-07 17:34:39 +01001088 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001089 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001090 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001091 }
1092
1093 /* check if a cache filter was already registered with this cache
1094 * name, if that's the case, must use it. */
1095 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001096 if (fconf->id == cache_store_flt_id) {
1097 cconf = fconf->conf;
1098 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1099 rule->arg.act.p[0] = cconf;
1100 return 1;
1101 }
William Lallemand41db4602017-10-30 11:15:51 +01001102 }
1103 }
1104
Christopher Faulet95220e22018-12-07 17:34:39 +01001105 /* Create the filter cache config */
1106 cconf = calloc(1, sizeof(*cconf));
1107 if (!cconf) {
1108 memprintf(err, "out of memory\n");
1109 goto err;
1110 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001111 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001112 cconf->c.name = strdup(name);
1113 if (!cconf->c.name) {
1114 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001115 goto err;
1116 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001117
William Lallemand41db4602017-10-30 11:15:51 +01001118 /* register a filter to fill the cache buffer */
1119 fconf = calloc(1, sizeof(*fconf));
1120 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001121 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001122 goto err;
1123 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001124 fconf->id = cache_store_flt_id;
1125 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001126 fconf->ops = &cache_ops;
1127 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1128
Christopher Faulet95220e22018-12-07 17:34:39 +01001129 rule->arg.act.p[0] = cconf;
1130 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001131
Christopher Faulet95220e22018-12-07 17:34:39 +01001132 err:
1133 free(cconf);
1134 return 0;
1135}
1136
1137enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1138 struct act_rule *rule, char **err)
1139{
1140 rule->action = ACT_CUSTOM;
1141 rule->action_ptr = http_action_store_cache;
1142
1143 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1144 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001145
Christopher Faulet95220e22018-12-07 17:34:39 +01001146 (*orig_arg)++;
1147 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001148}
1149
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001150/* This produces a sha1 hash of the concatenation of the HTTP method,
1151 * the first occurrence of the Host header followed by the path component
1152 * if it begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001153int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001154{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001155 struct http_txn *txn = s->txn;
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001156 struct htx *htx = htxbuf(&s->req.buf);
1157 struct htx_sl *sl;
1158 struct http_hdr_ctx ctx;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001159 struct ist uri;
William Lallemandf528fff2017-11-23 19:43:17 +01001160 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001161 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001162
William Lallemandf528fff2017-11-23 19:43:17 +01001163 trash = get_trash_chunk();
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001164 ctx.blk = NULL;
Baptiste Assmanndb92a832019-08-05 16:55:32 +02001165
1166 switch (txn->meth) {
1167 case HTTP_METH_HEAD:
1168 case HTTP_METH_GET:
1169 chunk_memcat(trash, "GET", 3);
1170 break;
1171 default:
1172 return 0;
1173 }
1174
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001175 sl = http_get_stline(htx);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001176 uri = htx_sl_req_uri(sl); // whole uri
1177 if (!uri.len)
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001178 return 0;
Willy Tarreauccc61d82019-10-17 09:28:28 +02001179
1180 /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'),
1181 * unless haproxy is deployed in front of an outbound cache. In HTTP/2,
1182 * URIs are almost always sent in absolute form with their scheme. In
1183 * this case, the scheme is almost always "https". In order to support
1184 * sharing of cache objects between H1 and H2, we'll hash the absolute
1185 * URI whenever known, or prepend "https://" + the Host header for
1186 * relative URIs. The difference will only appear on absolute HTTP/1
1187 * requests sent to an origin server, which practically is never met in
1188 * the real world so we don't care about the ability to share the same
1189 * key here.URIs are normalized from the absolute URI to an origin form as
1190 * well.
1191 */
1192 if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) {
Willy Tarreau20020ae2019-10-29 13:02:15 +01001193 chunk_istcat(trash, ist("https://"));
Willy Tarreauccc61d82019-10-17 09:28:28 +02001194 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1195 return 0;
Willy Tarreau20020ae2019-10-29 13:02:15 +01001196 chunk_istcat(trash, ctx.value);
Willy Tarreauccc61d82019-10-17 09:28:28 +02001197 }
1198
1199 chunk_memcat(trash, uri.ptr, uri.len);
William Lallemandf528fff2017-11-23 19:43:17 +01001200
1201 /* hash everything */
1202 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001203 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001204 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1205
1206 return 1;
1207}
1208
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001209/* Looks for "If-None-Match" headers in the request and compares their value
1210 * with the one that might have been stored in the cache_entry. If any of them
1211 * matches, a "304 Not Modified" response should be sent instead of the cached
1212 * data.
1213 * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001214 * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2).
1215 *
1216 * If no "If-None-Match" header was found, look for an "If-Modified-Since"
1217 * header and compare its value (date) to the one stored in the cache_entry.
1218 * If the request's date is later than the cached one, we also send a
1219 * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2).
1220 *
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001221 * Returns 1 if "304 Not Modified" should be sent, 0 otherwise.
1222 */
1223static int should_send_notmodified_response(struct cache *cache, struct htx *htx,
1224 struct cache_entry *entry)
1225{
1226 int retval = 0;
1227
1228 struct http_hdr_ctx ctx = { .blk = NULL };
1229 struct ist cache_entry_etag = IST_NULL;
1230 struct buffer *etag_buffer = NULL;
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001231 int if_none_match_found = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001232
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001233 struct tm tm = {};
1234 time_t if_modified_since = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001235
1236 /* If we find a "If-None-Match" header in the request, rebuild the
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001237 * cache_entry's ETag in order to perform comparisons.
1238 * There could be multiple "if-none-match" header lines. */
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001239 while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) {
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001240 if_none_match_found = 1;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001241
1242 /* A '*' matches everything. */
1243 if (isteq(ctx.value, ist("*")) != 0) {
1244 retval = 1;
1245 break;
1246 }
1247
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001248 /* No need to rebuild an etag if none was stored in the cache. */
1249 if (entry->etag_length == 0)
1250 break;
1251
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001252 /* Rebuild the stored ETag. */
1253 if (etag_buffer == NULL) {
1254 etag_buffer = get_trash_chunk();
1255
1256 if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry),
1257 (unsigned char*)b_orig(etag_buffer),
1258 entry->etag_offset, entry->etag_length) == 0) {
1259 cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length);
1260 } else {
1261 /* We could not rebuild the ETag in one go, we
1262 * won't send a "304 Not Modified" response. */
1263 break;
1264 }
1265 }
1266
1267 if (http_compare_etags(cache_entry_etag, ctx.value) == 1) {
1268 retval = 1;
1269 break;
1270 }
1271 }
1272
Remi Tricot-Le Breton53161d82020-10-23 10:51:28 +02001273 /* If the request did not contain an "If-None-Match" header, we look for
1274 * an "If-Modified-Since" header (see RFC 7232#3.3). */
1275 if (retval == 0 && if_none_match_found == 0) {
1276 ctx.blk = NULL;
1277 if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) {
1278 if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) {
1279 if_modified_since = my_timegm(&tm);
1280
1281 /* We send a "304 Not Modified" response if the
1282 * entry's last modified date is earlier than
1283 * the one found in the "If-Modified-Since"
1284 * header. */
1285 retval = (entry->last_modified <= if_modified_since);
1286 }
1287 }
1288 }
1289
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001290 return retval;
1291}
1292
William Lallemand41db4602017-10-30 11:15:51 +01001293enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1294 struct session *sess, struct stream *s, int flags)
1295{
William Lallemand77c11972017-10-31 20:43:01 +01001296
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001297 struct http_txn *txn = s->txn;
William Lallemand77c11972017-10-31 20:43:01 +01001298 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001299 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1300 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001301
Willy Tarreau6905d182019-10-01 17:59:17 +02001302 /* Ignore cache for HTTP/1.0 requests and for requests other than GET
1303 * and HEAD */
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001304 if (!(txn->req.flags & HTTP_MSGF_VER_11) ||
Willy Tarreau6905d182019-10-01 17:59:17 +02001305 (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD))
Christopher Fauletb3d4bca2019-02-25 10:59:33 +01001306 txn->flags |= TX_CACHE_IGNORE;
1307
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001308 http_check_request_for_cacheability(s, &s->req);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001309
Willy Tarreau504455c2017-12-22 17:47:35 +01001310 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1311 return ACT_RET_CONT;
1312
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001313 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001314 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001315
Willy Tarreau504455c2017-12-22 17:47:35 +01001316 if (s->txn->flags & TX_CACHE_IGNORE)
1317 return ACT_RET_CONT;
1318
Willy Tarreaua1214a52018-12-14 14:00:25 +01001319 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001320 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001321 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001322 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001323
William Lallemanda400a3a2017-11-20 19:13:12 +01001324 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001325 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001326 if (res) {
1327 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001328 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1329 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001330 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001331 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulet95e7ea32019-07-15 21:01:29 +02001332 appctx->st0 = HTX_CACHE_INIT;
William Lallemand77c11972017-10-31 20:43:01 +01001333 appctx->rule = rule;
1334 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001335 appctx->ctx.cache.next = NULL;
1336 appctx->ctx.cache.sent = 0;
Remi Tricot-Le Breton6cb10382020-10-22 10:40:05 +02001337 appctx->ctx.cache.send_notmodified =
1338 should_send_notmodified_response(cache, htxbuf(&s->req.buf), res);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001339
1340 if (px == strm_fe(s))
Olivier Houchardaa090d42019-03-08 18:49:24 +01001341 _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001342 else
Olivier Houchardaa090d42019-03-08 18:49:24 +01001343 _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001344 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001345 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001346 shctx_lock(shctx_ptr(cache));
1347 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1348 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001349 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001350 }
1351 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001352 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001353 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001354}
1355
1356
1357enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1358 struct act_rule *rule, char **err)
1359{
William Lallemand41db4602017-10-30 11:15:51 +01001360 rule->action = ACT_CUSTOM;
1361 rule->action_ptr = http_action_req_cache_use;
1362
Christopher Faulet95220e22018-12-07 17:34:39 +01001363 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001364 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001365
1366 (*orig_arg)++;
1367 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001368}
1369
1370int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1371{
1372 int err_code = 0;
1373
1374 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1375
1376 if (!*args[1]) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001377 ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001378 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001379 err_code |= ERR_ALERT | ERR_ABORT;
1380 goto out;
1381 }
1382
1383 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1384 err_code |= ERR_ABORT;
1385 goto out;
1386 }
1387
1388 if (tmp_cache_config == NULL) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001389 struct cache *cache_config;
1390
William Lallemand41db4602017-10-30 11:15:51 +01001391 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1392 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001393 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001394 err_code |= ERR_ALERT | ERR_ABORT;
1395 goto out;
1396 }
1397
1398 strlcpy2(tmp_cache_config->id, args[1], 33);
1399 if (strlen(args[1]) > 32) {
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001400 ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001401 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001402 err_code |= ERR_WARN;
1403 }
Tim Duesterhusff4d86b2020-08-18 22:20:27 +02001404
1405 list_for_each_entry(cache_config, &caches_config, list) {
1406 if (strcmp(tmp_cache_config->id, cache_config->id) == 0) {
1407 ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n",
1408 file, linenum, tmp_cache_config->id);
1409 err_code |= ERR_ALERT | ERR_ABORT;
1410 goto out;
1411 }
1412 }
1413
William Lallemand49b44532017-11-24 18:53:43 +01001414 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001415 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001416 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001417 }
1418 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001419 unsigned long int maxsize;
1420 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001421
1422 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1423 err_code |= ERR_ABORT;
1424 goto out;
1425 }
1426
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001427 maxsize = strtoul(args[1], &err, 10);
1428 if (err == args[1] || *err != '\0') {
1429 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1430 file, linenum, args[1]);
1431 err_code |= ERR_ABORT;
1432 goto out;
1433 }
1434
1435 if (maxsize > (UINT_MAX >> 20)) {
1436 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1437 file, linenum, args[1], UINT_MAX >> 20);
1438 err_code |= ERR_ABORT;
1439 goto out;
1440 }
1441
William Lallemand41db4602017-10-30 11:15:51 +01001442 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001443 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001444 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001445 } else if (strcmp(args[0], "max-age") == 0) {
1446 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1447 err_code |= ERR_ABORT;
1448 goto out;
1449 }
1450
1451 if (!*args[1]) {
1452 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1453 file, linenum, args[0]);
1454 err_code |= ERR_WARN;
1455 }
1456
1457 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001458 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001459 unsigned int maxobjsz;
1460 char *err;
1461
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001462 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1463 err_code |= ERR_ABORT;
1464 goto out;
1465 }
1466
1467 if (!*args[1]) {
1468 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1469 file, linenum, args[0]);
1470 err_code |= ERR_WARN;
1471 }
1472
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001473 maxobjsz = strtoul(args[1], &err, 10);
1474 if (err == args[1] || *err != '\0') {
1475 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1476 file, linenum, args[1]);
1477 err_code |= ERR_ABORT;
1478 goto out;
1479 }
1480 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001481 }
1482 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001483 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001484 err_code |= ERR_ALERT | ERR_FATAL;
1485 goto out;
1486 }
1487out:
1488 return err_code;
1489}
1490
1491/* once the cache section is parsed */
1492
1493int cfg_post_parse_section_cache()
1494{
William Lallemand41db4602017-10-30 11:15:51 +01001495 int err_code = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001496
1497 if (tmp_cache_config) {
William Lallemand41db4602017-10-30 11:15:51 +01001498
1499 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001500 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001501 err_code |= ERR_FATAL | ERR_ALERT;
1502 goto out;
1503 }
1504
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001505 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001506 /* Default max. file size is a 256th of the cache size. */
1507 tmp_cache_config->maxobjsz =
1508 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001509 }
1510 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1511 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1512 err_code |= ERR_FATAL | ERR_ALERT;
1513 goto out;
1514 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001515
William Lallemandd1d1e222019-08-28 15:22:49 +02001516 /* add to the list of cache to init and reinit tmp_cache_config
1517 * for next cache section, if any.
1518 */
1519 LIST_ADDQ(&caches_config, &tmp_cache_config->list);
1520 tmp_cache_config = NULL;
1521 return err_code;
1522 }
1523out:
1524 free(tmp_cache_config);
1525 tmp_cache_config = NULL;
1526 return err_code;
1527
1528}
1529
1530int post_check_cache()
1531{
1532 struct proxy *px;
1533 struct cache *back, *cache_config, *cache;
1534 struct shared_context *shctx;
1535 int ret_shctx;
1536 int err_code = 0;
1537
1538 list_for_each_entry_safe(cache_config, back, &caches_config, list) {
1539
1540 ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE,
1541 cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001542
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001543 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001544 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001545 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001546 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001547 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001548
1549 err_code |= ERR_FATAL | ERR_ALERT;
1550 goto out;
1551 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001552 shctx->free_block = cache_free_blocks;
William Lallemandd1d1e222019-08-28 15:22:49 +02001553 /* the cache structure is stored in the shctx and added to the
1554 * caches list, we can remove the entry from the caches_config
1555 * list */
1556 memcpy(shctx->data, cache_config, sizeof(struct cache));
William Lallemand41db4602017-10-30 11:15:51 +01001557 cache = (struct cache *)shctx->data;
1558 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001559 LIST_ADDQ(&caches, &cache->list);
William Lallemandd1d1e222019-08-28 15:22:49 +02001560 LIST_DEL(&cache_config->list);
1561 free(cache_config);
1562
1563 /* Find all references for this cache in the existing filters
1564 * (over all proxies) and reference it in matching filters.
1565 */
1566 for (px = proxies_list; px; px = px->next) {
1567 struct flt_conf *fconf;
1568 struct cache_flt_conf *cconf;
1569
1570 list_for_each_entry(fconf, &px->filter_configs, list) {
1571 if (fconf->id != cache_store_flt_id)
1572 continue;
1573
1574 cconf = fconf->conf;
1575 if (!strcmp(cache->id, cconf->c.name)) {
1576 free(cconf->c.name);
Tim Duesterhusd7c6e6a2020-09-14 18:01:33 +02001577 cconf->flags |= CACHE_FLT_INIT;
William Lallemandd1d1e222019-08-28 15:22:49 +02001578 cconf->c.cache = cache;
1579 break;
1580 }
1581 }
1582 }
William Lallemand41db4602017-10-30 11:15:51 +01001583 }
William Lallemandd1d1e222019-08-28 15:22:49 +02001584
William Lallemand41db4602017-10-30 11:15:51 +01001585out:
William Lallemand41db4602017-10-30 11:15:51 +01001586 return err_code;
1587
William Lallemand41db4602017-10-30 11:15:51 +01001588}
1589
William Lallemand41db4602017-10-30 11:15:51 +01001590struct flt_ops cache_ops = {
1591 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001592 .check = cache_store_check,
1593 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001594
Christopher Faulet65554e12020-03-06 14:52:06 +01001595 /* Handle stream init/deinit */
1596 .attach = cache_store_strm_init,
1597 .detach = cache_store_strm_deinit,
1598
William Lallemand4da3f8a2017-10-31 14:33:34 +01001599 /* Handle channels activity */
Christopher Faulet839791a2019-01-07 16:12:07 +01001600 .channel_post_analyze = cache_store_post_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001601
1602 /* Filter HTTP requests and responses */
1603 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001604 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001605 .http_end = cache_store_http_end,
William Lallemand41db4602017-10-30 11:15:51 +01001606};
1607
Christopher Faulet99a17a22018-12-11 09:18:27 +01001608
1609
1610static int
1611parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1612 struct flt_conf *fconf, char **err, void *private)
1613{
1614 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001615 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001616 char *name = NULL;
1617 int pos = *cur_arg;
1618
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001619 /* Get the cache filter name. <pos> point on "cache" keyword */
1620 if (!*args[pos + 1]) {
Tim Duesterhusea969f62020-08-18 22:06:51 +02001621 memprintf(err, "%s : expects a <name> argument", args[pos]);
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001622 goto error;
1623 }
1624 name = strdup(args[pos + 1]);
1625 if (!name) {
1626 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1627 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001628 }
Christopher Faulet2a37cdb2020-05-18 11:58:16 +02001629 pos += 2;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001630
1631 /* Check if an implicit filter with the same name already exists. If so,
1632 * we remove the implicit filter to use the explicit one. */
1633 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1634 if (f->id != cache_store_flt_id)
1635 continue;
1636
1637 cconf = f->conf;
1638 if (strcmp(name, cconf->c.name)) {
1639 cconf = NULL;
1640 continue;
1641 }
1642
1643 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1644 cconf = NULL;
1645 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1646 px->id, name);
Tim Duesterhusd34b1ce2020-01-18 01:46:18 +01001647 goto error;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001648 }
1649
1650 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1651 LIST_DEL(&f->list);
1652 free(f);
1653 free(name);
1654 break;
1655 }
1656
1657 /* No implicit cache filter found, create configuration for the explicit one */
1658 if (!cconf) {
1659 cconf = calloc(1, sizeof(*cconf));
1660 if (!cconf) {
1661 memprintf(err, "%s: out of memory", args[*cur_arg]);
1662 goto error;
1663 }
1664 cconf->c.name = name;
1665 }
1666
1667 cconf->flags = 0;
1668 fconf->id = cache_store_flt_id;
1669 fconf->conf = cconf;
1670 fconf->ops = &cache_ops;
1671
1672 *cur_arg = pos;
1673 return 0;
1674
1675 error:
1676 free(name);
1677 free(cconf);
1678 return -1;
1679}
1680
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001681static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001682{
1683 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1684 return 1;
1685
1686 return 0;
1687}
1688
1689static int cli_io_handler_show_cache(struct appctx *appctx)
1690{
1691 struct cache* cache = appctx->ctx.cli.p0;
1692 struct stream_interface *si = appctx->owner;
1693
William Lallemand1f49a362017-11-21 20:01:26 +01001694 if (cache == NULL) {
1695 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1696 }
1697
1698 list_for_each_entry_from(cache, &caches, list) {
1699 struct eb32_node *node = NULL;
1700 unsigned int next_key;
1701 struct cache_entry *entry;
1702
William Lallemand1f49a362017-11-21 20:01:26 +01001703 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001704 if (!next_key) {
1705 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1706 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001707 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001708 return 0;
1709 }
1710 }
William Lallemand1f49a362017-11-21 20:01:26 +01001711
1712 appctx->ctx.cli.p0 = cache;
1713
1714 while (1) {
1715
1716 shctx_lock(shctx_ptr(cache));
1717 node = eb32_lookup_ge(&cache->entries, next_key);
1718 if (!node) {
1719 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001720 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001721 break;
1722 }
1723
1724 entry = container_of(node, struct cache_entry, eb);
Willy Tarreau8b507582020-02-25 09:35:07 +01001725 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 +01001726
1727 next_key = node->key + 1;
1728 appctx->ctx.cli.i0 = next_key;
1729
1730 shctx_unlock(shctx_ptr(cache));
1731
1732 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001733 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001734 return 0;
1735 }
1736 }
1737
1738 }
1739
1740 return 1;
1741
1742}
1743
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01001744
1745/*
1746 * boolean, returns true if response was built out of a cache entry.
1747 */
1748static int
1749smp_fetch_res_cache_hit(const struct arg *args, struct sample *smp,
1750 const char *kw, void *private)
1751{
1752 smp->data.type = SMP_T_BOOL;
1753 smp->data.u.sint = (smp->strm ? (smp->strm->target == &http_cache_applet.obj_type) : 0);
1754
1755 return 1;
1756}
1757
1758/*
1759 * string, returns cache name (if response came from a cache).
1760 */
1761static int
1762smp_fetch_res_cache_name(const struct arg *args, struct sample *smp,
1763 const char *kw, void *private)
1764{
1765 struct appctx *appctx = NULL;
1766
1767 struct cache_flt_conf *cconf = NULL;
1768 struct cache *cache = NULL;
1769
1770 if (!smp->strm || smp->strm->target != &http_cache_applet.obj_type)
1771 return 0;
1772
1773 /* Get appctx from the stream_interface. */
1774 appctx = si_appctx(&smp->strm->si[1]);
1775 if (appctx && appctx->rule) {
1776 cconf = appctx->rule->arg.act.p[0];
1777 if (cconf) {
1778 cache = cconf->c.cache;
1779
1780 smp->data.type = SMP_T_STR;
1781 smp->flags = SMP_F_CONST;
1782 smp->data.u.str.area = cache->id;
1783 smp->data.u.str.data = strlen(cache->id);
1784 return 1;
1785 }
1786 }
1787
1788 return 0;
1789}
1790
Christopher Faulet99a17a22018-12-11 09:18:27 +01001791/* Declare the filter parser for "cache" keyword */
1792static struct flt_kw_list filter_kws = { "CACHE", { }, {
1793 { "cache", parse_cache_flt, NULL },
1794 { NULL, NULL, NULL },
1795 }
1796};
1797
1798INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1799
William Lallemand1f49a362017-11-21 20:01:26 +01001800static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001801 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1802 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001803}};
1804
Willy Tarreau0108d902018-11-25 19:14:37 +01001805INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001806
William Lallemand41db4602017-10-30 11:15:51 +01001807static struct action_kw_list http_res_actions = {
1808 .kw = {
1809 { "cache-store", parse_cache_store },
1810 { NULL, NULL }
1811 }
1812};
1813
Willy Tarreau0108d902018-11-25 19:14:37 +01001814INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1815
William Lallemand41db4602017-10-30 11:15:51 +01001816static struct action_kw_list http_req_actions = {
1817 .kw = {
1818 { "cache-use", parse_cache_use },
1819 { NULL, NULL }
1820 }
1821};
1822
Willy Tarreau0108d902018-11-25 19:14:37 +01001823INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1824
Willy Tarreau2231b632019-03-29 18:26:52 +01001825struct applet http_cache_applet = {
William Lallemand41db4602017-10-30 11:15:51 +01001826 .obj_type = OBJ_TYPE_APPLET,
1827 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001828 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001829 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001830};
1831
Willy Tarreaue6552512018-11-26 11:33:13 +01001832/* config parsers for this section */
1833REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
William Lallemandd1d1e222019-08-28 15:22:49 +02001834REGISTER_POST_CHECK(post_check_cache);
Remi Tricot-Le Bretonbf971212020-10-27 11:55:57 +01001835
1836
1837/* Note: must not be declared <const> as its list will be overwritten */
1838static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
1839 { "res.cache_hit", smp_fetch_res_cache_hit, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
1840 { "res.cache_name", smp_fetch_res_cache_name, 0, NULL, SMP_T_STR, SMP_USE_HRSHP, SMP_VAL_RESPONSE },
1841 { /* END */ },
1842 }
1843};
1844
1845INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);