blob: 834efc1848b921f7683f1d53dd1f20d975c9ad34 [file] [log] [blame]
William Lallemand41db4602017-10-30 11:15:51 +01001/*
2 * Cache management
3 *
4 * Copyright 2017 HAProxy Technologies
5 * William Lallemand <wlallemand@haproxy.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
William Lallemand41db4602017-10-30 11:15:51 +010013#include <eb32tree.h>
William Lallemandf528fff2017-11-23 19:43:17 +010014#include <import/sha1.h>
William Lallemand41db4602017-10-30 11:15:51 +010015
William Lallemand75d93292017-11-21 20:01:24 +010016#include <types/action.h>
William Lallemand1f49a362017-11-21 20:01:26 +010017#include <types/cli.h>
William Lallemand75d93292017-11-21 20:01:24 +010018#include <types/filters.h>
19#include <types/proxy.h>
20#include <types/shctx.h>
21
William Lallemand41db4602017-10-30 11:15:51 +010022#include <proto/channel.h>
William Lallemand1f49a362017-11-21 20:01:26 +010023#include <proto/cli.h>
William Lallemand41db4602017-10-30 11:15:51 +010024#include <proto/proxy.h>
25#include <proto/hdr_idx.h>
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010026#include <proto/http_htx.h>
27#include <proto/htx.h>
William Lallemand41db4602017-10-30 11:15:51 +010028#include <proto/filters.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020029#include <proto/http_rules.h>
William Lallemand41db4602017-10-30 11:15:51 +010030#include <proto/proto_http.h>
31#include <proto/log.h>
32#include <proto/stream.h>
33#include <proto/stream_interface.h>
34#include <proto/shctx.h>
35
William Lallemand41db4602017-10-30 11:15:51 +010036
37#include <common/cfgparse.h>
38#include <common/hash.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010039#include <common/initcall.h>
William Lallemand41db4602017-10-30 11:15:51 +010040
41/* flt_cache_store */
Christopher Faulet1f672c52018-12-03 14:30:41 +010042#define CACHE_F_LEGACY_HTTP 0x00000001 /* The cache is used to store raw HTTP
43 * messages (legacy implementation) */
44#define CACHE_F_HTX 0x00000002 /* The cache is used to store HTX messages */
William Lallemand41db4602017-10-30 11:15:51 +010045
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010046const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010047
48struct applet http_cache_applet;
49
50struct flt_ops cache_ops;
51
52struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010053 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010054 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010055 unsigned int maxage; /* max-age */
56 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020057 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010058 char id[33]; /* cache name */
Christopher Faulet1f672c52018-12-03 14:30:41 +010059 unsigned int flags; /* CACHE_F_* */
William Lallemand41db4602017-10-30 11:15:51 +010060};
61
Christopher Faulet95220e22018-12-07 17:34:39 +010062/* cache config for filters */
63struct cache_flt_conf {
64 union {
65 struct cache *cache; /* cache used by the filter */
66 char *name; /* cache name used during conf parsing */
67 } c;
68 unsigned int flags; /* CACHE_FLT_F_* */
69};
70
William Lallemand41db4602017-10-30 11:15:51 +010071/*
72 * cache ctx for filters
73 */
74struct cache_st {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010075 int hdrs_len; // field used in legacy mode only
William Lallemand41db4602017-10-30 11:15:51 +010076 struct shared_block *first_block;
77};
78
79struct cache_entry {
80 unsigned int latest_validation; /* latest validation date */
81 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020082 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010083 unsigned int eoh; /* Origin server end of headers offset. */ // field used in legacy mode only
84
85 unsigned int hdrs_len; // field used in HTX mode only
86 unsigned int data_len; // field used in HTX mode only
87
William Lallemand41db4602017-10-30 11:15:51 +010088 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010089 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010090 unsigned char data[0];
91};
92
93#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010094#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010095
96static struct list caches = LIST_HEAD_INIT(caches);
97static 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
William Lallemandf528fff2017-11-23 19:43:17 +0100106 node = eb32_lookup(&cache->entries, (*(unsigned int *)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
150 free(cconf);
151}
152
William Lallemand4da3f8a2017-10-31 14:33:34 +0100153static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100154cache_store_check(struct proxy *px, struct flt_conf *fconf)
155{
156 struct cache_flt_conf *cconf = fconf->conf;
157 struct cache *cache;
158
159 /* resolve the cache name to a ptr in the filter config */
160 list_for_each_entry(cache, &caches, list) {
161 if (!strcmp(cache->id, cconf->c.name)) {
162 /* there can be only one filter per cache, so we free it there */
163 cache->flags |= ((px->options2 & PR_O2_USE_HTX)
164 ? CACHE_F_HTX
165 : CACHE_F_LEGACY_HTTP);
166
167 free(cconf->c.name);
168 cconf->c.cache = cache;
169 goto found;
170 }
171 }
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:
178 return 0;
179}
180
181static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100182cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
183{
184 if (!(chn->flags & CF_ISRESP))
185 return 1;
186
187 if (filter->ctx == NULL) {
188 struct cache_st *st;
189
Willy Tarreaubafbe012017-11-24 17:34:44 +0100190 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100191 if (st == NULL)
192 return -1;
193
194 st->hdrs_len = 0;
195 st->first_block = NULL;
196 filter->ctx = st;
197 }
198
William Lallemand4da3f8a2017-10-31 14:33:34 +0100199 return 1;
200}
201
202static int
William Lallemand49dc0482017-11-24 14:33:54 +0100203cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
204{
205 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100206 struct cache_flt_conf *cconf = FLT_CONF(filter);
207 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100208 struct shared_context *shctx = shctx_ptr(cache);
209
210 if (!(chn->flags & CF_ISRESP))
211 return 1;
212
213 /* Everything should be released in the http_end filter, but we need to do it
214 * there too, in case of errors */
215
216 if (st && st->first_block) {
217
218 shctx_lock(shctx);
219 shctx_row_dec_hot(shctx, st->first_block);
220 shctx_unlock(shctx);
221
222 }
223 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100224 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100225 filter->ctx = NULL;
226 }
227
228 return 1;
229}
230
231
232static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100233cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
234{
235 struct cache_st *st = filter->ctx;
236
William Lallemand4da3f8a2017-10-31 14:33:34 +0100237 if (!(msg->chn->flags & CF_ISRESP) || !st)
238 return 1;
239
Christopher Faulet67658c92018-12-06 21:59:39 +0100240 if (st->first_block) {
241 register_data_filter(s, msg->chn, filter);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100242 if (!IS_HTX_STRM(s))
243 st->hdrs_len = msg->sov;
Christopher Faulet67658c92018-12-06 21:59:39 +0100244 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100245 return 1;
246}
247
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200248static inline void disable_cache_entry(struct cache_st *st,
249 struct filter *filter, struct shared_context *shctx)
250{
251 struct cache_entry *object;
252
253 object = (struct cache_entry *)st->first_block->data;
254 filter->ctx = NULL; /* disable cache */
255 shctx_lock(shctx);
256 shctx_row_dec_hot(shctx, st->first_block);
257 object->eb.key = 0;
258 shctx_unlock(shctx);
259 pool_free(pool_head_cache_st, st);
260}
261
William Lallemand4da3f8a2017-10-31 14:33:34 +0100262static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100263cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
264 unsigned int offset, unsigned int len)
265{
Christopher Faulet95220e22018-12-07 17:34:39 +0100266 struct cache_flt_conf *cconf = FLT_CONF(filter);
267 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100268 struct cache_st *st = filter->ctx;
269 struct htx *htx = htxbuf(&msg->chn->buf);
270 struct htx_blk *blk;
271 struct htx_ret htx_ret;
272 struct cache_entry *object;
273 int ret, to_forward = 0;
274
275 if (!len)
276 return len;
277
278 if (!st->first_block) {
279 unregister_data_filter(s, msg->chn, filter);
280 return len;
281 }
282 object = (struct cache_entry *)st->first_block->data;
283
284 htx_ret = htx_find_blk(htx, offset);
285 blk = htx_ret.blk;
286 offset = htx_ret.ret;
287
288 while (blk && len) {
289 struct shared_block *fb;
290 enum htx_blk_type type = htx_get_blk_type(blk);
291 uint32_t sz = htx_get_blksz(blk);
292 struct ist v;
293
294 switch (type) {
295 case HTX_BLK_UNUSED:
296 break;
297
298 case HTX_BLK_DATA:
299 case HTX_BLK_TLR:
300 v = htx_get_blk_value(htx, blk);
301 v.ptr += offset;
302 v.len -= offset;
303 if (v.len > len)
304 v.len = len;
305
306 shctx_lock(shctx);
307 fb = shctx_row_reserve_hot(shctx, st->first_block, v.len);
308 if (!fb) {
309 shctx_unlock(shctx);
310 goto no_cache;
311 }
312 shctx_unlock(shctx);
313
314 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
315 (unsigned char *)v.ptr, v.len);
316 if (ret < 0)
317 goto no_cache;
318
319 if (type == HTX_BLK_DATA)
320 object->data_len += v.len;
321 to_forward += v.len;
322 len -= v.len;
323 break;
324
325 default:
326 sz -= offset;
327 if (sz > len)
328 sz = len;
329 to_forward += sz;
330 len -= sz;
331 break;
332 }
333
334 offset = 0;
335 blk = htx_get_next_blk(htx, blk);
336 }
337
338 return to_forward;
339
340 no_cache:
341 disable_cache_entry(st, filter, shctx);
342 unregister_data_filter(s, msg->chn, filter);
343 return len;
344}
345
346static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100347cache_store_http_forward_data(struct stream *s, struct filter *filter,
348 struct http_msg *msg, unsigned int len)
349{
350 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100351 struct cache_flt_conf *cconf = FLT_CONF(filter);
352 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100353 int ret;
354
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200355 ret = 0;
356
William Lallemand4da3f8a2017-10-31 14:33:34 +0100357 /*
358 * We need to skip the HTTP headers first, because we saved them in the
359 * http-response action.
360 */
Christopher Faulet67658c92018-12-06 21:59:39 +0100361 if (!(msg->chn->flags & CF_ISRESP) || !st) {
362 /* should never happen */
363 unregister_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100364 return len;
Christopher Faulet67658c92018-12-06 21:59:39 +0100365 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100366
367 if (!len) {
Joseph Herlant8dae5b32018-11-15 14:07:53 -0800368 /* Nothing to forward */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100369 ret = len;
370 }
William Lallemand10935bc2017-11-14 14:39:23 +0100371 else if (st->hdrs_len >= len) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100372 /* Forward part of headers */
373 ret = len;
374 st->hdrs_len -= len;
375 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100376 else {
William Lallemand10935bc2017-11-14 14:39:23 +0100377 /* Forward data */
Christopher Faulet67658c92018-12-06 21:59:39 +0100378 if (st->first_block) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200379 int to_append, append;
380 struct shared_block *fb;
381
382 to_append = MIN(ci_contig_data(msg->chn), len - st->hdrs_len);
383
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200384 shctx_lock(shctx);
385 fb = shctx_row_reserve_hot(shctx, st->first_block, to_append);
386 if (!fb) {
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100387 shctx_unlock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200388 disable_cache_entry(st, filter, shctx);
Christopher Faulet67658c92018-12-06 21:59:39 +0100389 unregister_data_filter(s, msg->chn, filter);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200390 return len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100391 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200392 shctx_unlock(shctx);
393
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200394 /* Skip remaining headers to fill the cache */
395 c_adv(msg->chn, st->hdrs_len);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200396 append = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
397 (unsigned char *)ci_head(msg->chn), to_append);
398 ret = st->hdrs_len + to_append - append;
399 /* Rewind the buffer to forward all data */
400 c_rew(msg->chn, st->hdrs_len);
401 st->hdrs_len = 0;
Christopher Faulet67658c92018-12-06 21:59:39 +0100402 if (ret < 0) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200403 disable_cache_entry(st, filter, shctx);
Christopher Faulet67658c92018-12-06 21:59:39 +0100404 unregister_data_filter(s, msg->chn, filter);
405 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100406 }
Christopher Faulet67658c92018-12-06 21:59:39 +0100407 else {
408 /* should never happen */
409 unregister_data_filter(s, msg->chn, filter);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200410 ret = len;
Christopher Faulet67658c92018-12-06 21:59:39 +0100411 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100412 }
413
414 if ((ret != len) ||
415 (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret))
416 task_wakeup(s->task, TASK_WOKEN_MSG);
417
418 return ret;
419}
420
421static int
422cache_store_http_end(struct stream *s, struct filter *filter,
423 struct http_msg *msg)
424{
425 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100426 struct cache_flt_conf *cconf = FLT_CONF(filter);
427 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100428 struct shared_context *shctx = shctx_ptr(cache);
429 struct cache_entry *object;
430
431 if (!(msg->chn->flags & CF_ISRESP))
432 return 1;
433
434 if (st && st->first_block) {
435
436 object = (struct cache_entry *)st->first_block->data;
437
438 /* does not need to test if the insertion worked, if it
439 * doesn't, the blocks will be reused anyway */
440
441 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100442 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
443 object->eb.key = 0;
444 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100445 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100446 shctx_row_dec_hot(shctx, st->first_block);
447 shctx_unlock(shctx);
448
449 }
450 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100451 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100452 filter->ctx = NULL;
453 }
454
455 return 1;
456}
457
458 /*
459 * This intends to be used when checking HTTP headers for some
460 * word=value directive. Return a pointer to the first character of value, if
461 * the word was not found or if there wasn't any value assigned ot it return NULL
462 */
463char *directive_value(const char *sample, int slen, const char *word, int wlen)
464{
465 int st = 0;
466
467 if (slen < wlen)
468 return 0;
469
470 while (wlen) {
471 char c = *sample ^ *word;
472 if (c && c != ('A' ^ 'a'))
473 return NULL;
474 sample++;
475 word++;
476 slen--;
477 wlen--;
478 }
479
480 while (slen) {
481 if (st == 0) {
482 if (*sample != '=')
483 return NULL;
484 sample++;
485 slen--;
486 st = 1;
487 continue;
488 } else {
489 return (char *)sample;
490 }
491 }
492
493 return NULL;
494}
495
496/*
497 * Return the maxage in seconds of an HTTP response.
498 * Compute the maxage using either:
499 * - the assigned max-age of the cache
500 * - the s-maxage directive
501 * - the max-age directive
502 * - (Expires - Data) headers
503 * - the default-max-age of the cache
504 *
505 */
William Lallemand49b44532017-11-24 18:53:43 +0100506int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100507{
508 struct http_txn *txn = s->txn;
509 struct hdr_ctx ctx;
510
511 int smaxage = -1;
512 int maxage = -1;
513
514
William Lallemand4da3f8a2017-10-31 14:33:34 +0100515 ctx.idx = 0;
516
517 /* loop on the Cache-Control values */
Willy Tarreau178b9872018-06-19 07:13:36 +0200518 while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100519 char *directive = ctx.line + ctx.val;
520 char *value;
521
522 value = directive_value(directive, ctx.vlen, "s-maxage", 8);
523 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200524 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100525
526 chunk_strncat(chk, value, ctx.vlen - 8 + 1);
527 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200528 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100529 }
530
531 value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
532 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200533 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100534
535 chunk_strncat(chk, value, ctx.vlen - 7 + 1);
536 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200537 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100538 }
539 }
540
541 /* TODO: Expires - Data */
542
543
544 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100545 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100546
547 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100548 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100549
William Lallemand49b44532017-11-24 18:53:43 +0100550 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100551
552}
553
554
William Lallemanda400a3a2017-11-20 19:13:12 +0100555static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
556{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200557 struct cache_entry *object = (struct cache_entry *)block->data;
558
559 if (first == block && object->eb.key)
560 eb32_delete(&object->eb);
561 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100562}
563
William Lallemand41db4602017-10-30 11:15:51 +0100564/*
565 * This fonction will store the headers of the response in a buffer and then
566 * register a filter to store the data
567 */
568enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
569 struct session *sess, struct stream *s, int flags)
570{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200571 unsigned int age;
572 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100573 struct http_txn *txn = s->txn;
574 struct http_msg *msg = &txn->rsp;
575 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100576 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100577 struct cache_flt_conf *cconf = rule->arg.act.p[0];
578 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100579 struct cache_entry *object;
580
William Lallemand4da3f8a2017-10-31 14:33:34 +0100581 /* Don't cache if the response came from a cache */
582 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
583 s->target == &http_cache_applet.obj_type) {
584 goto out;
585 }
586
587 /* cache only HTTP/1.1 */
588 if (!(txn->req.flags & HTTP_MSGF_VER_11))
589 goto out;
590
591 /* cache only GET method */
592 if (txn->meth != HTTP_METH_GET)
593 goto out;
594
595 /* cache only 200 status code */
596 if (txn->status != 200)
597 goto out;
598
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100599 if (IS_HTX_STRM(s)) {
600 struct htx *htx = htxbuf(&s->res.buf);
601 struct http_hdr_ctx ctx;
602 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100603
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100604 /* Do not cache too big objects. */
605 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
606 htx->data + htx->extra > shctx->max_obj_size)
607 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100608
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100609 /* Does not manage Vary at the moment. We will need a secondary key later for that */
610 ctx.blk = NULL;
611 if (http_find_header(htx, ist("Vary"), &ctx, 0))
612 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100613
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100614 htx_check_response_for_cacheability(s, &s->res);
615
616 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
617 goto out;
618
619 age = 0;
620 ctx.blk = NULL;
621 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
622 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
623 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
624 hdr_age = CACHE_ENTRY_MAX_AGE;
625 age = hdr_age;
626 }
627 http_remove_header(htx, &ctx);
628 }
629
630 chunk_reset(&trash);
631 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
632 struct htx_blk *blk = htx_get_blk(htx, pos);
633 uint32_t sz = htx_get_blksz(blk);
634
635 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
636 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
637 break;
638 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
639 }
640 }
641 else {
642 struct hdr_ctx ctx;
643
644 /* Do not cache too big objects. */
645 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
646 msg->sov + msg->body_len > shctx->max_obj_size)
647 goto out;
648
649 /* Does not manage Vary at the moment. We will need a secondary key later for that */
650 ctx.idx = 0;
651 if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx))
652 goto out;
653
654 check_response_for_cacheability(s, &s->res);
655
656 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
657 goto out;
658
659 age = 0;
660 ctx.idx = 0;
661 if (http_find_header2("Age", 3, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
662 if (!strl2llrc(ctx.line + ctx.val, ctx.vlen, &hdr_age) && hdr_age > 0) {
663 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
664 hdr_age = CACHE_ENTRY_MAX_AGE;
665 age = hdr_age;
666 }
667 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200668 }
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200669 }
670
William Lallemand4da3f8a2017-10-31 14:33:34 +0100671 shctx_lock(shctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100672 if (IS_HTX_STRM(s))
673 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
674 else
675 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + msg->sov);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100676 if (!first) {
677 shctx_unlock(shctx);
678 goto out;
679 }
680 shctx_unlock(shctx);
681
Willy Tarreau1093a452018-04-06 19:02:25 +0200682 /* the received memory is not initialized, we need at least to mark
683 * the object as not indexed yet.
684 */
685 object = (struct cache_entry *)first->data;
686 object->eb.node.leaf_p = NULL;
687 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200688 object->age = age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100689 if (IS_HTX_STRM(s)) {
690 object->hdrs_len = trash.data;
691 object->data_len = 0;
692 }
693 else
694 object->eoh = msg->eoh;
Willy Tarreau1093a452018-04-06 19:02:25 +0200695
William Lallemand4da3f8a2017-10-31 14:33:34 +0100696 /* reserve space for the cache_entry structure */
697 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200698 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100699 /* cache the headers in a http action because it allows to chose what
700 * to cache, for example you might want to cache a response before
701 * modifying some HTTP headers, or on the contrary after modifying
702 * those headers.
703 */
704
705 /* does not need to be locked because it's in the "hot" list,
706 * copy the headers */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100707 if (IS_HTX_STRM(s)) {
708 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
709 goto out;
710 }
711 else {
712 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)ci_head(&s->res), msg->sov) < 0)
713 goto out;
714 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100715
716 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet95220e22018-12-07 17:34:39 +0100717 list_for_each_entry(filter, &s->strm_flt.filters, list) {
718 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
719 if (filter->ctx) {
720 struct cache_st *cache_ctx = filter->ctx;
721 struct cache_entry *old;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100722
Christopher Faulet95220e22018-12-07 17:34:39 +0100723 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100724
Christopher Faulet95220e22018-12-07 17:34:39 +0100725 object->eb.key = (*(unsigned int *)&txn->cache_hash);
726 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
727 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100728
Christopher Faulet95220e22018-12-07 17:34:39 +0100729 shctx_lock(shctx);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100730
Christopher Faulet95220e22018-12-07 17:34:39 +0100731 old = entry_exist(cconf->c.cache, txn->cache_hash);
732 if (old) {
733 eb32_delete(&old->eb);
734 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100735 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100736 shctx_unlock(shctx);
737
738 /* store latest value and expiration time */
739 object->latest_validation = now.tv_sec;
740 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100741 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100742 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100743 }
744 }
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
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200759#define HTTP_CACHE_INIT 0 /* Initial state. */
760#define HTTP_CACHE_HEADER 1 /* Cache entry headers forwarded. */
761#define HTTP_CACHE_FWD 2 /* Cache entry completely forwarded. */
762#define HTTP_CACHE_END 3 /* Cache entry treatment terminated. */
William Lallemand77c11972017-10-31 20:43:01 +0100763
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100764#define HTX_CACHE_INIT 0 /* Initial state. */
765#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
766#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
767#define HTX_CACHE_EOD 3 /* Cache entry data forwarded. DATA->TLR transition */
768#define HTX_CACHE_TLR 4 /* Cache entry trailers forwarding */
769#define HTX_CACHE_EOM 5 /* Cache entry completely forwarded. Finish the HTX message */
770#define HTX_CACHE_END 6 /* Cache entry treatment terminated */
771
William Lallemandecb73b12017-11-24 14:33:55 +0100772static void http_cache_applet_release(struct appctx *appctx)
773{
Christopher Faulet95220e22018-12-07 17:34:39 +0100774 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100775 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100776 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100777 struct shared_block *first = block_ptr(cache_ptr);
778
779 shctx_lock(shctx_ptr(cache));
780 shctx_row_dec_hot(shctx_ptr(cache), first);
781 shctx_unlock(shctx_ptr(cache));
782}
783
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100784static size_t htx_cache_dump_headers(struct appctx *appctx, struct htx *htx)
785{
Christopher Faulet95220e22018-12-07 17:34:39 +0100786 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
787 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100788 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
789 struct shared_block *shblk = appctx->ctx.cache.next;
790 struct buffer *tmp = get_trash_chunk();
791 char *end;
792 unsigned int offset, len, age;
793
794 offset = appctx->ctx.cache.offset;
795 len = cache_ptr->hdrs_len;
796
797 /* 1. Retrieve all headers from the cache */
798 list_for_each_entry_from(shblk, &shctx->hot, list) {
799 int sz;
800
801 sz = MIN(len, shctx->block_size - offset);
802 if (!chunk_memcat(tmp, (const char *)shblk->data + offset, sz))
803 return 0;
804
805 offset += sz;
806 len -= sz;
807 if (!len)
808 break;
809 offset = 0;
810 }
811 appctx->ctx.cache.offset = offset;
812 appctx->ctx.cache.next = shblk;
813 appctx->ctx.cache.sent += b_data(tmp);
814
815 /* 2. push these headers in the HTX message */
816 offset = 0;
817 while (offset < b_data(tmp)) {
818 struct htx_blk *blk;
819 enum htx_blk_type type;
820 uint32_t info, sz;
821
822 /* Read the header's info */
823 memcpy((char *)&info, b_peek(tmp, offset), 4);
824 type = (info >> 28);
825 sz = ((type == HTX_BLK_HDR)
826 ? (info & 0xff) + ((info >> 8) & 0xfffff)
827 : info & 0xfffffff);
828
829 /* Create the block with the right type and the right size */
830 blk = htx_add_blk(htx, type, sz);
831 if (!blk)
832 return 0;
833
834 /* Copy info and data */
835 blk->info = info;
836 memcpy(htx_get_blk_ptr(htx, blk), b_peek(tmp, offset+4), sz);
837
838 /* next header */
839 offset += 4 + sz;
840 }
841
842 /* 3. Append "age" header */
843 chunk_reset(tmp);
844 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
845 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
846 age = CACHE_ENTRY_MAX_AGE;
847 end = ultoa_o(age, b_head(tmp), b_size(tmp));
848 b_set_data(tmp, end - b_head(tmp));
849
850 if (!http_add_header(htx, ist("Age"), ist2(b_head(tmp), b_data(tmp))))
851 return 0;
852
853 return htx->data;
854}
855
856static size_t htx_cache_dump_data(struct appctx *appctx, struct htx *htx,
857 enum htx_blk_type type, unsigned int len)
858{
Christopher Faulet95220e22018-12-07 17:34:39 +0100859 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
860 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100861 struct shared_block *shblk = appctx->ctx.cache.next;
862 uint32_t max = htx_free_data_space(htx);
863 unsigned int offset;
864 size_t total = 0;
865
866 offset = appctx->ctx.cache.offset;
867 if (len > max)
868 len = max;
869 if (!len)
870 goto end;
871
872 list_for_each_entry_from(shblk, &shctx->hot, list) {
873 struct ist data;
874 int sz;
875
876 sz = MIN(len, shctx->block_size - offset);
877 data = ist2((const char *)shblk->data + offset, sz);
878 if (type == HTX_BLK_DATA) {
879 if (!htx_add_data(htx, data))
880 break;
881 }
882 else { /* HTX_BLK_TLR */
883 if (!htx_add_trailer(htx, data))
884 break;
885 }
886
887 offset += sz;
888 len -= sz;
889 total += sz;
890 if (!len)
891 break;
892 offset = 0;
893 }
894 appctx->ctx.cache.offset = offset;
895 appctx->ctx.cache.next = shblk;
896 appctx->ctx.cache.sent += total;
897
898 end:
899 return total;
900}
901static void htx_cache_io_handler(struct appctx *appctx)
902{
903 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
904 struct shared_block *first = block_ptr(cache_ptr);
905 struct stream_interface *si = appctx->owner;
906 struct channel *req = si_oc(si);
907 struct channel *res = si_ic(si);
908 struct htx *req_htx, *res_htx;
909 struct buffer *errmsg;
910 size_t ret, total = 0;
911
912 res_htx = htxbuf(&res->buf);
913
914 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
915 goto out;
916
917 /* Check if the input buffer is avalaible. */
918 if (!b_size(&res->buf)) {
919 si_rx_room_blk(si);
920 goto out;
921 }
922
923 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
924 appctx->st0 = HTTP_CACHE_END;
925
926 if (appctx->st0 == HTX_CACHE_INIT) {
927 appctx->ctx.cache.next = block_ptr(cache_ptr);
928 appctx->ctx.cache.offset = sizeof(*cache_ptr);
929 appctx->ctx.cache.sent = 0;
930 appctx->st0 = HTX_CACHE_HEADER;
931 }
932
933 if (appctx->st0 == HTX_CACHE_HEADER) {
934 /* Headers must be dump at once. Otherwise it is an error */
935 ret = htx_cache_dump_headers(appctx, res_htx);
936 if (!ret)
937 goto error;
938
939 total += ret;
940 if (cache_ptr->data_len)
941 appctx->st0 = HTX_CACHE_DATA;
942 else if (first->len > sizeof(*cache_ptr) + appctx->ctx.cache.sent) {
943 /* Headers have benn sent (hrds_len) and there is no data
944 * (data_len == 0). So, all the remaining is the
945 * trailers */
946 appctx->st0 = HTX_CACHE_EOD;
947 }
948 else
949 appctx->st0 = HTX_CACHE_EOM;
950 }
951
952 if (appctx->st0 == HTX_CACHE_DATA) {
953 unsigned int len = cache_ptr->hdrs_len + cache_ptr->data_len - appctx->ctx.cache.sent;
954
955 ret = htx_cache_dump_data(appctx, res_htx, HTX_BLK_DATA, len);
956 if (!ret) {
957 si_rx_room_blk(si);
958 goto out;
959 }
960
961 total += ret;
962 if (cache_ptr->hdrs_len + cache_ptr->data_len == appctx->ctx.cache.sent) {
963 if (first->len > sizeof(*cache_ptr) + appctx->ctx.cache.sent) {
964 /* Headers and all data have been sent
965 * (hrds_len + data_len == sent). So, all the remaining
966 * is the trailers */
967 appctx->st0 = HTX_CACHE_EOD;
968 }
969 else
970 appctx->st0 = HTX_CACHE_EOM;
971 }
972 }
973
974 if (appctx->st0 == HTX_CACHE_EOD) {
975 if (!htx_add_endof(res_htx, HTX_BLK_EOD)) {
976 si_rx_room_blk(si);
977 goto out;
978 }
979
980 total++;
981 appctx->st0 = HTX_CACHE_TLR;
982 }
983
984 if (appctx->st0 == HTX_CACHE_TLR) {
985 unsigned int len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
986
987 ret = htx_cache_dump_data(appctx, res_htx, HTX_BLK_TLR, len);
988 if (!ret) {
989 si_rx_room_blk(si);
990 goto out;
991 }
992
993 total += ret;
994 if (first->len == sizeof(*cache_ptr) + appctx->ctx.cache.sent)
995 appctx->st0 = HTX_CACHE_EOM;
996 }
997
998 if (appctx->st0 == HTX_CACHE_EOM) {
999 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1000 si_rx_room_blk(si);
1001 goto out;
1002 }
1003
1004 total++;
1005 appctx->st0 = HTX_CACHE_END;
1006 }
1007
1008 end:
1009 if (appctx->st0 == HTX_CACHE_END) {
1010 /* eat the whole request */
1011 req_htx = htxbuf(&req->buf);
1012 htx_reset(req_htx);
1013 htx_to_buf(req_htx, &req->buf);
1014 co_set_data(req, 0);
1015 res->flags |= CF_READ_NULL;
1016 si_shutr(si);
1017 }
1018
1019 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
1020 si_shutw(si);
1021
1022 if (appctx->st0 == HTX_CACHE_END) {
1023 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
1024 si_shutr(si);
1025 res->flags |= CF_READ_NULL;
1026 }
1027 }
1028 out:
1029 if (total) {
1030 res->total += total;
1031 res->flags |= CF_READ_PARTIAL;
1032 }
1033
1034 /* we have left the request in the buffer for the case where we
1035 * process a POST, and this automatically re-enables activity on
1036 * read. It's better to indicate that we want to stop reading when
1037 * we're sending, so that we know there's at most one direction
1038 * deciding to wake the applet up. It saves it from looping when
1039 * emitting large blocks into small TCP windows.
1040 */
1041 htx_to_buf(res_htx, &res->buf);
1042 if (!channel_is_empty(res))
1043 si_stop_get(si);
1044 return;
1045
1046 error:
1047 /* Sent and HTTP error 500 */
1048 b_reset(&res->buf);
1049 errmsg = &htx_err_chunks[HTTP_ERR_500];
1050 res->buf.data = b_data(errmsg);
1051 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1052 res_htx = htx_from_buf(&res->buf);
1053
1054 total = res_htx->data;
1055 appctx->st0 = HTX_CACHE_END;
1056 goto end;
1057}
1058
1059
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001060/*
1061 * Append an "Age" header into <chn> channel for this <ce> cache entry.
Joseph Herlant8dae5b32018-11-15 14:07:53 -08001062 * This is the responsibility of the caller to insure there is enough
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001063 * data in the channel.
1064 *
1065 * Returns the number of bytes inserted if succeeded, 0 if failed.
1066 */
1067static int cache_channel_append_age_header(struct cache_entry *ce, struct channel *chn)
1068{
1069 unsigned int age;
1070
1071 age = MAX(0, (int)(now.tv_sec - ce->latest_validation)) + ce->age;
1072 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1073 age = CACHE_ENTRY_MAX_AGE;
1074
1075 chunk_reset(&trash);
1076 chunk_printf(&trash, "Age: %u", age);
1077
1078 return ci_insert_line2(chn, ce->eoh, trash.area, trash.data);
1079}
1080
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001081static int cache_channel_row_data_get(struct appctx *appctx, int len)
William Lallemand77c11972017-10-31 20:43:01 +01001082{
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001083 int ret, total;
William Lallemand77c11972017-10-31 20:43:01 +01001084 struct stream_interface *si = appctx->owner;
1085 struct channel *res = si_ic(si);
Christopher Faulet95220e22018-12-07 17:34:39 +01001086 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1087 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001088 struct shared_context *shctx = shctx_ptr(cache);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001089 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1090 struct shared_block *blk, *next = appctx->ctx.cache.next;
1091 int offset;
1092
1093 total = 0;
1094 offset = 0;
1095
1096 if (!next) {
1097 offset = sizeof(struct cache_entry);
1098 next = block_ptr(cache_ptr);
1099 }
1100
1101 blk = next;
1102 list_for_each_entry_from(blk, &shctx->hot, list) {
1103 int sz;
1104
1105 if (len <= 0)
1106 break;
1107
1108 sz = MIN(len, shctx->block_size - offset);
1109
1110 ret = ci_putblk(res, (const char *)blk->data + offset, sz);
1111 if (unlikely(offset))
1112 offset = 0;
1113 if (ret <= 0) {
1114 if (ret == -3 || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001115 si_rx_room_blk(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001116 break;
1117 }
1118 return -1;
1119 }
1120
1121 total += sz;
1122 len -= sz;
1123 }
1124 appctx->ctx.cache.next = blk;
1125
1126 return total;
1127}
1128
1129static void http_cache_io_handler(struct appctx *appctx)
1130{
1131 struct stream_interface *si = appctx->owner;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001132 struct stream *s = si_strm(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001133 struct channel *res = si_ic(si);
1134 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
William Lallemand77c11972017-10-31 20:43:01 +01001135 struct shared_block *first = block_ptr(cache_ptr);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001136 unsigned int *sent = &appctx->ctx.cache.sent;
1137
1138 if (IS_HTX_STRM(s))
1139 return htx_cache_io_handler(appctx);
William Lallemand77c11972017-10-31 20:43:01 +01001140
1141 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1142 goto out;
1143
Joseph Herlant8dae5b32018-11-15 14:07:53 -08001144 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001145 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01001146 /* buf.size==0 means we failed to get a buffer and were
1147 * already subscribed to a wait list to get a buffer.
1148 */
William Lallemand77c11972017-10-31 20:43:01 +01001149 goto out;
1150 }
1151
1152 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
1153 appctx->st0 = HTTP_CACHE_END;
1154
1155 /* buffer are aligned there, should be fine */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001156 if (appctx->st0 == HTTP_CACHE_HEADER || appctx->st0 == HTTP_CACHE_INIT) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001157 int len = first->len - *sent - sizeof(struct cache_entry);
William Lallemand55e76742017-11-21 20:01:28 +01001158
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001159 if (len > 0) {
1160 int ret;
1161
1162 ret = cache_channel_row_data_get(appctx, len);
1163 if (ret == -1)
1164 appctx->st0 = HTTP_CACHE_END;
1165 else
1166 *sent += ret;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001167 if (appctx->st0 == HTTP_CACHE_INIT && *sent > cache_ptr->eoh &&
1168 cache_channel_append_age_header(cache_ptr, res))
1169 appctx->st0 = HTTP_CACHE_HEADER;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001170 }
1171 else {
1172 *sent = 0;
1173 appctx->st0 = HTTP_CACHE_FWD;
William Lallemand77c11972017-10-31 20:43:01 +01001174 }
William Lallemand77c11972017-10-31 20:43:01 +01001175 }
1176
1177 if (appctx->st0 == HTTP_CACHE_FWD) {
1178 /* eat the whole request */
Willy Tarreau178b9872018-06-19 07:13:36 +02001179 co_skip(si_oc(si), co_data(si_oc(si))); // NOTE: when disabled does not repport the correct status code
William Lallemand77c11972017-10-31 20:43:01 +01001180 res->flags |= CF_READ_NULL;
1181 si_shutr(si);
1182 }
1183
1184 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
1185 si_shutw(si);
1186out:
1187 ;
1188}
1189
Christopher Faulet95220e22018-12-07 17:34:39 +01001190static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001191{
1192 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001193 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001194
Christopher Faulet95220e22018-12-07 17:34:39 +01001195 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001196 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001197 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001198 }
1199
1200 /* check if a cache filter was already registered with this cache
1201 * name, if that's the case, must use it. */
1202 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001203 if (fconf->id == cache_store_flt_id) {
1204 cconf = fconf->conf;
1205 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1206 rule->arg.act.p[0] = cconf;
1207 return 1;
1208 }
William Lallemand41db4602017-10-30 11:15:51 +01001209 }
1210 }
1211
Christopher Faulet95220e22018-12-07 17:34:39 +01001212 /* Create the filter cache config */
1213 cconf = calloc(1, sizeof(*cconf));
1214 if (!cconf) {
1215 memprintf(err, "out of memory\n");
1216 goto err;
1217 }
1218 cconf->flags = 0;
1219 cconf->c.name = strdup(name);
1220 if (!cconf->c.name) {
1221 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001222 goto err;
1223 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001224
William Lallemand41db4602017-10-30 11:15:51 +01001225 /* register a filter to fill the cache buffer */
1226 fconf = calloc(1, sizeof(*fconf));
1227 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001228 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001229 goto err;
1230 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001231 fconf->id = cache_store_flt_id;
1232 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001233 fconf->ops = &cache_ops;
1234 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1235
Christopher Faulet95220e22018-12-07 17:34:39 +01001236 rule->arg.act.p[0] = cconf;
1237 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001238
Christopher Faulet95220e22018-12-07 17:34:39 +01001239 err:
1240 free(cconf);
1241 return 0;
1242}
1243
1244enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1245 struct act_rule *rule, char **err)
1246{
1247 rule->action = ACT_CUSTOM;
1248 rule->action_ptr = http_action_store_cache;
1249
1250 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1251 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001252
Christopher Faulet95220e22018-12-07 17:34:39 +01001253 (*orig_arg)++;
1254 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001255}
1256
William Lallemandf528fff2017-11-23 19:43:17 +01001257/* This produces a sha1 hash of the concatenation of the first
1258 * occurrence of the Host header followed by the path component if it
1259 * begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001260int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001261{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001262 struct http_txn *txn = s->txn;
William Lallemandf528fff2017-11-23 19:43:17 +01001263 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001264 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001265
William Lallemandf528fff2017-11-23 19:43:17 +01001266 trash = get_trash_chunk();
1267
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001268 if (IS_HTX_STRM(s)) {
1269 struct htx *htx = htxbuf(&s->req.buf);
1270 struct htx_sl *sl;
1271 struct http_hdr_ctx ctx;
1272 struct ist path;
William Lallemandf528fff2017-11-23 19:43:17 +01001273
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001274 ctx.blk = NULL;
1275 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1276 return 0;
1277 chunk_memcat(trash, ctx.value.ptr, ctx.value.len);
1278
1279 sl = http_find_stline(htx);
1280 path = http_get_path(htx_sl_req_uri(sl));
1281 if (!path.ptr)
1282 return 0;
1283 chunk_memcat(trash, path.ptr, path.len);
1284 }
1285 else {
1286 struct hdr_ctx ctx;
1287 char *path;
1288 char *end;
1289
1290 /* retrive the host */
1291 ctx.idx = 0;
1292 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx))
1293 return 0;
1294 chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen);
1295
1296 /* now retrieve the path */
1297 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1298 path = http_txn_get_path(txn);
1299 if (!path)
1300 return 0;
1301 chunk_strncat(trash, path, end - path);
1302 }
William Lallemandf528fff2017-11-23 19:43:17 +01001303
1304 /* hash everything */
1305 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001306 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001307 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1308
1309 return 1;
1310}
1311
William Lallemand41db4602017-10-30 11:15:51 +01001312enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1313 struct session *sess, struct stream *s, int flags)
1314{
William Lallemand77c11972017-10-31 20:43:01 +01001315
William Lallemand77c11972017-10-31 20:43:01 +01001316 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001317 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1318 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001319
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001320 if (IS_HTX_STRM(s))
1321 htx_check_request_for_cacheability(s, &s->req);
1322 else
1323 check_request_for_cacheability(s, &s->req);
1324
Willy Tarreau504455c2017-12-22 17:47:35 +01001325 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1326 return ACT_RET_CONT;
1327
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001328 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001329 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001330
Willy Tarreau504455c2017-12-22 17:47:35 +01001331 if (s->txn->flags & TX_CACHE_IGNORE)
1332 return ACT_RET_CONT;
1333
William Lallemanda400a3a2017-11-20 19:13:12 +01001334 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001335 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001336 if (res) {
1337 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001338 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1339 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001340 s->target = &http_cache_applet.obj_type;
1341 if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
1342 appctx->st0 = HTTP_CACHE_INIT;
1343 appctx->rule = rule;
1344 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001345 appctx->ctx.cache.next = NULL;
1346 appctx->ctx.cache.sent = 0;
Olivier Houchardfccf8402017-11-01 14:04:02 +01001347 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001348 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001349 shctx_lock(shctx_ptr(cache));
1350 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1351 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001352 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001353 }
1354 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001355 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001356 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001357}
1358
1359
1360enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1361 struct act_rule *rule, char **err)
1362{
William Lallemand41db4602017-10-30 11:15:51 +01001363 rule->action = ACT_CUSTOM;
1364 rule->action_ptr = http_action_req_cache_use;
1365
Christopher Faulet95220e22018-12-07 17:34:39 +01001366 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001367 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001368
1369 (*orig_arg)++;
1370 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001371}
1372
1373int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1374{
1375 int err_code = 0;
1376
1377 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1378
1379 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001380 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1381 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001382 err_code |= ERR_ALERT | ERR_ABORT;
1383 goto out;
1384 }
1385
1386 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1387 err_code |= ERR_ABORT;
1388 goto out;
1389 }
1390
1391 if (tmp_cache_config == NULL) {
1392 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1393 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001394 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001395 err_code |= ERR_ALERT | ERR_ABORT;
1396 goto out;
1397 }
1398
1399 strlcpy2(tmp_cache_config->id, args[1], 33);
1400 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001401 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1402 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001403 err_code |= ERR_WARN;
1404 }
William Lallemand49b44532017-11-24 18:53:43 +01001405 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001406 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001407 tmp_cache_config->maxobjsz = 0;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001408 tmp_cache_config->flags = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001409 }
1410 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001411 unsigned long int maxsize;
1412 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001413
1414 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1415 err_code |= ERR_ABORT;
1416 goto out;
1417 }
1418
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001419 maxsize = strtoul(args[1], &err, 10);
1420 if (err == args[1] || *err != '\0') {
1421 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1422 file, linenum, args[1]);
1423 err_code |= ERR_ABORT;
1424 goto out;
1425 }
1426
1427 if (maxsize > (UINT_MAX >> 20)) {
1428 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1429 file, linenum, args[1], UINT_MAX >> 20);
1430 err_code |= ERR_ABORT;
1431 goto out;
1432 }
1433
William Lallemand41db4602017-10-30 11:15:51 +01001434 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001435 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001436 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001437 } else if (strcmp(args[0], "max-age") == 0) {
1438 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1439 err_code |= ERR_ABORT;
1440 goto out;
1441 }
1442
1443 if (!*args[1]) {
1444 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1445 file, linenum, args[0]);
1446 err_code |= ERR_WARN;
1447 }
1448
1449 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001450 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001451 unsigned int maxobjsz;
1452 char *err;
1453
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001454 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1455 err_code |= ERR_ABORT;
1456 goto out;
1457 }
1458
1459 if (!*args[1]) {
1460 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1461 file, linenum, args[0]);
1462 err_code |= ERR_WARN;
1463 }
1464
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001465 maxobjsz = strtoul(args[1], &err, 10);
1466 if (err == args[1] || *err != '\0') {
1467 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1468 file, linenum, args[1]);
1469 err_code |= ERR_ABORT;
1470 goto out;
1471 }
1472 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001473 }
1474 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001475 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001476 err_code |= ERR_ALERT | ERR_FATAL;
1477 goto out;
1478 }
1479out:
1480 return err_code;
1481}
1482
1483/* once the cache section is parsed */
1484
1485int cfg_post_parse_section_cache()
1486{
1487 struct shared_context *shctx;
1488 int err_code = 0;
1489 int ret_shctx;
1490
1491 if (tmp_cache_config) {
1492 struct cache *cache;
1493
1494 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001495 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001496 err_code |= ERR_FATAL | ERR_ALERT;
1497 goto out;
1498 }
1499
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001500 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001501 /* Default max. file size is a 256th of the cache size. */
1502 tmp_cache_config->maxobjsz =
1503 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001504 }
1505 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1506 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1507 err_code |= ERR_FATAL | ERR_ALERT;
1508 goto out;
1509 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001510
1511 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE,
1512 tmp_cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001513
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001514 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001515 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001516 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001517 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001518 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001519
1520 err_code |= ERR_FATAL | ERR_ALERT;
1521 goto out;
1522 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001523 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +01001524 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
1525 cache = (struct cache *)shctx->data;
1526 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001527 LIST_ADDQ(&caches, &cache->list);
1528 }
1529out:
1530 free(tmp_cache_config);
1531 tmp_cache_config = NULL;
1532 return err_code;
1533
1534}
1535
1536/*
1537 * Resolve the cache name to a pointer once the file is completely read.
1538 */
1539int cfg_cache_postparser()
1540{
William Lallemand41db4602017-10-30 11:15:51 +01001541 struct cache *cache;
William Lallemand41db4602017-10-30 11:15:51 +01001542 int err = 0;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001543
1544 /* Check if the cache is used by HTX and legacy HTTP proxies in same
1545 * time
1546 */
1547 list_for_each_entry(cache, &caches, list) {
1548 if ((cache->flags & (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) == (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) {
1549 ha_alert("Cache '%s': cannot be used by HTX and legacy HTTP proxies in same time.\n",
1550 cache->id);
1551 err++;
1552 }
1553 }
1554
William Lallemand41db4602017-10-30 11:15:51 +01001555 return err;
1556}
1557
1558
1559struct flt_ops cache_ops = {
1560 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001561 .check = cache_store_check,
1562 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001563
William Lallemand4da3f8a2017-10-31 14:33:34 +01001564 /* Handle channels activity */
1565 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001566 .channel_end_analyze = cache_store_chn_end_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001567
1568 /* Filter HTTP requests and responses */
1569 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001570 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001571 .http_end = cache_store_http_end,
1572
1573 .http_forward_data = cache_store_http_forward_data,
1574
William Lallemand41db4602017-10-30 11:15:51 +01001575};
1576
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001577static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001578{
1579 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1580 return 1;
1581
1582 return 0;
1583}
1584
1585static int cli_io_handler_show_cache(struct appctx *appctx)
1586{
1587 struct cache* cache = appctx->ctx.cli.p0;
1588 struct stream_interface *si = appctx->owner;
1589
William Lallemand1f49a362017-11-21 20:01:26 +01001590 if (cache == NULL) {
1591 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1592 }
1593
1594 list_for_each_entry_from(cache, &caches, list) {
1595 struct eb32_node *node = NULL;
1596 unsigned int next_key;
1597 struct cache_entry *entry;
1598
William Lallemand1f49a362017-11-21 20:01:26 +01001599 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001600 if (!next_key) {
1601 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1602 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001603 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001604 return 0;
1605 }
1606 }
William Lallemand1f49a362017-11-21 20:01:26 +01001607
1608 appctx->ctx.cli.p0 = cache;
1609
1610 while (1) {
1611
1612 shctx_lock(shctx_ptr(cache));
1613 node = eb32_lookup_ge(&cache->entries, next_key);
1614 if (!node) {
1615 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001616 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001617 break;
1618 }
1619
1620 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001621 chunk_printf(&trash, "%p hash:%u size:%u (%u blocks), refcount:%u, expire:%d\n", entry, (*(unsigned int *)entry->hash), block_ptr(entry)->len, block_ptr(entry)->block_count, block_ptr(entry)->refcount, entry->expire - (int)now.tv_sec);
William Lallemand1f49a362017-11-21 20:01:26 +01001622
1623 next_key = node->key + 1;
1624 appctx->ctx.cli.i0 = next_key;
1625
1626 shctx_unlock(shctx_ptr(cache));
1627
1628 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001629 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001630 return 0;
1631 }
1632 }
1633
1634 }
1635
1636 return 1;
1637
1638}
1639
1640static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001641 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1642 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001643}};
1644
Willy Tarreau0108d902018-11-25 19:14:37 +01001645INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001646
William Lallemand41db4602017-10-30 11:15:51 +01001647static struct action_kw_list http_res_actions = {
1648 .kw = {
1649 { "cache-store", parse_cache_store },
1650 { NULL, NULL }
1651 }
1652};
1653
Willy Tarreau0108d902018-11-25 19:14:37 +01001654INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1655
William Lallemand41db4602017-10-30 11:15:51 +01001656static struct action_kw_list http_req_actions = {
1657 .kw = {
1658 { "cache-use", parse_cache_use },
1659 { NULL, NULL }
1660 }
1661};
1662
Willy Tarreau0108d902018-11-25 19:14:37 +01001663INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1664
William Lallemand41db4602017-10-30 11:15:51 +01001665struct applet http_cache_applet = {
1666 .obj_type = OBJ_TYPE_APPLET,
1667 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001668 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001669 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001670};
1671
Willy Tarreaue6552512018-11-26 11:33:13 +01001672/* config parsers for this section */
1673REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
1674REGISTER_CONFIG_POSTPARSER("cache", cfg_cache_postparser);