blob: 407d150f9dcd9f9656df3ff3fd0df5867a79c40b [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 */
Christopher Faulet99a17a22018-12-11 09:18:27 +010048#define CACHE_FLT_F_IMPLICIT_DECL 0x00000002 /* The cache filtre was implicitly declared (ie without
49 * the filter keyword) */
Christopher Fauletafd819c2018-12-11 08:57:45 +010050
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010051const char *cache_store_flt_id = "cache store filter";
William Lallemand41db4602017-10-30 11:15:51 +010052
53struct applet http_cache_applet;
54
55struct flt_ops cache_ops;
56
57struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010058 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010059 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010060 unsigned int maxage; /* max-age */
61 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020062 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010063 char id[33]; /* cache name */
Christopher Faulet1f672c52018-12-03 14:30:41 +010064 unsigned int flags; /* CACHE_F_* */
William Lallemand41db4602017-10-30 11:15:51 +010065};
66
Christopher Faulet95220e22018-12-07 17:34:39 +010067/* cache config for filters */
68struct cache_flt_conf {
69 union {
70 struct cache *cache; /* cache used by the filter */
71 char *name; /* cache name used during conf parsing */
72 } c;
73 unsigned int flags; /* CACHE_FLT_F_* */
74};
75
William Lallemand41db4602017-10-30 11:15:51 +010076/*
77 * cache ctx for filters
78 */
79struct cache_st {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010080 int hdrs_len; // field used in legacy mode only
William Lallemand41db4602017-10-30 11:15:51 +010081 struct shared_block *first_block;
82};
83
84struct cache_entry {
85 unsigned int latest_validation; /* latest validation date */
86 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020087 unsigned int age; /* Origin server "Age" header value */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +010088 unsigned int eoh; /* Origin server end of headers offset. */ // field used in legacy mode only
89
90 unsigned int hdrs_len; // field used in HTX mode only
91 unsigned int data_len; // field used in HTX mode only
92
William Lallemand41db4602017-10-30 11:15:51 +010093 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010094 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010095 unsigned char data[0];
96};
97
98#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010099#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +0100100
101static struct list caches = LIST_HEAD_INIT(caches);
102static struct cache *tmp_cache_config = NULL;
103
Willy Tarreau8ceae722018-11-26 11:58:30 +0100104DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
105
William Lallemandf528fff2017-11-23 19:43:17 +0100106struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100107{
108 struct eb32_node *node;
109 struct cache_entry *entry;
110
William Lallemandf528fff2017-11-23 19:43:17 +0100111 node = eb32_lookup(&cache->entries, (*(unsigned int *)hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100112 if (!node)
113 return NULL;
114
115 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +0100116
117 /* if that's not the right node */
118 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
119 return NULL;
120
William Lallemand08727662017-11-21 20:01:27 +0100121 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100122 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100123 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100124 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100125 entry->eb.key = 0;
126 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100127 return NULL;
128
129}
130
131static inline struct shared_context *shctx_ptr(struct cache *cache)
132{
133 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
134}
135
William Lallemand77c11972017-10-31 20:43:01 +0100136static inline struct shared_block *block_ptr(struct cache_entry *entry)
137{
138 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
139}
140
141
142
William Lallemand41db4602017-10-30 11:15:51 +0100143static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100144cache_store_init(struct proxy *px, struct flt_conf *fconf)
William Lallemand41db4602017-10-30 11:15:51 +0100145{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100146 fconf->flags |= FLT_CFG_FL_HTX;
William Lallemand41db4602017-10-30 11:15:51 +0100147 return 0;
148}
149
Christopher Faulet95220e22018-12-07 17:34:39 +0100150static void
151cache_store_deinit(struct proxy *px, struct flt_conf *fconf)
152{
153 struct cache_flt_conf *cconf = fconf->conf;
154
155 free(cconf);
156}
157
William Lallemand4da3f8a2017-10-31 14:33:34 +0100158static int
Christopher Faulet95220e22018-12-07 17:34:39 +0100159cache_store_check(struct proxy *px, struct flt_conf *fconf)
160{
161 struct cache_flt_conf *cconf = fconf->conf;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100162 struct flt_conf *f;
Christopher Faulet95220e22018-12-07 17:34:39 +0100163 struct cache *cache;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100164 int ignore = 0;
Christopher Faulet95220e22018-12-07 17:34:39 +0100165
166 /* resolve the cache name to a ptr in the filter config */
167 list_for_each_entry(cache, &caches, list) {
168 if (!strcmp(cache->id, cconf->c.name)) {
169 /* there can be only one filter per cache, so we free it there */
170 cache->flags |= ((px->options2 & PR_O2_USE_HTX)
171 ? CACHE_F_HTX
172 : CACHE_F_LEGACY_HTTP);
173
174 free(cconf->c.name);
175 cconf->c.cache = cache;
176 goto found;
177 }
178 }
179
180 ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
181 proxy_type_str(px), px->id, (char *)cconf->c.name);
182 return 1;
183
184 found:
Christopher Fauletafd819c2018-12-11 08:57:45 +0100185 /* Here <cache> points on the cache the filter must use and <cconf>
186 * points on the cache filter configuration. */
187
188 /* Check all filters for proxy <px> to know if the compression is
189 * enabled and if it is before or after the cache. When the compression
190 * is before the cache, nothing special is done. The response is stored
191 * compressed in the cache. When the compression is after the cache, the
192 * 'Content-encoding' header must be ignored because the response will
193 * be stored uncompressed. The compression will be done on the cached
Christopher Faulet99a17a22018-12-11 09:18:27 +0100194 * response too. Also check if the cache filter must be explicitly
195 * declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100196 list_for_each_entry(f, &px->filter_configs, list) {
197 if (f == fconf) {
198 ignore = 1;
199 continue;
200 }
201
Christopher Faulet99a17a22018-12-11 09:18:27 +0100202 if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
203 ha_alert("config: %s '%s': require an explicit filter declaration "
204 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
205 return 1;
206 }
207
Christopher Fauletafd819c2018-12-11 08:57:45 +0100208 if (f->id == http_comp_flt_id) {
209 if (!(px->options2 & PR_O2_USE_HTX)) {
210 ha_alert("config: %s '%s' : compression and cache filters cannot be "
211 "both enabled on non HTX proxy.\n",
212 proxy_type_str(px), px->id);
213 return 1;
214 }
215 if (ignore)
216 cconf->flags |= CACHE_FLT_F_IGNORE_CNT_ENC;
217 break;
218 }
219 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100220 return 0;
221}
222
223static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100224cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
225{
226 if (!(chn->flags & CF_ISRESP))
227 return 1;
228
229 if (filter->ctx == NULL) {
230 struct cache_st *st;
231
Willy Tarreaubafbe012017-11-24 17:34:44 +0100232 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100233 if (st == NULL)
234 return -1;
235
236 st->hdrs_len = 0;
237 st->first_block = NULL;
238 filter->ctx = st;
239 }
240
William Lallemand4da3f8a2017-10-31 14:33:34 +0100241 return 1;
242}
243
244static int
William Lallemand49dc0482017-11-24 14:33:54 +0100245cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
246{
247 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100248 struct cache_flt_conf *cconf = FLT_CONF(filter);
249 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100250 struct shared_context *shctx = shctx_ptr(cache);
251
252 if (!(chn->flags & CF_ISRESP))
253 return 1;
254
255 /* Everything should be released in the http_end filter, but we need to do it
256 * there too, in case of errors */
257
258 if (st && st->first_block) {
259
260 shctx_lock(shctx);
261 shctx_row_dec_hot(shctx, st->first_block);
262 shctx_unlock(shctx);
263
264 }
265 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100266 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100267 filter->ctx = NULL;
268 }
269
270 return 1;
271}
272
273
274static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100275cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
276{
277 struct cache_st *st = filter->ctx;
278
William Lallemand4da3f8a2017-10-31 14:33:34 +0100279 if (!(msg->chn->flags & CF_ISRESP) || !st)
280 return 1;
281
Christopher Faulet67658c92018-12-06 21:59:39 +0100282 if (st->first_block) {
283 register_data_filter(s, msg->chn, filter);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100284 if (!IS_HTX_STRM(s))
285 st->hdrs_len = msg->sov;
Christopher Faulet67658c92018-12-06 21:59:39 +0100286 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100287 return 1;
288}
289
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200290static inline void disable_cache_entry(struct cache_st *st,
291 struct filter *filter, struct shared_context *shctx)
292{
293 struct cache_entry *object;
294
295 object = (struct cache_entry *)st->first_block->data;
296 filter->ctx = NULL; /* disable cache */
297 shctx_lock(shctx);
298 shctx_row_dec_hot(shctx, st->first_block);
299 object->eb.key = 0;
300 shctx_unlock(shctx);
301 pool_free(pool_head_cache_st, st);
302}
303
William Lallemand4da3f8a2017-10-31 14:33:34 +0100304static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100305cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
306 unsigned int offset, unsigned int len)
307{
Christopher Faulet95220e22018-12-07 17:34:39 +0100308 struct cache_flt_conf *cconf = FLT_CONF(filter);
309 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100310 struct cache_st *st = filter->ctx;
311 struct htx *htx = htxbuf(&msg->chn->buf);
312 struct htx_blk *blk;
313 struct htx_ret htx_ret;
314 struct cache_entry *object;
315 int ret, to_forward = 0;
316
317 if (!len)
318 return len;
319
320 if (!st->first_block) {
321 unregister_data_filter(s, msg->chn, filter);
322 return len;
323 }
324 object = (struct cache_entry *)st->first_block->data;
325
326 htx_ret = htx_find_blk(htx, offset);
327 blk = htx_ret.blk;
328 offset = htx_ret.ret;
329
330 while (blk && len) {
331 struct shared_block *fb;
332 enum htx_blk_type type = htx_get_blk_type(blk);
333 uint32_t sz = htx_get_blksz(blk);
334 struct ist v;
335
336 switch (type) {
337 case HTX_BLK_UNUSED:
338 break;
339
340 case HTX_BLK_DATA:
341 case HTX_BLK_TLR:
342 v = htx_get_blk_value(htx, blk);
343 v.ptr += offset;
344 v.len -= offset;
345 if (v.len > len)
346 v.len = len;
347
348 shctx_lock(shctx);
349 fb = shctx_row_reserve_hot(shctx, st->first_block, v.len);
350 if (!fb) {
351 shctx_unlock(shctx);
352 goto no_cache;
353 }
354 shctx_unlock(shctx);
355
356 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
357 (unsigned char *)v.ptr, v.len);
358 if (ret < 0)
359 goto no_cache;
360
361 if (type == HTX_BLK_DATA)
362 object->data_len += v.len;
363 to_forward += v.len;
364 len -= v.len;
365 break;
366
367 default:
368 sz -= offset;
369 if (sz > len)
370 sz = len;
371 to_forward += sz;
372 len -= sz;
373 break;
374 }
375
376 offset = 0;
377 blk = htx_get_next_blk(htx, blk);
378 }
379
380 return to_forward;
381
382 no_cache:
383 disable_cache_entry(st, filter, shctx);
384 unregister_data_filter(s, msg->chn, filter);
385 return len;
386}
387
388static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100389cache_store_http_forward_data(struct stream *s, struct filter *filter,
390 struct http_msg *msg, unsigned int len)
391{
392 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100393 struct cache_flt_conf *cconf = FLT_CONF(filter);
394 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100395 int ret;
396
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200397 ret = 0;
398
William Lallemand4da3f8a2017-10-31 14:33:34 +0100399 /*
400 * We need to skip the HTTP headers first, because we saved them in the
401 * http-response action.
402 */
Christopher Faulet67658c92018-12-06 21:59:39 +0100403 if (!(msg->chn->flags & CF_ISRESP) || !st) {
404 /* should never happen */
405 unregister_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100406 return len;
Christopher Faulet67658c92018-12-06 21:59:39 +0100407 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100408
409 if (!len) {
Joseph Herlant8dae5b32018-11-15 14:07:53 -0800410 /* Nothing to forward */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100411 ret = len;
412 }
William Lallemand10935bc2017-11-14 14:39:23 +0100413 else if (st->hdrs_len >= len) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100414 /* Forward part of headers */
415 ret = len;
416 st->hdrs_len -= len;
417 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100418 else {
William Lallemand10935bc2017-11-14 14:39:23 +0100419 /* Forward data */
Christopher Faulet67658c92018-12-06 21:59:39 +0100420 if (st->first_block) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200421 int to_append, append;
422 struct shared_block *fb;
423
424 to_append = MIN(ci_contig_data(msg->chn), len - st->hdrs_len);
425
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200426 shctx_lock(shctx);
427 fb = shctx_row_reserve_hot(shctx, st->first_block, to_append);
428 if (!fb) {
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100429 shctx_unlock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200430 disable_cache_entry(st, filter, shctx);
Christopher Faulet67658c92018-12-06 21:59:39 +0100431 unregister_data_filter(s, msg->chn, filter);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200432 return len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100433 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200434 shctx_unlock(shctx);
435
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200436 /* Skip remaining headers to fill the cache */
437 c_adv(msg->chn, st->hdrs_len);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200438 append = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
439 (unsigned char *)ci_head(msg->chn), to_append);
440 ret = st->hdrs_len + to_append - append;
441 /* Rewind the buffer to forward all data */
442 c_rew(msg->chn, st->hdrs_len);
443 st->hdrs_len = 0;
Christopher Faulet67658c92018-12-06 21:59:39 +0100444 if (ret < 0) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200445 disable_cache_entry(st, filter, shctx);
Christopher Faulet67658c92018-12-06 21:59:39 +0100446 unregister_data_filter(s, msg->chn, filter);
447 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100448 }
Christopher Faulet67658c92018-12-06 21:59:39 +0100449 else {
450 /* should never happen */
451 unregister_data_filter(s, msg->chn, filter);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200452 ret = len;
Christopher Faulet67658c92018-12-06 21:59:39 +0100453 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100454 }
455
456 if ((ret != len) ||
457 (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret))
458 task_wakeup(s->task, TASK_WOKEN_MSG);
459
460 return ret;
461}
462
463static int
464cache_store_http_end(struct stream *s, struct filter *filter,
465 struct http_msg *msg)
466{
467 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100468 struct cache_flt_conf *cconf = FLT_CONF(filter);
469 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100470 struct shared_context *shctx = shctx_ptr(cache);
471 struct cache_entry *object;
472
473 if (!(msg->chn->flags & CF_ISRESP))
474 return 1;
475
476 if (st && st->first_block) {
477
478 object = (struct cache_entry *)st->first_block->data;
479
480 /* does not need to test if the insertion worked, if it
481 * doesn't, the blocks will be reused anyway */
482
483 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100484 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
485 object->eb.key = 0;
486 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100487 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100488 shctx_row_dec_hot(shctx, st->first_block);
489 shctx_unlock(shctx);
490
491 }
492 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100493 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100494 filter->ctx = NULL;
495 }
496
497 return 1;
498}
499
500 /*
501 * This intends to be used when checking HTTP headers for some
502 * word=value directive. Return a pointer to the first character of value, if
503 * the word was not found or if there wasn't any value assigned ot it return NULL
504 */
505char *directive_value(const char *sample, int slen, const char *word, int wlen)
506{
507 int st = 0;
508
509 if (slen < wlen)
510 return 0;
511
512 while (wlen) {
513 char c = *sample ^ *word;
514 if (c && c != ('A' ^ 'a'))
515 return NULL;
516 sample++;
517 word++;
518 slen--;
519 wlen--;
520 }
521
522 while (slen) {
523 if (st == 0) {
524 if (*sample != '=')
525 return NULL;
526 sample++;
527 slen--;
528 st = 1;
529 continue;
530 } else {
531 return (char *)sample;
532 }
533 }
534
535 return NULL;
536}
537
538/*
539 * Return the maxage in seconds of an HTTP response.
540 * Compute the maxage using either:
541 * - the assigned max-age of the cache
542 * - the s-maxage directive
543 * - the max-age directive
544 * - (Expires - Data) headers
545 * - the default-max-age of the cache
546 *
547 */
William Lallemand49b44532017-11-24 18:53:43 +0100548int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100549{
550 struct http_txn *txn = s->txn;
551 struct hdr_ctx ctx;
552
553 int smaxage = -1;
554 int maxage = -1;
555
556
William Lallemand4da3f8a2017-10-31 14:33:34 +0100557 ctx.idx = 0;
558
559 /* loop on the Cache-Control values */
Willy Tarreau178b9872018-06-19 07:13:36 +0200560 while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100561 char *directive = ctx.line + ctx.val;
562 char *value;
563
564 value = directive_value(directive, ctx.vlen, "s-maxage", 8);
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 - 8 + 1);
569 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200570 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100571 }
572
573 value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
574 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200575 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100576
577 chunk_strncat(chk, value, ctx.vlen - 7 + 1);
578 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200579 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100580 }
581 }
582
583 /* TODO: Expires - Data */
584
585
586 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100587 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100588
589 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100590 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100591
William Lallemand49b44532017-11-24 18:53:43 +0100592 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100593
594}
595
596
William Lallemanda400a3a2017-11-20 19:13:12 +0100597static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
598{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200599 struct cache_entry *object = (struct cache_entry *)block->data;
600
601 if (first == block && object->eb.key)
602 eb32_delete(&object->eb);
603 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100604}
605
William Lallemand41db4602017-10-30 11:15:51 +0100606/*
607 * This fonction will store the headers of the response in a buffer and then
608 * register a filter to store the data
609 */
610enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
611 struct session *sess, struct stream *s, int flags)
612{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200613 unsigned int age;
614 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100615 struct http_txn *txn = s->txn;
616 struct http_msg *msg = &txn->rsp;
617 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100618 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100619 struct cache_flt_conf *cconf = rule->arg.act.p[0];
620 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100621 struct cache_entry *object;
622
William Lallemand4da3f8a2017-10-31 14:33:34 +0100623 /* Don't cache if the response came from a cache */
624 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
625 s->target == &http_cache_applet.obj_type) {
626 goto out;
627 }
628
629 /* cache only HTTP/1.1 */
630 if (!(txn->req.flags & HTTP_MSGF_VER_11))
631 goto out;
632
633 /* cache only GET method */
634 if (txn->meth != HTTP_METH_GET)
635 goto out;
636
637 /* cache only 200 status code */
638 if (txn->status != 200)
639 goto out;
640
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100641 if (IS_HTX_STRM(s)) {
642 struct htx *htx = htxbuf(&s->res.buf);
643 struct http_hdr_ctx ctx;
644 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100645
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100646 /* Do not cache too big objects. */
647 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
648 htx->data + htx->extra > shctx->max_obj_size)
649 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100650
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100651 /* Does not manage Vary at the moment. We will need a secondary key later for that */
652 ctx.blk = NULL;
653 if (http_find_header(htx, ist("Vary"), &ctx, 0))
654 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100655
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100656 htx_check_response_for_cacheability(s, &s->res);
657
658 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
659 goto out;
660
661 age = 0;
662 ctx.blk = NULL;
663 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
664 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
665 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
666 hdr_age = CACHE_ENTRY_MAX_AGE;
667 age = hdr_age;
668 }
669 http_remove_header(htx, &ctx);
670 }
671
672 chunk_reset(&trash);
673 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
674 struct htx_blk *blk = htx_get_blk(htx, pos);
Christopher Fauletafd819c2018-12-11 08:57:45 +0100675 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100676 uint32_t sz = htx_get_blksz(blk);
677
Christopher Fauletafd819c2018-12-11 08:57:45 +0100678 /* Check if we need to skip 'Content-encoding' header or not */
679 if ((msg->flags & HTTP_MSGF_COMPRESSING) && /* Compression in progress */
680 (cconf->flags & CACHE_FLT_F_IGNORE_CNT_ENC) && /* Compression before the cache */
681 (type == HTX_BLK_HDR)) {
682 struct ist n = htx_get_blk_name(htx, blk);
683 struct ist v = htx_get_blk_value(htx, blk);
684
685 if (isteq(n, ist("content-encoding")))
686 continue;
687 if (!(msg->flags & HTTP_MSGF_TE_CHNK) &&
688 isteq(n, ist("transfer-encoding")) &&
689 isteqi(v, ist("chunked")))
690 continue;
691 }
692
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100693 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
Christopher Fauletafd819c2018-12-11 08:57:45 +0100694 if (type == HTX_BLK_EOH)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100695 break;
696 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
697 }
698 }
699 else {
700 struct hdr_ctx ctx;
701
702 /* Do not cache too big objects. */
703 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
704 msg->sov + msg->body_len > shctx->max_obj_size)
705 goto out;
706
707 /* Does not manage Vary at the moment. We will need a secondary key later for that */
708 ctx.idx = 0;
709 if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx))
710 goto out;
711
712 check_response_for_cacheability(s, &s->res);
713
714 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
715 goto out;
716
717 age = 0;
718 ctx.idx = 0;
719 if (http_find_header2("Age", 3, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
720 if (!strl2llrc(ctx.line + ctx.val, ctx.vlen, &hdr_age) && hdr_age > 0) {
721 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
722 hdr_age = CACHE_ENTRY_MAX_AGE;
723 age = hdr_age;
724 }
725 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200726 }
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200727 }
728
William Lallemand4da3f8a2017-10-31 14:33:34 +0100729 shctx_lock(shctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100730 if (IS_HTX_STRM(s))
731 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
732 else
733 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + msg->sov);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100734 if (!first) {
735 shctx_unlock(shctx);
736 goto out;
737 }
738 shctx_unlock(shctx);
739
Willy Tarreau1093a452018-04-06 19:02:25 +0200740 /* the received memory is not initialized, we need at least to mark
741 * the object as not indexed yet.
742 */
743 object = (struct cache_entry *)first->data;
744 object->eb.node.leaf_p = NULL;
745 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200746 object->age = age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100747 if (IS_HTX_STRM(s)) {
748 object->hdrs_len = trash.data;
749 object->data_len = 0;
750 }
751 else
752 object->eoh = msg->eoh;
Willy Tarreau1093a452018-04-06 19:02:25 +0200753
William Lallemand4da3f8a2017-10-31 14:33:34 +0100754 /* reserve space for the cache_entry structure */
755 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200756 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100757 /* cache the headers in a http action because it allows to chose what
758 * to cache, for example you might want to cache a response before
759 * modifying some HTTP headers, or on the contrary after modifying
760 * those headers.
761 */
762
763 /* does not need to be locked because it's in the "hot" list,
764 * copy the headers */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100765 if (IS_HTX_STRM(s)) {
766 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
767 goto out;
768 }
769 else {
770 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)ci_head(&s->res), msg->sov) < 0)
771 goto out;
772 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100773
774 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet95220e22018-12-07 17:34:39 +0100775 list_for_each_entry(filter, &s->strm_flt.filters, list) {
776 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
777 if (filter->ctx) {
778 struct cache_st *cache_ctx = filter->ctx;
779 struct cache_entry *old;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100780
Christopher Faulet95220e22018-12-07 17:34:39 +0100781 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100782
Christopher Faulet95220e22018-12-07 17:34:39 +0100783 object->eb.key = (*(unsigned int *)&txn->cache_hash);
784 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
785 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100786
Christopher Faulet95220e22018-12-07 17:34:39 +0100787 shctx_lock(shctx);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100788
Christopher Faulet95220e22018-12-07 17:34:39 +0100789 old = entry_exist(cconf->c.cache, txn->cache_hash);
790 if (old) {
791 eb32_delete(&old->eb);
792 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100793 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100794 shctx_unlock(shctx);
795
796 /* store latest value and expiration time */
797 object->latest_validation = now.tv_sec;
798 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100799 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100800 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100801 }
802 }
803
804out:
805 /* if does not cache */
806 if (first) {
807 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100808 first->len = 0;
809 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100810 shctx_row_dec_hot(shctx, first);
811 shctx_unlock(shctx);
812 }
813
William Lallemand41db4602017-10-30 11:15:51 +0100814 return ACT_RET_CONT;
815}
816
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200817#define HTTP_CACHE_INIT 0 /* Initial state. */
818#define HTTP_CACHE_HEADER 1 /* Cache entry headers forwarded. */
819#define HTTP_CACHE_FWD 2 /* Cache entry completely forwarded. */
820#define HTTP_CACHE_END 3 /* Cache entry treatment terminated. */
William Lallemand77c11972017-10-31 20:43:01 +0100821
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100822#define HTX_CACHE_INIT 0 /* Initial state. */
823#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
824#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
825#define HTX_CACHE_EOD 3 /* Cache entry data forwarded. DATA->TLR transition */
826#define HTX_CACHE_TLR 4 /* Cache entry trailers forwarding */
827#define HTX_CACHE_EOM 5 /* Cache entry completely forwarded. Finish the HTX message */
828#define HTX_CACHE_END 6 /* Cache entry treatment terminated */
829
William Lallemandecb73b12017-11-24 14:33:55 +0100830static void http_cache_applet_release(struct appctx *appctx)
831{
Christopher Faulet95220e22018-12-07 17:34:39 +0100832 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100833 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100834 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100835 struct shared_block *first = block_ptr(cache_ptr);
836
837 shctx_lock(shctx_ptr(cache));
838 shctx_row_dec_hot(shctx_ptr(cache), first);
839 shctx_unlock(shctx_ptr(cache));
840}
841
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100842static size_t htx_cache_dump_headers(struct appctx *appctx, struct htx *htx)
843{
Christopher Faulet95220e22018-12-07 17:34:39 +0100844 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
845 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100846 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
847 struct shared_block *shblk = appctx->ctx.cache.next;
848 struct buffer *tmp = get_trash_chunk();
849 char *end;
850 unsigned int offset, len, age;
851
852 offset = appctx->ctx.cache.offset;
853 len = cache_ptr->hdrs_len;
854
855 /* 1. Retrieve all headers from the cache */
856 list_for_each_entry_from(shblk, &shctx->hot, list) {
857 int sz;
858
859 sz = MIN(len, shctx->block_size - offset);
860 if (!chunk_memcat(tmp, (const char *)shblk->data + offset, sz))
861 return 0;
862
863 offset += sz;
864 len -= sz;
865 if (!len)
866 break;
867 offset = 0;
868 }
869 appctx->ctx.cache.offset = offset;
870 appctx->ctx.cache.next = shblk;
871 appctx->ctx.cache.sent += b_data(tmp);
872
873 /* 2. push these headers in the HTX message */
874 offset = 0;
875 while (offset < b_data(tmp)) {
876 struct htx_blk *blk;
877 enum htx_blk_type type;
878 uint32_t info, sz;
879
880 /* Read the header's info */
881 memcpy((char *)&info, b_peek(tmp, offset), 4);
882 type = (info >> 28);
883 sz = ((type == HTX_BLK_HDR)
884 ? (info & 0xff) + ((info >> 8) & 0xfffff)
885 : info & 0xfffffff);
886
887 /* Create the block with the right type and the right size */
888 blk = htx_add_blk(htx, type, sz);
889 if (!blk)
890 return 0;
891
892 /* Copy info and data */
893 blk->info = info;
894 memcpy(htx_get_blk_ptr(htx, blk), b_peek(tmp, offset+4), sz);
895
896 /* next header */
897 offset += 4 + sz;
898 }
899
900 /* 3. Append "age" header */
901 chunk_reset(tmp);
902 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
903 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
904 age = CACHE_ENTRY_MAX_AGE;
905 end = ultoa_o(age, b_head(tmp), b_size(tmp));
906 b_set_data(tmp, end - b_head(tmp));
907
908 if (!http_add_header(htx, ist("Age"), ist2(b_head(tmp), b_data(tmp))))
909 return 0;
910
911 return htx->data;
912}
913
914static size_t htx_cache_dump_data(struct appctx *appctx, struct htx *htx,
915 enum htx_blk_type type, unsigned int len)
916{
Christopher Faulet95220e22018-12-07 17:34:39 +0100917 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
918 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100919 struct shared_block *shblk = appctx->ctx.cache.next;
920 uint32_t max = htx_free_data_space(htx);
921 unsigned int offset;
922 size_t total = 0;
923
924 offset = appctx->ctx.cache.offset;
925 if (len > max)
926 len = max;
927 if (!len)
928 goto end;
929
930 list_for_each_entry_from(shblk, &shctx->hot, list) {
931 struct ist data;
932 int sz;
933
934 sz = MIN(len, shctx->block_size - offset);
935 data = ist2((const char *)shblk->data + offset, sz);
936 if (type == HTX_BLK_DATA) {
937 if (!htx_add_data(htx, data))
938 break;
939 }
940 else { /* HTX_BLK_TLR */
941 if (!htx_add_trailer(htx, data))
942 break;
943 }
944
945 offset += sz;
946 len -= sz;
947 total += sz;
948 if (!len)
949 break;
950 offset = 0;
951 }
952 appctx->ctx.cache.offset = offset;
953 appctx->ctx.cache.next = shblk;
954 appctx->ctx.cache.sent += total;
955
956 end:
957 return total;
958}
959static void htx_cache_io_handler(struct appctx *appctx)
960{
961 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
962 struct shared_block *first = block_ptr(cache_ptr);
963 struct stream_interface *si = appctx->owner;
964 struct channel *req = si_oc(si);
965 struct channel *res = si_ic(si);
966 struct htx *req_htx, *res_htx;
967 struct buffer *errmsg;
968 size_t ret, total = 0;
969
970 res_htx = htxbuf(&res->buf);
971
972 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
973 goto out;
974
975 /* Check if the input buffer is avalaible. */
976 if (!b_size(&res->buf)) {
977 si_rx_room_blk(si);
978 goto out;
979 }
980
981 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
982 appctx->st0 = HTTP_CACHE_END;
983
984 if (appctx->st0 == HTX_CACHE_INIT) {
985 appctx->ctx.cache.next = block_ptr(cache_ptr);
986 appctx->ctx.cache.offset = sizeof(*cache_ptr);
987 appctx->ctx.cache.sent = 0;
988 appctx->st0 = HTX_CACHE_HEADER;
989 }
990
991 if (appctx->st0 == HTX_CACHE_HEADER) {
992 /* Headers must be dump at once. Otherwise it is an error */
993 ret = htx_cache_dump_headers(appctx, res_htx);
994 if (!ret)
995 goto error;
996
997 total += ret;
998 if (cache_ptr->data_len)
999 appctx->st0 = HTX_CACHE_DATA;
1000 else if (first->len > sizeof(*cache_ptr) + appctx->ctx.cache.sent) {
1001 /* Headers have benn sent (hrds_len) and there is no data
1002 * (data_len == 0). So, all the remaining is the
1003 * trailers */
1004 appctx->st0 = HTX_CACHE_EOD;
1005 }
1006 else
1007 appctx->st0 = HTX_CACHE_EOM;
1008 }
1009
1010 if (appctx->st0 == HTX_CACHE_DATA) {
1011 unsigned int len = cache_ptr->hdrs_len + cache_ptr->data_len - appctx->ctx.cache.sent;
1012
1013 ret = htx_cache_dump_data(appctx, res_htx, HTX_BLK_DATA, len);
1014 if (!ret) {
1015 si_rx_room_blk(si);
1016 goto out;
1017 }
1018
1019 total += ret;
1020 if (cache_ptr->hdrs_len + cache_ptr->data_len == appctx->ctx.cache.sent) {
1021 if (first->len > sizeof(*cache_ptr) + appctx->ctx.cache.sent) {
1022 /* Headers and all data have been sent
1023 * (hrds_len + data_len == sent). So, all the remaining
1024 * is the trailers */
1025 appctx->st0 = HTX_CACHE_EOD;
1026 }
1027 else
1028 appctx->st0 = HTX_CACHE_EOM;
1029 }
1030 }
1031
1032 if (appctx->st0 == HTX_CACHE_EOD) {
1033 if (!htx_add_endof(res_htx, HTX_BLK_EOD)) {
1034 si_rx_room_blk(si);
1035 goto out;
1036 }
1037
1038 total++;
1039 appctx->st0 = HTX_CACHE_TLR;
1040 }
1041
1042 if (appctx->st0 == HTX_CACHE_TLR) {
1043 unsigned int len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1044
1045 ret = htx_cache_dump_data(appctx, res_htx, HTX_BLK_TLR, len);
1046 if (!ret) {
1047 si_rx_room_blk(si);
1048 goto out;
1049 }
1050
1051 total += ret;
1052 if (first->len == sizeof(*cache_ptr) + appctx->ctx.cache.sent)
1053 appctx->st0 = HTX_CACHE_EOM;
1054 }
1055
1056 if (appctx->st0 == HTX_CACHE_EOM) {
1057 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1058 si_rx_room_blk(si);
1059 goto out;
1060 }
1061
1062 total++;
1063 appctx->st0 = HTX_CACHE_END;
1064 }
1065
1066 end:
1067 if (appctx->st0 == HTX_CACHE_END) {
1068 /* eat the whole request */
1069 req_htx = htxbuf(&req->buf);
1070 htx_reset(req_htx);
1071 htx_to_buf(req_htx, &req->buf);
1072 co_set_data(req, 0);
1073 res->flags |= CF_READ_NULL;
1074 si_shutr(si);
1075 }
1076
1077 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
1078 si_shutw(si);
1079
1080 if (appctx->st0 == HTX_CACHE_END) {
1081 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
1082 si_shutr(si);
1083 res->flags |= CF_READ_NULL;
1084 }
1085 }
1086 out:
1087 if (total) {
1088 res->total += total;
1089 res->flags |= CF_READ_PARTIAL;
1090 }
1091
1092 /* we have left the request in the buffer for the case where we
1093 * process a POST, and this automatically re-enables activity on
1094 * read. It's better to indicate that we want to stop reading when
1095 * we're sending, so that we know there's at most one direction
1096 * deciding to wake the applet up. It saves it from looping when
1097 * emitting large blocks into small TCP windows.
1098 */
1099 htx_to_buf(res_htx, &res->buf);
1100 if (!channel_is_empty(res))
1101 si_stop_get(si);
1102 return;
1103
1104 error:
1105 /* Sent and HTTP error 500 */
1106 b_reset(&res->buf);
1107 errmsg = &htx_err_chunks[HTTP_ERR_500];
1108 res->buf.data = b_data(errmsg);
1109 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1110 res_htx = htx_from_buf(&res->buf);
1111
1112 total = res_htx->data;
1113 appctx->st0 = HTX_CACHE_END;
1114 goto end;
1115}
1116
1117
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001118/*
1119 * Append an "Age" header into <chn> channel for this <ce> cache entry.
Joseph Herlant8dae5b32018-11-15 14:07:53 -08001120 * This is the responsibility of the caller to insure there is enough
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001121 * data in the channel.
1122 *
1123 * Returns the number of bytes inserted if succeeded, 0 if failed.
1124 */
1125static int cache_channel_append_age_header(struct cache_entry *ce, struct channel *chn)
1126{
1127 unsigned int age;
1128
1129 age = MAX(0, (int)(now.tv_sec - ce->latest_validation)) + ce->age;
1130 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1131 age = CACHE_ENTRY_MAX_AGE;
1132
1133 chunk_reset(&trash);
1134 chunk_printf(&trash, "Age: %u", age);
1135
1136 return ci_insert_line2(chn, ce->eoh, trash.area, trash.data);
1137}
1138
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001139static int cache_channel_row_data_get(struct appctx *appctx, int len)
William Lallemand77c11972017-10-31 20:43:01 +01001140{
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001141 int ret, total;
William Lallemand77c11972017-10-31 20:43:01 +01001142 struct stream_interface *si = appctx->owner;
1143 struct channel *res = si_ic(si);
Christopher Faulet95220e22018-12-07 17:34:39 +01001144 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1145 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001146 struct shared_context *shctx = shctx_ptr(cache);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001147 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1148 struct shared_block *blk, *next = appctx->ctx.cache.next;
1149 int offset;
1150
1151 total = 0;
1152 offset = 0;
1153
1154 if (!next) {
1155 offset = sizeof(struct cache_entry);
1156 next = block_ptr(cache_ptr);
1157 }
1158
1159 blk = next;
1160 list_for_each_entry_from(blk, &shctx->hot, list) {
1161 int sz;
1162
1163 if (len <= 0)
1164 break;
1165
1166 sz = MIN(len, shctx->block_size - offset);
1167
1168 ret = ci_putblk(res, (const char *)blk->data + offset, sz);
1169 if (unlikely(offset))
1170 offset = 0;
1171 if (ret <= 0) {
1172 if (ret == -3 || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001173 si_rx_room_blk(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001174 break;
1175 }
1176 return -1;
1177 }
1178
1179 total += sz;
1180 len -= sz;
1181 }
1182 appctx->ctx.cache.next = blk;
1183
1184 return total;
1185}
1186
1187static void http_cache_io_handler(struct appctx *appctx)
1188{
1189 struct stream_interface *si = appctx->owner;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001190 struct stream *s = si_strm(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001191 struct channel *res = si_ic(si);
1192 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
William Lallemand77c11972017-10-31 20:43:01 +01001193 struct shared_block *first = block_ptr(cache_ptr);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001194 unsigned int *sent = &appctx->ctx.cache.sent;
1195
1196 if (IS_HTX_STRM(s))
1197 return htx_cache_io_handler(appctx);
William Lallemand77c11972017-10-31 20:43:01 +01001198
1199 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1200 goto out;
1201
Joseph Herlant8dae5b32018-11-15 14:07:53 -08001202 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001203 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01001204 /* buf.size==0 means we failed to get a buffer and were
1205 * already subscribed to a wait list to get a buffer.
1206 */
William Lallemand77c11972017-10-31 20:43:01 +01001207 goto out;
1208 }
1209
1210 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
1211 appctx->st0 = HTTP_CACHE_END;
1212
1213 /* buffer are aligned there, should be fine */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001214 if (appctx->st0 == HTTP_CACHE_HEADER || appctx->st0 == HTTP_CACHE_INIT) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001215 int len = first->len - *sent - sizeof(struct cache_entry);
William Lallemand55e76742017-11-21 20:01:28 +01001216
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001217 if (len > 0) {
1218 int ret;
1219
1220 ret = cache_channel_row_data_get(appctx, len);
1221 if (ret == -1)
1222 appctx->st0 = HTTP_CACHE_END;
1223 else
1224 *sent += ret;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001225 if (appctx->st0 == HTTP_CACHE_INIT && *sent > cache_ptr->eoh &&
1226 cache_channel_append_age_header(cache_ptr, res))
1227 appctx->st0 = HTTP_CACHE_HEADER;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001228 }
1229 else {
1230 *sent = 0;
1231 appctx->st0 = HTTP_CACHE_FWD;
William Lallemand77c11972017-10-31 20:43:01 +01001232 }
William Lallemand77c11972017-10-31 20:43:01 +01001233 }
1234
1235 if (appctx->st0 == HTTP_CACHE_FWD) {
1236 /* eat the whole request */
Willy Tarreau178b9872018-06-19 07:13:36 +02001237 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 +01001238 res->flags |= CF_READ_NULL;
1239 si_shutr(si);
1240 }
1241
1242 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
1243 si_shutw(si);
1244out:
1245 ;
1246}
1247
Christopher Faulet95220e22018-12-07 17:34:39 +01001248static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001249{
1250 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001251 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001252
Christopher Faulet95220e22018-12-07 17:34:39 +01001253 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001254 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001255 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001256 }
1257
1258 /* check if a cache filter was already registered with this cache
1259 * name, if that's the case, must use it. */
1260 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001261 if (fconf->id == cache_store_flt_id) {
1262 cconf = fconf->conf;
1263 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1264 rule->arg.act.p[0] = cconf;
1265 return 1;
1266 }
William Lallemand41db4602017-10-30 11:15:51 +01001267 }
1268 }
1269
Christopher Faulet95220e22018-12-07 17:34:39 +01001270 /* Create the filter cache config */
1271 cconf = calloc(1, sizeof(*cconf));
1272 if (!cconf) {
1273 memprintf(err, "out of memory\n");
1274 goto err;
1275 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001276 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001277 cconf->c.name = strdup(name);
1278 if (!cconf->c.name) {
1279 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001280 goto err;
1281 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001282
William Lallemand41db4602017-10-30 11:15:51 +01001283 /* register a filter to fill the cache buffer */
1284 fconf = calloc(1, sizeof(*fconf));
1285 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001286 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001287 goto err;
1288 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001289 fconf->id = cache_store_flt_id;
1290 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001291 fconf->ops = &cache_ops;
1292 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1293
Christopher Faulet95220e22018-12-07 17:34:39 +01001294 rule->arg.act.p[0] = cconf;
1295 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001296
Christopher Faulet95220e22018-12-07 17:34:39 +01001297 err:
1298 free(cconf);
1299 return 0;
1300}
1301
1302enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1303 struct act_rule *rule, char **err)
1304{
1305 rule->action = ACT_CUSTOM;
1306 rule->action_ptr = http_action_store_cache;
1307
1308 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1309 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001310
Christopher Faulet95220e22018-12-07 17:34:39 +01001311 (*orig_arg)++;
1312 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001313}
1314
William Lallemandf528fff2017-11-23 19:43:17 +01001315/* This produces a sha1 hash of the concatenation of the first
1316 * occurrence of the Host header followed by the path component if it
1317 * begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001318int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001319{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001320 struct http_txn *txn = s->txn;
William Lallemandf528fff2017-11-23 19:43:17 +01001321 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001322 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001323
William Lallemandf528fff2017-11-23 19:43:17 +01001324 trash = get_trash_chunk();
1325
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001326 if (IS_HTX_STRM(s)) {
1327 struct htx *htx = htxbuf(&s->req.buf);
1328 struct htx_sl *sl;
1329 struct http_hdr_ctx ctx;
1330 struct ist path;
William Lallemandf528fff2017-11-23 19:43:17 +01001331
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001332 ctx.blk = NULL;
1333 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1334 return 0;
1335 chunk_memcat(trash, ctx.value.ptr, ctx.value.len);
1336
1337 sl = http_find_stline(htx);
1338 path = http_get_path(htx_sl_req_uri(sl));
1339 if (!path.ptr)
1340 return 0;
1341 chunk_memcat(trash, path.ptr, path.len);
1342 }
1343 else {
1344 struct hdr_ctx ctx;
1345 char *path;
1346 char *end;
1347
1348 /* retrive the host */
1349 ctx.idx = 0;
1350 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx))
1351 return 0;
1352 chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen);
1353
1354 /* now retrieve the path */
1355 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1356 path = http_txn_get_path(txn);
1357 if (!path)
1358 return 0;
1359 chunk_strncat(trash, path, end - path);
1360 }
William Lallemandf528fff2017-11-23 19:43:17 +01001361
1362 /* hash everything */
1363 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001364 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001365 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1366
1367 return 1;
1368}
1369
William Lallemand41db4602017-10-30 11:15:51 +01001370enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1371 struct session *sess, struct stream *s, int flags)
1372{
William Lallemand77c11972017-10-31 20:43:01 +01001373
William Lallemand77c11972017-10-31 20:43:01 +01001374 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001375 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1376 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001377
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001378 if (IS_HTX_STRM(s))
1379 htx_check_request_for_cacheability(s, &s->req);
1380 else
1381 check_request_for_cacheability(s, &s->req);
1382
Willy Tarreau504455c2017-12-22 17:47:35 +01001383 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1384 return ACT_RET_CONT;
1385
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001386 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001387 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001388
Willy Tarreau504455c2017-12-22 17:47:35 +01001389 if (s->txn->flags & TX_CACHE_IGNORE)
1390 return ACT_RET_CONT;
1391
William Lallemanda400a3a2017-11-20 19:13:12 +01001392 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001393 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001394 if (res) {
1395 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001396 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1397 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001398 s->target = &http_cache_applet.obj_type;
1399 if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
1400 appctx->st0 = HTTP_CACHE_INIT;
1401 appctx->rule = rule;
1402 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001403 appctx->ctx.cache.next = NULL;
1404 appctx->ctx.cache.sent = 0;
Olivier Houchardfccf8402017-11-01 14:04:02 +01001405 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001406 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001407 shctx_lock(shctx_ptr(cache));
1408 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1409 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001410 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001411 }
1412 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001413 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001414 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001415}
1416
1417
1418enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1419 struct act_rule *rule, char **err)
1420{
William Lallemand41db4602017-10-30 11:15:51 +01001421 rule->action = ACT_CUSTOM;
1422 rule->action_ptr = http_action_req_cache_use;
1423
Christopher Faulet95220e22018-12-07 17:34:39 +01001424 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001425 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001426
1427 (*orig_arg)++;
1428 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001429}
1430
1431int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1432{
1433 int err_code = 0;
1434
1435 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1436
1437 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001438 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1439 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001440 err_code |= ERR_ALERT | ERR_ABORT;
1441 goto out;
1442 }
1443
1444 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1445 err_code |= ERR_ABORT;
1446 goto out;
1447 }
1448
1449 if (tmp_cache_config == NULL) {
1450 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1451 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001452 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001453 err_code |= ERR_ALERT | ERR_ABORT;
1454 goto out;
1455 }
1456
1457 strlcpy2(tmp_cache_config->id, args[1], 33);
1458 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001459 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1460 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001461 err_code |= ERR_WARN;
1462 }
William Lallemand49b44532017-11-24 18:53:43 +01001463 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001464 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001465 tmp_cache_config->maxobjsz = 0;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001466 tmp_cache_config->flags = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001467 }
1468 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001469 unsigned long int maxsize;
1470 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001471
1472 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1473 err_code |= ERR_ABORT;
1474 goto out;
1475 }
1476
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001477 maxsize = strtoul(args[1], &err, 10);
1478 if (err == args[1] || *err != '\0') {
1479 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1480 file, linenum, args[1]);
1481 err_code |= ERR_ABORT;
1482 goto out;
1483 }
1484
1485 if (maxsize > (UINT_MAX >> 20)) {
1486 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1487 file, linenum, args[1], UINT_MAX >> 20);
1488 err_code |= ERR_ABORT;
1489 goto out;
1490 }
1491
William Lallemand41db4602017-10-30 11:15:51 +01001492 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001493 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001494 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001495 } else if (strcmp(args[0], "max-age") == 0) {
1496 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1497 err_code |= ERR_ABORT;
1498 goto out;
1499 }
1500
1501 if (!*args[1]) {
1502 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1503 file, linenum, args[0]);
1504 err_code |= ERR_WARN;
1505 }
1506
1507 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001508 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001509 unsigned int maxobjsz;
1510 char *err;
1511
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001512 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1513 err_code |= ERR_ABORT;
1514 goto out;
1515 }
1516
1517 if (!*args[1]) {
1518 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1519 file, linenum, args[0]);
1520 err_code |= ERR_WARN;
1521 }
1522
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001523 maxobjsz = strtoul(args[1], &err, 10);
1524 if (err == args[1] || *err != '\0') {
1525 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1526 file, linenum, args[1]);
1527 err_code |= ERR_ABORT;
1528 goto out;
1529 }
1530 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001531 }
1532 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001533 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001534 err_code |= ERR_ALERT | ERR_FATAL;
1535 goto out;
1536 }
1537out:
1538 return err_code;
1539}
1540
1541/* once the cache section is parsed */
1542
1543int cfg_post_parse_section_cache()
1544{
1545 struct shared_context *shctx;
1546 int err_code = 0;
1547 int ret_shctx;
1548
1549 if (tmp_cache_config) {
1550 struct cache *cache;
1551
1552 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001553 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001554 err_code |= ERR_FATAL | ERR_ALERT;
1555 goto out;
1556 }
1557
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001558 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001559 /* Default max. file size is a 256th of the cache size. */
1560 tmp_cache_config->maxobjsz =
1561 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001562 }
1563 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1564 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1565 err_code |= ERR_FATAL | ERR_ALERT;
1566 goto out;
1567 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001568
1569 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE,
1570 tmp_cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001571
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001572 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001573 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001574 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001575 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001576 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001577
1578 err_code |= ERR_FATAL | ERR_ALERT;
1579 goto out;
1580 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001581 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +01001582 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
1583 cache = (struct cache *)shctx->data;
1584 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001585 LIST_ADDQ(&caches, &cache->list);
1586 }
1587out:
1588 free(tmp_cache_config);
1589 tmp_cache_config = NULL;
1590 return err_code;
1591
1592}
1593
1594/*
1595 * Resolve the cache name to a pointer once the file is completely read.
1596 */
1597int cfg_cache_postparser()
1598{
William Lallemand41db4602017-10-30 11:15:51 +01001599 struct cache *cache;
William Lallemand41db4602017-10-30 11:15:51 +01001600 int err = 0;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001601
1602 /* Check if the cache is used by HTX and legacy HTTP proxies in same
1603 * time
1604 */
1605 list_for_each_entry(cache, &caches, list) {
1606 if ((cache->flags & (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) == (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) {
1607 ha_alert("Cache '%s': cannot be used by HTX and legacy HTTP proxies in same time.\n",
1608 cache->id);
1609 err++;
1610 }
1611 }
1612
William Lallemand41db4602017-10-30 11:15:51 +01001613 return err;
1614}
1615
1616
1617struct flt_ops cache_ops = {
1618 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001619 .check = cache_store_check,
1620 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001621
William Lallemand4da3f8a2017-10-31 14:33:34 +01001622 /* Handle channels activity */
1623 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001624 .channel_end_analyze = cache_store_chn_end_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001625
1626 /* Filter HTTP requests and responses */
1627 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001628 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001629 .http_end = cache_store_http_end,
1630
1631 .http_forward_data = cache_store_http_forward_data,
1632
William Lallemand41db4602017-10-30 11:15:51 +01001633};
1634
Christopher Faulet99a17a22018-12-11 09:18:27 +01001635
1636
1637static int
1638parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1639 struct flt_conf *fconf, char **err, void *private)
1640{
1641 struct flt_conf *f, *back;
1642 struct cache_flt_conf *cconf;
1643 char *name = NULL;
1644 int pos = *cur_arg;
1645
1646 /* Get the cache filter name*/
1647 if (!strcmp(args[pos], "cache")) {
1648 if (!*args[pos + 1]) {
1649 memprintf(err, "%s : expects an <id> argument", args[pos]);
1650 goto error;
1651 }
1652 name = strdup(args[pos + 1]);
1653 if (!name) {
1654 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1655 goto error;
1656 }
1657 pos += 2;
1658 }
1659
1660 /* Check if an implicit filter with the same name already exists. If so,
1661 * we remove the implicit filter to use the explicit one. */
1662 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1663 if (f->id != cache_store_flt_id)
1664 continue;
1665
1666 cconf = f->conf;
1667 if (strcmp(name, cconf->c.name)) {
1668 cconf = NULL;
1669 continue;
1670 }
1671
1672 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1673 cconf = NULL;
1674 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1675 px->id, name);
1676 return -1;
1677 }
1678
1679 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1680 LIST_DEL(&f->list);
1681 free(f);
1682 free(name);
1683 break;
1684 }
1685
1686 /* No implicit cache filter found, create configuration for the explicit one */
1687 if (!cconf) {
1688 cconf = calloc(1, sizeof(*cconf));
1689 if (!cconf) {
1690 memprintf(err, "%s: out of memory", args[*cur_arg]);
1691 goto error;
1692 }
1693 cconf->c.name = name;
1694 }
1695
1696 cconf->flags = 0;
1697 fconf->id = cache_store_flt_id;
1698 fconf->conf = cconf;
1699 fconf->ops = &cache_ops;
1700
1701 *cur_arg = pos;
1702 return 0;
1703
1704 error:
1705 free(name);
1706 free(cconf);
1707 return -1;
1708}
1709
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001710static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001711{
1712 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1713 return 1;
1714
1715 return 0;
1716}
1717
1718static int cli_io_handler_show_cache(struct appctx *appctx)
1719{
1720 struct cache* cache = appctx->ctx.cli.p0;
1721 struct stream_interface *si = appctx->owner;
1722
William Lallemand1f49a362017-11-21 20:01:26 +01001723 if (cache == NULL) {
1724 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1725 }
1726
1727 list_for_each_entry_from(cache, &caches, list) {
1728 struct eb32_node *node = NULL;
1729 unsigned int next_key;
1730 struct cache_entry *entry;
1731
William Lallemand1f49a362017-11-21 20:01:26 +01001732 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001733 if (!next_key) {
1734 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1735 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001736 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001737 return 0;
1738 }
1739 }
William Lallemand1f49a362017-11-21 20:01:26 +01001740
1741 appctx->ctx.cli.p0 = cache;
1742
1743 while (1) {
1744
1745 shctx_lock(shctx_ptr(cache));
1746 node = eb32_lookup_ge(&cache->entries, next_key);
1747 if (!node) {
1748 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001749 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001750 break;
1751 }
1752
1753 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001754 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 +01001755
1756 next_key = node->key + 1;
1757 appctx->ctx.cli.i0 = next_key;
1758
1759 shctx_unlock(shctx_ptr(cache));
1760
1761 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001762 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001763 return 0;
1764 }
1765 }
1766
1767 }
1768
1769 return 1;
1770
1771}
1772
Christopher Faulet99a17a22018-12-11 09:18:27 +01001773/* Declare the filter parser for "cache" keyword */
1774static struct flt_kw_list filter_kws = { "CACHE", { }, {
1775 { "cache", parse_cache_flt, NULL },
1776 { NULL, NULL, NULL },
1777 }
1778};
1779
1780INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1781
William Lallemand1f49a362017-11-21 20:01:26 +01001782static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001783 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1784 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001785}};
1786
Willy Tarreau0108d902018-11-25 19:14:37 +01001787INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001788
William Lallemand41db4602017-10-30 11:15:51 +01001789static struct action_kw_list http_res_actions = {
1790 .kw = {
1791 { "cache-store", parse_cache_store },
1792 { NULL, NULL }
1793 }
1794};
1795
Willy Tarreau0108d902018-11-25 19:14:37 +01001796INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1797
William Lallemand41db4602017-10-30 11:15:51 +01001798static struct action_kw_list http_req_actions = {
1799 .kw = {
1800 { "cache-use", parse_cache_use },
1801 { NULL, NULL }
1802 }
1803};
1804
Willy Tarreau0108d902018-11-25 19:14:37 +01001805INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1806
William Lallemand41db4602017-10-30 11:15:51 +01001807struct applet http_cache_applet = {
1808 .obj_type = OBJ_TYPE_APPLET,
1809 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001810 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001811 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001812};
1813
Willy Tarreaue6552512018-11-26 11:33:13 +01001814/* config parsers for this section */
1815REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
1816REGISTER_CONFIG_POSTPARSER("cache", cfg_cache_postparser);