blob: fd8a00ad3d7b1bc00f9e26c149ba8dc9e11f45b0 [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 Fauletafd819c2018-12-11 08:57:45 +010046#define CACHE_FLT_F_IGNORE_CNT_ENC 0x00000001 /* Ignore 'Content-Encoding' header when response is cached
47 * if compression is already started */
48
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010049const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010050
51struct applet http_cache_applet;
52
53struct flt_ops cache_ops;
54
55struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010056 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010057 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010058 unsigned int maxage; /* max-age */
59 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020060 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010061 char id[33]; /* cache name */
Christopher Faulet1f672c52018-12-03 14:30:41 +010062 unsigned int flags; /* CACHE_F_* */
William Lallemand41db4602017-10-30 11:15:51 +010063};
64
Christopher Faulet95220e22018-12-07 17:34:39 +010065/* cache config for filters */
66struct cache_flt_conf {
67 union {
68 struct cache *cache; /* cache used by the filter */
69 char *name; /* cache name used during conf parsing */
70 } c;
71 unsigned int flags; /* CACHE_FLT_F_* */
72};
73
William Lallemand41db4602017-10-30 11:15:51 +010074/*
75 * cache ctx for filters
76 */
77struct cache_st {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010078 int hdrs_len; // field used in legacy mode only
William Lallemand41db4602017-10-30 11:15:51 +010079 struct shared_block *first_block;
80};
81
82struct cache_entry {
83 unsigned int latest_validation; /* latest validation date */
84 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020085 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010086 unsigned int eoh; /* Origin server end of headers offset. */ // field used in legacy mode only
87
88 unsigned int hdrs_len; // field used in HTX mode only
89 unsigned int data_len; // field used in HTX mode only
90
William Lallemand41db4602017-10-30 11:15:51 +010091 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010092 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010093 unsigned char data[0];
94};
95
96#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010097#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010098
99static struct list caches = LIST_HEAD_INIT(caches);
100static struct cache *tmp_cache_config = NULL;
101
Willy Tarreau8ceae722018-11-26 11:58:30 +0100102DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
103
William Lallemandf528fff2017-11-23 19:43:17 +0100104struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100105{
106 struct eb32_node *node;
107 struct cache_entry *entry;
108
William Lallemandf528fff2017-11-23 19:43:17 +0100109 node = eb32_lookup(&cache->entries, (*(unsigned int *)hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100110 if (!node)
111 return NULL;
112
113 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100114
115 /* if that's not the right node */
116 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
117 return NULL;
118
William Lallemand08727662017-11-21 20:01:27 +0100119 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100120 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100121 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100122 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100123 entry->eb.key = 0;
124 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100125 return NULL;
126
127}
128
129static inline struct shared_context *shctx_ptr(struct cache *cache)
130{
131 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
132}
133
William Lallemand77c11972017-10-31 20:43:01 +0100134static inline struct shared_block *block_ptr(struct cache_entry *entry)
135{
136 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
137}
138
139
140
William Lallemand41db4602017-10-30 11:15:51 +0100141static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100142cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100143{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100144 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100145 return 0;
146}
147
Christopher Faulet95220e22018-12-07 17:34:39 +0100148static void
149cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
150{
151 struct cache_flt_conf *cconf = fconf->conf;
152
153 free(cconf);
154}
155
William Lallemand4da3f8a2017-10-31 14:33:34 +0100156static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100157cache_store_check(struct proxy *px, struct flt_conf *fconf)
158{
159 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100160 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100161 struct cache *cache;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100162 int ignore = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100163
164 /* resolve the cache name to a ptr in the filter config */
165 list_for_each_entry(cache, &caches, list) {
166 if (!strcmp(cache->id, cconf->c.name)) {
167 /* there can be only one filter per cache, so we free it there */
168 cache->flags |= ((px->options2 & PR_O2_USE_HTX)
169 ? CACHE_F_HTX
170 : CACHE_F_LEGACY_HTTP);
171
172 free(cconf->c.name);
173 cconf->c.cache = cache;
174 goto found;
175 }
176 }
177
178 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
179 proxy_type_str(px), px->id, (char *)cconf->c.name);
180 return 1;
181
182 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100183 /* Here <cache> points on the cache the filter must use and <cconf>
184 * points on the cache filter configuration. */
185
186 /* Check all filters for proxy <px> to know if the compression is
187 * enabled and if it is before or after the cache. When the compression
188 * is before the cache, nothing special is done. The response is stored
189 * compressed in the cache. When the compression is after the cache, the
190 * 'Content-encoding' header must be ignored because the response will
191 * be stored uncompressed. The compression will be done on the cached
192 * response too. */
193 list_for_each_entry(f, &px->filter_configs, list) {
194 if (f == fconf) {
195 ignore = 1;
196 continue;
197 }
198
199 if (f->id == http_comp_flt_id) {
200 if (!(px->options2 & PR_O2_USE_HTX)) {
201 ha_alert("config: %s '%s' : compression and cache filters cannot be "
202 "both enabled on non HTX proxy.\n",
203 proxy_type_str(px), px->id);
204 return 1;
205 }
206 if (ignore)
207 cconf->flags |= CACHE_FLT_F_IGNORE_CNT_ENC;
208 break;
209 }
210 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100211 return 0;
212}
213
214static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100215cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
216{
217 if (!(chn->flags & CF_ISRESP))
218 return 1;
219
220 if (filter->ctx == NULL) {
221 struct cache_st *st;
222
Willy Tarreaubafbe012017-11-24 17:34:44 +0100223 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100224 if (st == NULL)
225 return -1;
226
227 st->hdrs_len = 0;
228 st->first_block = NULL;
229 filter->ctx = st;
230 }
231
William Lallemand4da3f8a2017-10-31 14:33:34 +0100232 return 1;
233}
234
235static int
William Lallemand49dc0482017-11-24 14:33:54 +0100236cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
237{
238 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100239 struct cache_flt_conf *cconf = FLT_CONF(filter);
240 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100241 struct shared_context *shctx = shctx_ptr(cache);
242
243 if (!(chn->flags & CF_ISRESP))
244 return 1;
245
246 /* Everything should be released in the http_end filter, but we need to do it
247 * there too, in case of errors */
248
249 if (st && st->first_block) {
250
251 shctx_lock(shctx);
252 shctx_row_dec_hot(shctx, st->first_block);
253 shctx_unlock(shctx);
254
255 }
256 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100257 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100258 filter->ctx = NULL;
259 }
260
261 return 1;
262}
263
264
265static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100266cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
267{
268 struct cache_st *st = filter->ctx;
269
William Lallemand4da3f8a2017-10-31 14:33:34 +0100270 if (!(msg->chn->flags & CF_ISRESP) || !st)
271 return 1;
272
Christopher Faulet67658c92018-12-06 21:59:39 +0100273 if (st->first_block) {
274 register_data_filter(s, msg->chn, filter);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100275 if (!IS_HTX_STRM(s))
276 st->hdrs_len = msg->sov;
Christopher Faulet67658c92018-12-06 21:59:39 +0100277 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100278 return 1;
279}
280
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200281static inline void disable_cache_entry(struct cache_st *st,
282 struct filter *filter, struct shared_context *shctx)
283{
284 struct cache_entry *object;
285
286 object = (struct cache_entry *)st->first_block->data;
287 filter->ctx = NULL; /* disable cache */
288 shctx_lock(shctx);
289 shctx_row_dec_hot(shctx, st->first_block);
290 object->eb.key = 0;
291 shctx_unlock(shctx);
292 pool_free(pool_head_cache_st, st);
293}
294
William Lallemand4da3f8a2017-10-31 14:33:34 +0100295static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100296cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
297 unsigned int offset, unsigned int len)
298{
Christopher Faulet95220e22018-12-07 17:34:39 +0100299 struct cache_flt_conf *cconf = FLT_CONF(filter);
300 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100301 struct cache_st *st = filter->ctx;
302 struct htx *htx = htxbuf(&msg->chn->buf);
303 struct htx_blk *blk;
304 struct htx_ret htx_ret;
305 struct cache_entry *object;
306 int ret, to_forward = 0;
307
308 if (!len)
309 return len;
310
311 if (!st->first_block) {
312 unregister_data_filter(s, msg->chn, filter);
313 return len;
314 }
315 object = (struct cache_entry *)st->first_block->data;
316
317 htx_ret = htx_find_blk(htx, offset);
318 blk = htx_ret.blk;
319 offset = htx_ret.ret;
320
321 while (blk && len) {
322 struct shared_block *fb;
323 enum htx_blk_type type = htx_get_blk_type(blk);
324 uint32_t sz = htx_get_blksz(blk);
325 struct ist v;
326
327 switch (type) {
328 case HTX_BLK_UNUSED:
329 break;
330
331 case HTX_BLK_DATA:
332 case HTX_BLK_TLR:
333 v = htx_get_blk_value(htx, blk);
334 v.ptr += offset;
335 v.len -= offset;
336 if (v.len > len)
337 v.len = len;
338
339 shctx_lock(shctx);
340 fb = shctx_row_reserve_hot(shctx, st->first_block, v.len);
341 if (!fb) {
342 shctx_unlock(shctx);
343 goto no_cache;
344 }
345 shctx_unlock(shctx);
346
347 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
348 (unsigned char *)v.ptr, v.len);
349 if (ret < 0)
350 goto no_cache;
351
352 if (type == HTX_BLK_DATA)
353 object->data_len += v.len;
354 to_forward += v.len;
355 len -= v.len;
356 break;
357
358 default:
359 sz -= offset;
360 if (sz > len)
361 sz = len;
362 to_forward += sz;
363 len -= sz;
364 break;
365 }
366
367 offset = 0;
368 blk = htx_get_next_blk(htx, blk);
369 }
370
371 return to_forward;
372
373 no_cache:
374 disable_cache_entry(st, filter, shctx);
375 unregister_data_filter(s, msg->chn, filter);
376 return len;
377}
378
379static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100380cache_store_http_forward_data(struct stream *s, struct filter *filter,
381 struct http_msg *msg, unsigned int len)
382{
383 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100384 struct cache_flt_conf *cconf = FLT_CONF(filter);
385 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100386 int ret;
387
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200388 ret = 0;
389
William Lallemand4da3f8a2017-10-31 14:33:34 +0100390 /*
391 * We need to skip the HTTP headers first, because we saved them in the
392 * http-response action.
393 */
Christopher Faulet67658c92018-12-06 21:59:39 +0100394 if (!(msg->chn->flags & CF_ISRESP) || !st) {
395 /* should never happen */
396 unregister_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100397 return len;
Christopher Faulet67658c92018-12-06 21:59:39 +0100398 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100399
400 if (!len) {
Joseph Herlant8dae5b32018-11-15 14:07:53 -0800401 /* Nothing to forward */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100402 ret = len;
403 }
William Lallemand10935bc2017-11-14 14:39:23 +0100404 else if (st->hdrs_len >= len) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100405 /* Forward part of headers */
406 ret = len;
407 st->hdrs_len -= len;
408 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100409 else {
William Lallemand10935bc2017-11-14 14:39:23 +0100410 /* Forward data */
Christopher Faulet67658c92018-12-06 21:59:39 +0100411 if (st->first_block) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200412 int to_append, append;
413 struct shared_block *fb;
414
415 to_append = MIN(ci_contig_data(msg->chn), len - st->hdrs_len);
416
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200417 shctx_lock(shctx);
418 fb = shctx_row_reserve_hot(shctx, st->first_block, to_append);
419 if (!fb) {
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100420 shctx_unlock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200421 disable_cache_entry(st, filter, shctx);
Christopher Faulet67658c92018-12-06 21:59:39 +0100422 unregister_data_filter(s, msg->chn, filter);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200423 return len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100424 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200425 shctx_unlock(shctx);
426
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200427 /* Skip remaining headers to fill the cache */
428 c_adv(msg->chn, st->hdrs_len);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200429 append = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
430 (unsigned char *)ci_head(msg->chn), to_append);
431 ret = st->hdrs_len + to_append - append;
432 /* Rewind the buffer to forward all data */
433 c_rew(msg->chn, st->hdrs_len);
434 st->hdrs_len = 0;
Christopher Faulet67658c92018-12-06 21:59:39 +0100435 if (ret < 0) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200436 disable_cache_entry(st, filter, shctx);
Christopher Faulet67658c92018-12-06 21:59:39 +0100437 unregister_data_filter(s, msg->chn, filter);
438 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100439 }
Christopher Faulet67658c92018-12-06 21:59:39 +0100440 else {
441 /* should never happen */
442 unregister_data_filter(s, msg->chn, filter);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200443 ret = len;
Christopher Faulet67658c92018-12-06 21:59:39 +0100444 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100445 }
446
447 if ((ret != len) ||
448 (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret))
449 task_wakeup(s->task, TASK_WOKEN_MSG);
450
451 return ret;
452}
453
454static int
455cache_store_http_end(struct stream *s, struct filter *filter,
456 struct http_msg *msg)
457{
458 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100459 struct cache_flt_conf *cconf = FLT_CONF(filter);
460 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100461 struct shared_context *shctx = shctx_ptr(cache);
462 struct cache_entry *object;
463
464 if (!(msg->chn->flags & CF_ISRESP))
465 return 1;
466
467 if (st && st->first_block) {
468
469 object = (struct cache_entry *)st->first_block->data;
470
471 /* does not need to test if the insertion worked, if it
472 * doesn't, the blocks will be reused anyway */
473
474 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100475 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
476 object->eb.key = 0;
477 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100478 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100479 shctx_row_dec_hot(shctx, st->first_block);
480 shctx_unlock(shctx);
481
482 }
483 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100484 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100485 filter->ctx = NULL;
486 }
487
488 return 1;
489}
490
491 /*
492 * This intends to be used when checking HTTP headers for some
493 * word=value directive. Return a pointer to the first character of value, if
494 * the word was not found or if there wasn't any value assigned ot it return NULL
495 */
496char *directive_value(const char *sample, int slen, const char *word, int wlen)
497{
498 int st = 0;
499
500 if (slen < wlen)
501 return 0;
502
503 while (wlen) {
504 char c = *sample ^ *word;
505 if (c && c != ('A' ^ 'a'))
506 return NULL;
507 sample++;
508 word++;
509 slen--;
510 wlen--;
511 }
512
513 while (slen) {
514 if (st == 0) {
515 if (*sample != '=')
516 return NULL;
517 sample++;
518 slen--;
519 st = 1;
520 continue;
521 } else {
522 return (char *)sample;
523 }
524 }
525
526 return NULL;
527}
528
529/*
530 * Return the maxage in seconds of an HTTP response.
531 * Compute the maxage using either:
532 * - the assigned max-age of the cache
533 * - the s-maxage directive
534 * - the max-age directive
535 * - (Expires - Data) headers
536 * - the default-max-age of the cache
537 *
538 */
William Lallemand49b44532017-11-24 18:53:43 +0100539int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100540{
541 struct http_txn *txn = s->txn;
542 struct hdr_ctx ctx;
543
544 int smaxage = -1;
545 int maxage = -1;
546
547
William Lallemand4da3f8a2017-10-31 14:33:34 +0100548 ctx.idx = 0;
549
550 /* loop on the Cache-Control values */
Willy Tarreau178b9872018-06-19 07:13:36 +0200551 while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100552 char *directive = ctx.line + ctx.val;
553 char *value;
554
555 value = directive_value(directive, ctx.vlen, "s-maxage", 8);
556 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200557 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100558
559 chunk_strncat(chk, value, ctx.vlen - 8 + 1);
560 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200561 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100562 }
563
564 value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
565 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200566 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100567
568 chunk_strncat(chk, value, ctx.vlen - 7 + 1);
569 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200570 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100571 }
572 }
573
574 /* TODO: Expires - Data */
575
576
577 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100578 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100579
580 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100581 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100582
William Lallemand49b44532017-11-24 18:53:43 +0100583 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100584
585}
586
587
William Lallemanda400a3a2017-11-20 19:13:12 +0100588static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
589{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200590 struct cache_entry *object = (struct cache_entry *)block->data;
591
592 if (first == block && object->eb.key)
593 eb32_delete(&object->eb);
594 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100595}
596
William Lallemand41db4602017-10-30 11:15:51 +0100597/*
598 * This fonction will store the headers of the response in a buffer and then
599 * register a filter to store the data
600 */
601enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
602 struct session *sess, struct stream *s, int flags)
603{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200604 unsigned int age;
605 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100606 struct http_txn *txn = s->txn;
607 struct http_msg *msg = &txn->rsp;
608 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100609 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100610 struct cache_flt_conf *cconf = rule->arg.act.p[0];
611 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100612 struct cache_entry *object;
613
William Lallemand4da3f8a2017-10-31 14:33:34 +0100614 /* Don't cache if the response came from a cache */
615 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
616 s->target == &http_cache_applet.obj_type) {
617 goto out;
618 }
619
620 /* cache only HTTP/1.1 */
621 if (!(txn->req.flags & HTTP_MSGF_VER_11))
622 goto out;
623
624 /* cache only GET method */
625 if (txn->meth != HTTP_METH_GET)
626 goto out;
627
628 /* cache only 200 status code */
629 if (txn->status != 200)
630 goto out;
631
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100632 if (IS_HTX_STRM(s)) {
633 struct htx *htx = htxbuf(&s->res.buf);
634 struct http_hdr_ctx ctx;
635 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100636
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100637 /* Do not cache too big objects. */
638 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
639 htx->data + htx->extra > shctx->max_obj_size)
640 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100641
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100642 /* Does not manage Vary at the moment. We will need a secondary key later for that */
643 ctx.blk = NULL;
644 if (http_find_header(htx, ist("Vary"), &ctx, 0))
645 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100646
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100647 htx_check_response_for_cacheability(s, &s->res);
648
649 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
650 goto out;
651
652 age = 0;
653 ctx.blk = NULL;
654 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
655 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
656 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
657 hdr_age = CACHE_ENTRY_MAX_AGE;
658 age = hdr_age;
659 }
660 http_remove_header(htx, &ctx);
661 }
662
663 chunk_reset(&trash);
664 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
665 struct htx_blk *blk = htx_get_blk(htx, pos);
Christopher Fauletafd819c2018-12-11 08:57:45 +0100666 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100667 uint32_t sz = htx_get_blksz(blk);
668
Christopher Fauletafd819c2018-12-11 08:57:45 +0100669 /* Check if we need to skip 'Content-encoding' header or not */
670 if ((msg->flags & HTTP_MSGF_COMPRESSING) && /* Compression in progress */
671 (cconf->flags & CACHE_FLT_F_IGNORE_CNT_ENC) && /* Compression before the cache */
672 (type == HTX_BLK_HDR)) {
673 struct ist n = htx_get_blk_name(htx, blk);
674 struct ist v = htx_get_blk_value(htx, blk);
675
676 if (isteq(n, ist("content-encoding")))
677 continue;
678 if (!(msg->flags & HTTP_MSGF_TE_CHNK) &&
679 isteq(n, ist("transfer-encoding")) &&
680 isteqi(v, ist("chunked")))
681 continue;
682 }
683
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100684 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
Christopher Fauletafd819c2018-12-11 08:57:45 +0100685 if (type == HTX_BLK_EOH)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100686 break;
687 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
688 }
689 }
690 else {
691 struct hdr_ctx ctx;
692
693 /* Do not cache too big objects. */
694 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
695 msg->sov + msg->body_len > shctx->max_obj_size)
696 goto out;
697
698 /* Does not manage Vary at the moment. We will need a secondary key later for that */
699 ctx.idx = 0;
700 if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx))
701 goto out;
702
703 check_response_for_cacheability(s, &s->res);
704
705 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
706 goto out;
707
708 age = 0;
709 ctx.idx = 0;
710 if (http_find_header2("Age", 3, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
711 if (!strl2llrc(ctx.line + ctx.val, ctx.vlen, &hdr_age) && hdr_age > 0) {
712 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
713 hdr_age = CACHE_ENTRY_MAX_AGE;
714 age = hdr_age;
715 }
716 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200717 }
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200718 }
719
William Lallemand4da3f8a2017-10-31 14:33:34 +0100720 shctx_lock(shctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100721 if (IS_HTX_STRM(s))
722 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
723 else
724 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + msg->sov);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100725 if (!first) {
726 shctx_unlock(shctx);
727 goto out;
728 }
729 shctx_unlock(shctx);
730
Willy Tarreau1093a452018-04-06 19:02:25 +0200731 /* the received memory is not initialized, we need at least to mark
732 * the object as not indexed yet.
733 */
734 object = (struct cache_entry *)first->data;
735 object->eb.node.leaf_p = NULL;
736 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200737 object->age = age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100738 if (IS_HTX_STRM(s)) {
739 object->hdrs_len = trash.data;
740 object->data_len = 0;
741 }
742 else
743 object->eoh = msg->eoh;
Willy Tarreau1093a452018-04-06 19:02:25 +0200744
William Lallemand4da3f8a2017-10-31 14:33:34 +0100745 /* reserve space for the cache_entry structure */
746 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200747 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100748 /* cache the headers in a http action because it allows to chose what
749 * to cache, for example you might want to cache a response before
750 * modifying some HTTP headers, or on the contrary after modifying
751 * those headers.
752 */
753
754 /* does not need to be locked because it's in the "hot" list,
755 * copy the headers */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100756 if (IS_HTX_STRM(s)) {
757 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
758 goto out;
759 }
760 else {
761 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)ci_head(&s->res), msg->sov) < 0)
762 goto out;
763 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100764
765 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet95220e22018-12-07 17:34:39 +0100766 list_for_each_entry(filter, &s->strm_flt.filters, list) {
767 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
768 if (filter->ctx) {
769 struct cache_st *cache_ctx = filter->ctx;
770 struct cache_entry *old;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100771
Christopher Faulet95220e22018-12-07 17:34:39 +0100772 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100773
Christopher Faulet95220e22018-12-07 17:34:39 +0100774 object->eb.key = (*(unsigned int *)&txn->cache_hash);
775 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
776 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100777
Christopher Faulet95220e22018-12-07 17:34:39 +0100778 shctx_lock(shctx);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100779
Christopher Faulet95220e22018-12-07 17:34:39 +0100780 old = entry_exist(cconf->c.cache, txn->cache_hash);
781 if (old) {
782 eb32_delete(&old->eb);
783 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100784 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100785 shctx_unlock(shctx);
786
787 /* store latest value and expiration time */
788 object->latest_validation = now.tv_sec;
789 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100790 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100791 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100792 }
793 }
794
795out:
796 /* if does not cache */
797 if (first) {
798 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100799 first->len = 0;
800 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100801 shctx_row_dec_hot(shctx, first);
802 shctx_unlock(shctx);
803 }
804
William Lallemand41db4602017-10-30 11:15:51 +0100805 return ACT_RET_CONT;
806}
807
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200808#define HTTP_CACHE_INIT 0 /* Initial state. */
809#define HTTP_CACHE_HEADER 1 /* Cache entry headers forwarded. */
810#define HTTP_CACHE_FWD 2 /* Cache entry completely forwarded. */
811#define HTTP_CACHE_END 3 /* Cache entry treatment terminated. */
William Lallemand77c11972017-10-31 20:43:01 +0100812
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100813#define HTX_CACHE_INIT 0 /* Initial state. */
814#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
815#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
816#define HTX_CACHE_EOD 3 /* Cache entry data forwarded. DATA->TLR transition */
817#define HTX_CACHE_TLR 4 /* Cache entry trailers forwarding */
818#define HTX_CACHE_EOM 5 /* Cache entry completely forwarded. Finish the HTX message */
819#define HTX_CACHE_END 6 /* Cache entry treatment terminated */
820
William Lallemandecb73b12017-11-24 14:33:55 +0100821static void http_cache_applet_release(struct appctx *appctx)
822{
Christopher Faulet95220e22018-12-07 17:34:39 +0100823 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100824 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100825 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100826 struct shared_block *first = block_ptr(cache_ptr);
827
828 shctx_lock(shctx_ptr(cache));
829 shctx_row_dec_hot(shctx_ptr(cache), first);
830 shctx_unlock(shctx_ptr(cache));
831}
832
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100833static size_t htx_cache_dump_headers(struct appctx *appctx, struct htx *htx)
834{
Christopher Faulet95220e22018-12-07 17:34:39 +0100835 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
836 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100837 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
838 struct shared_block *shblk = appctx->ctx.cache.next;
839 struct buffer *tmp = get_trash_chunk();
840 char *end;
841 unsigned int offset, len, age;
842
843 offset = appctx->ctx.cache.offset;
844 len = cache_ptr->hdrs_len;
845
846 /* 1. Retrieve all headers from the cache */
847 list_for_each_entry_from(shblk, &shctx->hot, list) {
848 int sz;
849
850 sz = MIN(len, shctx->block_size - offset);
851 if (!chunk_memcat(tmp, (const char *)shblk->data + offset, sz))
852 return 0;
853
854 offset += sz;
855 len -= sz;
856 if (!len)
857 break;
858 offset = 0;
859 }
860 appctx->ctx.cache.offset = offset;
861 appctx->ctx.cache.next = shblk;
862 appctx->ctx.cache.sent += b_data(tmp);
863
864 /* 2. push these headers in the HTX message */
865 offset = 0;
866 while (offset < b_data(tmp)) {
867 struct htx_blk *blk;
868 enum htx_blk_type type;
869 uint32_t info, sz;
870
871 /* Read the header's info */
872 memcpy((char *)&info, b_peek(tmp, offset), 4);
873 type = (info >> 28);
874 sz = ((type == HTX_BLK_HDR)
875 ? (info & 0xff) + ((info >> 8) & 0xfffff)
876 : info & 0xfffffff);
877
878 /* Create the block with the right type and the right size */
879 blk = htx_add_blk(htx, type, sz);
880 if (!blk)
881 return 0;
882
883 /* Copy info and data */
884 blk->info = info;
885 memcpy(htx_get_blk_ptr(htx, blk), b_peek(tmp, offset+4), sz);
886
887 /* next header */
888 offset += 4 + sz;
889 }
890
891 /* 3. Append "age" header */
892 chunk_reset(tmp);
893 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
894 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
895 age = CACHE_ENTRY_MAX_AGE;
896 end = ultoa_o(age, b_head(tmp), b_size(tmp));
897 b_set_data(tmp, end - b_head(tmp));
898
899 if (!http_add_header(htx, ist("Age"), ist2(b_head(tmp), b_data(tmp))))
900 return 0;
901
902 return htx->data;
903}
904
905static size_t htx_cache_dump_data(struct appctx *appctx, struct htx *htx,
906 enum htx_blk_type type, unsigned int len)
907{
Christopher Faulet95220e22018-12-07 17:34:39 +0100908 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
909 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100910 struct shared_block *shblk = appctx->ctx.cache.next;
911 uint32_t max = htx_free_data_space(htx);
912 unsigned int offset;
913 size_t total = 0;
914
915 offset = appctx->ctx.cache.offset;
916 if (len > max)
917 len = max;
918 if (!len)
919 goto end;
920
921 list_for_each_entry_from(shblk, &shctx->hot, list) {
922 struct ist data;
923 int sz;
924
925 sz = MIN(len, shctx->block_size - offset);
926 data = ist2((const char *)shblk->data + offset, sz);
927 if (type == HTX_BLK_DATA) {
928 if (!htx_add_data(htx, data))
929 break;
930 }
931 else { /* HTX_BLK_TLR */
932 if (!htx_add_trailer(htx, data))
933 break;
934 }
935
936 offset += sz;
937 len -= sz;
938 total += sz;
939 if (!len)
940 break;
941 offset = 0;
942 }
943 appctx->ctx.cache.offset = offset;
944 appctx->ctx.cache.next = shblk;
945 appctx->ctx.cache.sent += total;
946
947 end:
948 return total;
949}
950static void htx_cache_io_handler(struct appctx *appctx)
951{
952 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
953 struct shared_block *first = block_ptr(cache_ptr);
954 struct stream_interface *si = appctx->owner;
955 struct channel *req = si_oc(si);
956 struct channel *res = si_ic(si);
957 struct htx *req_htx, *res_htx;
958 struct buffer *errmsg;
959 size_t ret, total = 0;
960
961 res_htx = htxbuf(&res->buf);
962
963 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
964 goto out;
965
966 /* Check if the input buffer is avalaible. */
967 if (!b_size(&res->buf)) {
968 si_rx_room_blk(si);
969 goto out;
970 }
971
972 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
973 appctx->st0 = HTTP_CACHE_END;
974
975 if (appctx->st0 == HTX_CACHE_INIT) {
976 appctx->ctx.cache.next = block_ptr(cache_ptr);
977 appctx->ctx.cache.offset = sizeof(*cache_ptr);
978 appctx->ctx.cache.sent = 0;
979 appctx->st0 = HTX_CACHE_HEADER;
980 }
981
982 if (appctx->st0 == HTX_CACHE_HEADER) {
983 /* Headers must be dump at once. Otherwise it is an error */
984 ret = htx_cache_dump_headers(appctx, res_htx);
985 if (!ret)
986 goto error;
987
988 total += ret;
989 if (cache_ptr->data_len)
990 appctx->st0 = HTX_CACHE_DATA;
991 else if (first->len > sizeof(*cache_ptr) + appctx->ctx.cache.sent) {
992 /* Headers have benn sent (hrds_len) and there is no data
993 * (data_len == 0). So, all the remaining is the
994 * trailers */
995 appctx->st0 = HTX_CACHE_EOD;
996 }
997 else
998 appctx->st0 = HTX_CACHE_EOM;
999 }
1000
1001 if (appctx->st0 == HTX_CACHE_DATA) {
1002 unsigned int len = cache_ptr->hdrs_len + cache_ptr->data_len - appctx->ctx.cache.sent;
1003
1004 ret = htx_cache_dump_data(appctx, res_htx, HTX_BLK_DATA, len);
1005 if (!ret) {
1006 si_rx_room_blk(si);
1007 goto out;
1008 }
1009
1010 total += ret;
1011 if (cache_ptr->hdrs_len + cache_ptr->data_len == appctx->ctx.cache.sent) {
1012 if (first->len > sizeof(*cache_ptr) + appctx->ctx.cache.sent) {
1013 /* Headers and all data have been sent
1014 * (hrds_len + data_len == sent). So, all the remaining
1015 * is the trailers */
1016 appctx->st0 = HTX_CACHE_EOD;
1017 }
1018 else
1019 appctx->st0 = HTX_CACHE_EOM;
1020 }
1021 }
1022
1023 if (appctx->st0 == HTX_CACHE_EOD) {
1024 if (!htx_add_endof(res_htx, HTX_BLK_EOD)) {
1025 si_rx_room_blk(si);
1026 goto out;
1027 }
1028
1029 total++;
1030 appctx->st0 = HTX_CACHE_TLR;
1031 }
1032
1033 if (appctx->st0 == HTX_CACHE_TLR) {
1034 unsigned int len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1035
1036 ret = htx_cache_dump_data(appctx, res_htx, HTX_BLK_TLR, len);
1037 if (!ret) {
1038 si_rx_room_blk(si);
1039 goto out;
1040 }
1041
1042 total += ret;
1043 if (first->len == sizeof(*cache_ptr) + appctx->ctx.cache.sent)
1044 appctx->st0 = HTX_CACHE_EOM;
1045 }
1046
1047 if (appctx->st0 == HTX_CACHE_EOM) {
1048 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1049 si_rx_room_blk(si);
1050 goto out;
1051 }
1052
1053 total++;
1054 appctx->st0 = HTX_CACHE_END;
1055 }
1056
1057 end:
1058 if (appctx->st0 == HTX_CACHE_END) {
1059 /* eat the whole request */
1060 req_htx = htxbuf(&req->buf);
1061 htx_reset(req_htx);
1062 htx_to_buf(req_htx, &req->buf);
1063 co_set_data(req, 0);
1064 res->flags |= CF_READ_NULL;
1065 si_shutr(si);
1066 }
1067
1068 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
1069 si_shutw(si);
1070
1071 if (appctx->st0 == HTX_CACHE_END) {
1072 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
1073 si_shutr(si);
1074 res->flags |= CF_READ_NULL;
1075 }
1076 }
1077 out:
1078 if (total) {
1079 res->total += total;
1080 res->flags |= CF_READ_PARTIAL;
1081 }
1082
1083 /* we have left the request in the buffer for the case where we
1084 * process a POST, and this automatically re-enables activity on
1085 * read. It's better to indicate that we want to stop reading when
1086 * we're sending, so that we know there's at most one direction
1087 * deciding to wake the applet up. It saves it from looping when
1088 * emitting large blocks into small TCP windows.
1089 */
1090 htx_to_buf(res_htx, &res->buf);
1091 if (!channel_is_empty(res))
1092 si_stop_get(si);
1093 return;
1094
1095 error:
1096 /* Sent and HTTP error 500 */
1097 b_reset(&res->buf);
1098 errmsg = &htx_err_chunks[HTTP_ERR_500];
1099 res->buf.data = b_data(errmsg);
1100 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1101 res_htx = htx_from_buf(&res->buf);
1102
1103 total = res_htx->data;
1104 appctx->st0 = HTX_CACHE_END;
1105 goto end;
1106}
1107
1108
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001109/*
1110 * Append an "Age" header into <chn> channel for this <ce> cache entry.
Joseph Herlant8dae5b32018-11-15 14:07:53 -08001111 * This is the responsibility of the caller to insure there is enough
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001112 * data in the channel.
1113 *
1114 * Returns the number of bytes inserted if succeeded, 0 if failed.
1115 */
1116static int cache_channel_append_age_header(struct cache_entry *ce, struct channel *chn)
1117{
1118 unsigned int age;
1119
1120 age = MAX(0, (int)(now.tv_sec - ce->latest_validation)) + ce->age;
1121 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1122 age = CACHE_ENTRY_MAX_AGE;
1123
1124 chunk_reset(&trash);
1125 chunk_printf(&trash, "Age: %u", age);
1126
1127 return ci_insert_line2(chn, ce->eoh, trash.area, trash.data);
1128}
1129
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001130static int cache_channel_row_data_get(struct appctx *appctx, int len)
William Lallemand77c11972017-10-31 20:43:01 +01001131{
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001132 int ret, total;
William Lallemand77c11972017-10-31 20:43:01 +01001133 struct stream_interface *si = appctx->owner;
1134 struct channel *res = si_ic(si);
Christopher Faulet95220e22018-12-07 17:34:39 +01001135 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1136 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001137 struct shared_context *shctx = shctx_ptr(cache);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001138 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1139 struct shared_block *blk, *next = appctx->ctx.cache.next;
1140 int offset;
1141
1142 total = 0;
1143 offset = 0;
1144
1145 if (!next) {
1146 offset = sizeof(struct cache_entry);
1147 next = block_ptr(cache_ptr);
1148 }
1149
1150 blk = next;
1151 list_for_each_entry_from(blk, &shctx->hot, list) {
1152 int sz;
1153
1154 if (len <= 0)
1155 break;
1156
1157 sz = MIN(len, shctx->block_size - offset);
1158
1159 ret = ci_putblk(res, (const char *)blk->data + offset, sz);
1160 if (unlikely(offset))
1161 offset = 0;
1162 if (ret <= 0) {
1163 if (ret == -3 || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001164 si_rx_room_blk(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001165 break;
1166 }
1167 return -1;
1168 }
1169
1170 total += sz;
1171 len -= sz;
1172 }
1173 appctx->ctx.cache.next = blk;
1174
1175 return total;
1176}
1177
1178static void http_cache_io_handler(struct appctx *appctx)
1179{
1180 struct stream_interface *si = appctx->owner;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001181 struct stream *s = si_strm(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001182 struct channel *res = si_ic(si);
1183 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
William Lallemand77c11972017-10-31 20:43:01 +01001184 struct shared_block *first = block_ptr(cache_ptr);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001185 unsigned int *sent = &appctx->ctx.cache.sent;
1186
1187 if (IS_HTX_STRM(s))
1188 return htx_cache_io_handler(appctx);
William Lallemand77c11972017-10-31 20:43:01 +01001189
1190 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1191 goto out;
1192
Joseph Herlant8dae5b32018-11-15 14:07:53 -08001193 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001194 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01001195 /* buf.size==0 means we failed to get a buffer and were
1196 * already subscribed to a wait list to get a buffer.
1197 */
William Lallemand77c11972017-10-31 20:43:01 +01001198 goto out;
1199 }
1200
1201 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
1202 appctx->st0 = HTTP_CACHE_END;
1203
1204 /* buffer are aligned there, should be fine */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001205 if (appctx->st0 == HTTP_CACHE_HEADER || appctx->st0 == HTTP_CACHE_INIT) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001206 int len = first->len - *sent - sizeof(struct cache_entry);
William Lallemand55e76742017-11-21 20:01:28 +01001207
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001208 if (len > 0) {
1209 int ret;
1210
1211 ret = cache_channel_row_data_get(appctx, len);
1212 if (ret == -1)
1213 appctx->st0 = HTTP_CACHE_END;
1214 else
1215 *sent += ret;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001216 if (appctx->st0 == HTTP_CACHE_INIT && *sent > cache_ptr->eoh &&
1217 cache_channel_append_age_header(cache_ptr, res))
1218 appctx->st0 = HTTP_CACHE_HEADER;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001219 }
1220 else {
1221 *sent = 0;
1222 appctx->st0 = HTTP_CACHE_FWD;
William Lallemand77c11972017-10-31 20:43:01 +01001223 }
William Lallemand77c11972017-10-31 20:43:01 +01001224 }
1225
1226 if (appctx->st0 == HTTP_CACHE_FWD) {
1227 /* eat the whole request */
Willy Tarreau178b9872018-06-19 07:13:36 +02001228 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 +01001229 res->flags |= CF_READ_NULL;
1230 si_shutr(si);
1231 }
1232
1233 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
1234 si_shutw(si);
1235out:
1236 ;
1237}
1238
Christopher Faulet95220e22018-12-07 17:34:39 +01001239static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001240{
1241 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001242 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001243
Christopher Faulet95220e22018-12-07 17:34:39 +01001244 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001245 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001246 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001247 }
1248
1249 /* check if a cache filter was already registered with this cache
1250 * name, if that's the case, must use it. */
1251 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001252 if (fconf->id == cache_store_flt_id) {
1253 cconf = fconf->conf;
1254 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1255 rule->arg.act.p[0] = cconf;
1256 return 1;
1257 }
William Lallemand41db4602017-10-30 11:15:51 +01001258 }
1259 }
1260
Christopher Faulet95220e22018-12-07 17:34:39 +01001261 /* Create the filter cache config */
1262 cconf = calloc(1, sizeof(*cconf));
1263 if (!cconf) {
1264 memprintf(err, "out of memory\n");
1265 goto err;
1266 }
1267 cconf->flags = 0;
1268 cconf->c.name = strdup(name);
1269 if (!cconf->c.name) {
1270 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001271 goto err;
1272 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001273
William Lallemand41db4602017-10-30 11:15:51 +01001274 /* register a filter to fill the cache buffer */
1275 fconf = calloc(1, sizeof(*fconf));
1276 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001277 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001278 goto err;
1279 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001280 fconf->id = cache_store_flt_id;
1281 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001282 fconf->ops = &cache_ops;
1283 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1284
Christopher Faulet95220e22018-12-07 17:34:39 +01001285 rule->arg.act.p[0] = cconf;
1286 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001287
Christopher Faulet95220e22018-12-07 17:34:39 +01001288 err:
1289 free(cconf);
1290 return 0;
1291}
1292
1293enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1294 struct act_rule *rule, char **err)
1295{
1296 rule->action = ACT_CUSTOM;
1297 rule->action_ptr = http_action_store_cache;
1298
1299 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1300 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001301
Christopher Faulet95220e22018-12-07 17:34:39 +01001302 (*orig_arg)++;
1303 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001304}
1305
William Lallemandf528fff2017-11-23 19:43:17 +01001306/* This produces a sha1 hash of the concatenation of the first
1307 * occurrence of the Host header followed by the path component if it
1308 * begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001309int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001310{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001311 struct http_txn *txn = s->txn;
William Lallemandf528fff2017-11-23 19:43:17 +01001312 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001313 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001314
William Lallemandf528fff2017-11-23 19:43:17 +01001315 trash = get_trash_chunk();
1316
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001317 if (IS_HTX_STRM(s)) {
1318 struct htx *htx = htxbuf(&s->req.buf);
1319 struct htx_sl *sl;
1320 struct http_hdr_ctx ctx;
1321 struct ist path;
William Lallemandf528fff2017-11-23 19:43:17 +01001322
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001323 ctx.blk = NULL;
1324 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1325 return 0;
1326 chunk_memcat(trash, ctx.value.ptr, ctx.value.len);
1327
1328 sl = http_find_stline(htx);
1329 path = http_get_path(htx_sl_req_uri(sl));
1330 if (!path.ptr)
1331 return 0;
1332 chunk_memcat(trash, path.ptr, path.len);
1333 }
1334 else {
1335 struct hdr_ctx ctx;
1336 char *path;
1337 char *end;
1338
1339 /* retrive the host */
1340 ctx.idx = 0;
1341 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx))
1342 return 0;
1343 chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen);
1344
1345 /* now retrieve the path */
1346 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1347 path = http_txn_get_path(txn);
1348 if (!path)
1349 return 0;
1350 chunk_strncat(trash, path, end - path);
1351 }
William Lallemandf528fff2017-11-23 19:43:17 +01001352
1353 /* hash everything */
1354 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001355 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001356 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1357
1358 return 1;
1359}
1360
William Lallemand41db4602017-10-30 11:15:51 +01001361enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1362 struct session *sess, struct stream *s, int flags)
1363{
William Lallemand77c11972017-10-31 20:43:01 +01001364
William Lallemand77c11972017-10-31 20:43:01 +01001365 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001366 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1367 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001368
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001369 if (IS_HTX_STRM(s))
1370 htx_check_request_for_cacheability(s, &s->req);
1371 else
1372 check_request_for_cacheability(s, &s->req);
1373
Willy Tarreau504455c2017-12-22 17:47:35 +01001374 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1375 return ACT_RET_CONT;
1376
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001377 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001378 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001379
Willy Tarreau504455c2017-12-22 17:47:35 +01001380 if (s->txn->flags & TX_CACHE_IGNORE)
1381 return ACT_RET_CONT;
1382
William Lallemanda400a3a2017-11-20 19:13:12 +01001383 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001384 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001385 if (res) {
1386 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001387 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1388 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001389 s->target = &http_cache_applet.obj_type;
1390 if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
1391 appctx->st0 = HTTP_CACHE_INIT;
1392 appctx->rule = rule;
1393 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001394 appctx->ctx.cache.next = NULL;
1395 appctx->ctx.cache.sent = 0;
Olivier Houchardfccf8402017-11-01 14:04:02 +01001396 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001397 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001398 shctx_lock(shctx_ptr(cache));
1399 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1400 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001401 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001402 }
1403 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001404 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001405 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001406}
1407
1408
1409enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1410 struct act_rule *rule, char **err)
1411{
William Lallemand41db4602017-10-30 11:15:51 +01001412 rule->action = ACT_CUSTOM;
1413 rule->action_ptr = http_action_req_cache_use;
1414
Christopher Faulet95220e22018-12-07 17:34:39 +01001415 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001416 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001417
1418 (*orig_arg)++;
1419 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001420}
1421
1422int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1423{
1424 int err_code = 0;
1425
1426 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1427
1428 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001429 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1430 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001431 err_code |= ERR_ALERT | ERR_ABORT;
1432 goto out;
1433 }
1434
1435 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1436 err_code |= ERR_ABORT;
1437 goto out;
1438 }
1439
1440 if (tmp_cache_config == NULL) {
1441 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1442 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001443 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001444 err_code |= ERR_ALERT | ERR_ABORT;
1445 goto out;
1446 }
1447
1448 strlcpy2(tmp_cache_config->id, args[1], 33);
1449 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001450 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1451 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001452 err_code |= ERR_WARN;
1453 }
William Lallemand49b44532017-11-24 18:53:43 +01001454 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001455 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001456 tmp_cache_config->maxobjsz = 0;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001457 tmp_cache_config->flags = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001458 }
1459 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001460 unsigned long int maxsize;
1461 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001462
1463 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1464 err_code |= ERR_ABORT;
1465 goto out;
1466 }
1467
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001468 maxsize = strtoul(args[1], &err, 10);
1469 if (err == args[1] || *err != '\0') {
1470 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1471 file, linenum, args[1]);
1472 err_code |= ERR_ABORT;
1473 goto out;
1474 }
1475
1476 if (maxsize > (UINT_MAX >> 20)) {
1477 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1478 file, linenum, args[1], UINT_MAX >> 20);
1479 err_code |= ERR_ABORT;
1480 goto out;
1481 }
1482
William Lallemand41db4602017-10-30 11:15:51 +01001483 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001484 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001485 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001486 } else if (strcmp(args[0], "max-age") == 0) {
1487 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1488 err_code |= ERR_ABORT;
1489 goto out;
1490 }
1491
1492 if (!*args[1]) {
1493 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1494 file, linenum, args[0]);
1495 err_code |= ERR_WARN;
1496 }
1497
1498 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001499 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001500 unsigned int maxobjsz;
1501 char *err;
1502
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001503 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1504 err_code |= ERR_ABORT;
1505 goto out;
1506 }
1507
1508 if (!*args[1]) {
1509 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1510 file, linenum, args[0]);
1511 err_code |= ERR_WARN;
1512 }
1513
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001514 maxobjsz = strtoul(args[1], &err, 10);
1515 if (err == args[1] || *err != '\0') {
1516 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1517 file, linenum, args[1]);
1518 err_code |= ERR_ABORT;
1519 goto out;
1520 }
1521 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001522 }
1523 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001524 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001525 err_code |= ERR_ALERT | ERR_FATAL;
1526 goto out;
1527 }
1528out:
1529 return err_code;
1530}
1531
1532/* once the cache section is parsed */
1533
1534int cfg_post_parse_section_cache()
1535{
1536 struct shared_context *shctx;
1537 int err_code = 0;
1538 int ret_shctx;
1539
1540 if (tmp_cache_config) {
1541 struct cache *cache;
1542
1543 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001544 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001545 err_code |= ERR_FATAL | ERR_ALERT;
1546 goto out;
1547 }
1548
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001549 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001550 /* Default max. file size is a 256th of the cache size. */
1551 tmp_cache_config->maxobjsz =
1552 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001553 }
1554 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1555 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1556 err_code |= ERR_FATAL | ERR_ALERT;
1557 goto out;
1558 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001559
1560 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE,
1561 tmp_cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001562
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001563 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001564 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001565 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001566 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001567 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001568
1569 err_code |= ERR_FATAL | ERR_ALERT;
1570 goto out;
1571 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001572 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +01001573 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
1574 cache = (struct cache *)shctx->data;
1575 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001576 LIST_ADDQ(&caches, &cache->list);
1577 }
1578out:
1579 free(tmp_cache_config);
1580 tmp_cache_config = NULL;
1581 return err_code;
1582
1583}
1584
1585/*
1586 * Resolve the cache name to a pointer once the file is completely read.
1587 */
1588int cfg_cache_postparser()
1589{
William Lallemand41db4602017-10-30 11:15:51 +01001590 struct cache *cache;
William Lallemand41db4602017-10-30 11:15:51 +01001591 int err = 0;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001592
1593 /* Check if the cache is used by HTX and legacy HTTP proxies in same
1594 * time
1595 */
1596 list_for_each_entry(cache, &caches, list) {
1597 if ((cache->flags & (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) == (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) {
1598 ha_alert("Cache '%s': cannot be used by HTX and legacy HTTP proxies in same time.\n",
1599 cache->id);
1600 err++;
1601 }
1602 }
1603
William Lallemand41db4602017-10-30 11:15:51 +01001604 return err;
1605}
1606
1607
1608struct flt_ops cache_ops = {
1609 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001610 .check = cache_store_check,
1611 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001612
William Lallemand4da3f8a2017-10-31 14:33:34 +01001613 /* Handle channels activity */
1614 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001615 .channel_end_analyze = cache_store_chn_end_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001616
1617 /* Filter HTTP requests and responses */
1618 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001619 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001620 .http_end = cache_store_http_end,
1621
1622 .http_forward_data = cache_store_http_forward_data,
1623
William Lallemand41db4602017-10-30 11:15:51 +01001624};
1625
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001626static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001627{
1628 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1629 return 1;
1630
1631 return 0;
1632}
1633
1634static int cli_io_handler_show_cache(struct appctx *appctx)
1635{
1636 struct cache* cache = appctx->ctx.cli.p0;
1637 struct stream_interface *si = appctx->owner;
1638
William Lallemand1f49a362017-11-21 20:01:26 +01001639 if (cache == NULL) {
1640 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1641 }
1642
1643 list_for_each_entry_from(cache, &caches, list) {
1644 struct eb32_node *node = NULL;
1645 unsigned int next_key;
1646 struct cache_entry *entry;
1647
William Lallemand1f49a362017-11-21 20:01:26 +01001648 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001649 if (!next_key) {
1650 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1651 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001652 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001653 return 0;
1654 }
1655 }
William Lallemand1f49a362017-11-21 20:01:26 +01001656
1657 appctx->ctx.cli.p0 = cache;
1658
1659 while (1) {
1660
1661 shctx_lock(shctx_ptr(cache));
1662 node = eb32_lookup_ge(&cache->entries, next_key);
1663 if (!node) {
1664 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001665 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001666 break;
1667 }
1668
1669 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001670 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 +01001671
1672 next_key = node->key + 1;
1673 appctx->ctx.cli.i0 = next_key;
1674
1675 shctx_unlock(shctx_ptr(cache));
1676
1677 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001678 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001679 return 0;
1680 }
1681 }
1682
1683 }
1684
1685 return 1;
1686
1687}
1688
1689static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001690 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1691 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001692}};
1693
Willy Tarreau0108d902018-11-25 19:14:37 +01001694INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001695
William Lallemand41db4602017-10-30 11:15:51 +01001696static struct action_kw_list http_res_actions = {
1697 .kw = {
1698 { "cache-store", parse_cache_store },
1699 { NULL, NULL }
1700 }
1701};
1702
Willy Tarreau0108d902018-11-25 19:14:37 +01001703INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1704
William Lallemand41db4602017-10-30 11:15:51 +01001705static struct action_kw_list http_req_actions = {
1706 .kw = {
1707 { "cache-use", parse_cache_use },
1708 { NULL, NULL }
1709 }
1710};
1711
Willy Tarreau0108d902018-11-25 19:14:37 +01001712INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1713
William Lallemand41db4602017-10-30 11:15:51 +01001714struct applet http_cache_applet = {
1715 .obj_type = OBJ_TYPE_APPLET,
1716 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001717 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001718 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001719};
1720
Willy Tarreaue6552512018-11-26 11:33:13 +01001721/* config parsers for this section */
1722REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
1723REGISTER_CONFIG_POSTPARSER("cache", cfg_cache_postparser);