blob: 020650a336307b2a13b7f42d109ab8d94a63c7c9 [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>
William Lallemand41db4602017-10-30 11:15:51 +010027#include <proto/filters.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020028#include <proto/http_rules.h>
William Lallemand41db4602017-10-30 11:15:51 +010029#include <proto/proto_http.h>
30#include <proto/log.h>
31#include <proto/stream.h>
32#include <proto/stream_interface.h>
33#include <proto/shctx.h>
34
William Lallemand41db4602017-10-30 11:15:51 +010035
36#include <common/cfgparse.h>
37#include <common/hash.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010038#include <common/htx.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 Faulet27d93c32018-12-15 22:32:02 +010046#define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without
Christopher Faulet99a17a22018-12-11 09:18:27 +010047 * the filter keyword) */
Christopher Fauletafd819c2018-12-11 08:57:45 +010048
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 Faulet27d93c32018-12-15 22:32:02 +0100162 int comp = 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
Christopher Faulet27d93c32018-12-15 22:32:02 +0100187 * enabled and if it is after the cache. When the compression is before
188 * the cache, an error is returned. Also check if the cache filter must
189 * be explicitly declaired or not. */
Christopher Fauletafd819c2018-12-11 08:57:45 +0100190 list_for_each_entry(f, &px->filter_configs, list) {
191 if (f == fconf) {
Christopher Faulet27d93c32018-12-15 22:32:02 +0100192 /* The compression filter must be evaluated after the cache. */
193 if (comp) {
194 ha_alert("config: %s '%s': unable to enable the compression filter before "
195 "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
196 return 1;
197 }
Christopher Faulet99a17a22018-12-11 09:18:27 +0100198 }
Christopher Faulet27d93c32018-12-15 22:32:02 +0100199 else if (f->id == http_comp_flt_id) {
Christopher Fauletafd819c2018-12-11 08:57:45 +0100200 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 }
Christopher Faulet27d93c32018-12-15 22:32:02 +0100206 comp = 1;
Christopher Fauletafd819c2018-12-11 08:57:45 +0100207 }
Christopher Faulet27d93c32018-12-15 22:32:02 +0100208 else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
209 /* Implicit declaration is only allowed with the
210 * compression. For other filters, an implicit
211 * declaration is required. */
212 ha_alert("config: %s '%s': require an explicit filter declaration "
213 "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id);
214 return 1;
215 }
216
Christopher Fauletafd819c2018-12-11 08:57:45 +0100217 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100218 return 0;
219}
220
221static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100222cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
223{
224 if (!(chn->flags & CF_ISRESP))
225 return 1;
226
227 if (filter->ctx == NULL) {
228 struct cache_st *st;
229
Willy Tarreaubafbe012017-11-24 17:34:44 +0100230 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100231 if (st == NULL)
232 return -1;
233
234 st->hdrs_len = 0;
235 st->first_block = NULL;
236 filter->ctx = st;
237 }
238
William Lallemand4da3f8a2017-10-31 14:33:34 +0100239 return 1;
240}
241
242static int
William Lallemand49dc0482017-11-24 14:33:54 +0100243cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
244{
245 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100246 struct cache_flt_conf *cconf = FLT_CONF(filter);
247 struct cache *cache = cconf->c.cache;
William Lallemand49dc0482017-11-24 14:33:54 +0100248 struct shared_context *shctx = shctx_ptr(cache);
249
250 if (!(chn->flags & CF_ISRESP))
251 return 1;
252
253 /* Everything should be released in the http_end filter, but we need to do it
254 * there too, in case of errors */
255
256 if (st && st->first_block) {
257
258 shctx_lock(shctx);
259 shctx_row_dec_hot(shctx, st->first_block);
260 shctx_unlock(shctx);
261
262 }
263 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100264 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100265 filter->ctx = NULL;
266 }
267
268 return 1;
269}
270
271
272static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100273cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
274{
275 struct cache_st *st = filter->ctx;
276
William Lallemand4da3f8a2017-10-31 14:33:34 +0100277 if (!(msg->chn->flags & CF_ISRESP) || !st)
278 return 1;
279
Christopher Faulet67658c92018-12-06 21:59:39 +0100280 if (st->first_block) {
281 register_data_filter(s, msg->chn, filter);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100282 if (!IS_HTX_STRM(s))
283 st->hdrs_len = msg->sov;
Christopher Faulet67658c92018-12-06 21:59:39 +0100284 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100285 return 1;
286}
287
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200288static inline void disable_cache_entry(struct cache_st *st,
289 struct filter *filter, struct shared_context *shctx)
290{
291 struct cache_entry *object;
292
293 object = (struct cache_entry *)st->first_block->data;
294 filter->ctx = NULL; /* disable cache */
295 shctx_lock(shctx);
296 shctx_row_dec_hot(shctx, st->first_block);
297 object->eb.key = 0;
298 shctx_unlock(shctx);
299 pool_free(pool_head_cache_st, st);
300}
301
William Lallemand4da3f8a2017-10-31 14:33:34 +0100302static int
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100303cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
304 unsigned int offset, unsigned int len)
305{
Christopher Faulet95220e22018-12-07 17:34:39 +0100306 struct cache_flt_conf *cconf = FLT_CONF(filter);
307 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100308 struct cache_st *st = filter->ctx;
309 struct htx *htx = htxbuf(&msg->chn->buf);
310 struct htx_blk *blk;
311 struct htx_ret htx_ret;
312 struct cache_entry *object;
313 int ret, to_forward = 0;
314
315 if (!len)
316 return len;
317
318 if (!st->first_block) {
319 unregister_data_filter(s, msg->chn, filter);
320 return len;
321 }
322 object = (struct cache_entry *)st->first_block->data;
323
324 htx_ret = htx_find_blk(htx, offset);
325 blk = htx_ret.blk;
326 offset = htx_ret.ret;
327
328 while (blk && len) {
329 struct shared_block *fb;
330 enum htx_blk_type type = htx_get_blk_type(blk);
331 uint32_t sz = htx_get_blksz(blk);
332 struct ist v;
333
334 switch (type) {
335 case HTX_BLK_UNUSED:
336 break;
337
338 case HTX_BLK_DATA:
339 case HTX_BLK_TLR:
340 v = htx_get_blk_value(htx, blk);
341 v.ptr += offset;
342 v.len -= offset;
343 if (v.len > len)
344 v.len = len;
345
346 shctx_lock(shctx);
347 fb = shctx_row_reserve_hot(shctx, st->first_block, v.len);
348 if (!fb) {
349 shctx_unlock(shctx);
350 goto no_cache;
351 }
352 shctx_unlock(shctx);
353
354 ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
355 (unsigned char *)v.ptr, v.len);
356 if (ret < 0)
357 goto no_cache;
358
359 if (type == HTX_BLK_DATA)
360 object->data_len += v.len;
361 to_forward += v.len;
362 len -= v.len;
363 break;
364
365 default:
366 sz -= offset;
367 if (sz > len)
368 sz = len;
369 to_forward += sz;
370 len -= sz;
371 break;
372 }
373
374 offset = 0;
375 blk = htx_get_next_blk(htx, blk);
376 }
377
378 return to_forward;
379
380 no_cache:
381 disable_cache_entry(st, filter, shctx);
382 unregister_data_filter(s, msg->chn, filter);
383 return len;
384}
385
386static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100387cache_store_http_forward_data(struct stream *s, struct filter *filter,
388 struct http_msg *msg, unsigned int len)
389{
390 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100391 struct cache_flt_conf *cconf = FLT_CONF(filter);
392 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100393 int ret;
394
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200395 ret = 0;
396
William Lallemand4da3f8a2017-10-31 14:33:34 +0100397 /*
398 * We need to skip the HTTP headers first, because we saved them in the
399 * http-response action.
400 */
Christopher Faulet67658c92018-12-06 21:59:39 +0100401 if (!(msg->chn->flags & CF_ISRESP) || !st) {
402 /* should never happen */
403 unregister_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100404 return len;
Christopher Faulet67658c92018-12-06 21:59:39 +0100405 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100406
407 if (!len) {
Joseph Herlant8dae5b32018-11-15 14:07:53 -0800408 /* Nothing to forward */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100409 ret = len;
410 }
William Lallemand10935bc2017-11-14 14:39:23 +0100411 else if (st->hdrs_len >= len) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100412 /* Forward part of headers */
413 ret = len;
414 st->hdrs_len -= len;
415 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100416 else {
William Lallemand10935bc2017-11-14 14:39:23 +0100417 /* Forward data */
Christopher Faulet67658c92018-12-06 21:59:39 +0100418 if (st->first_block) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200419 int to_append, append;
420 struct shared_block *fb;
421
422 to_append = MIN(ci_contig_data(msg->chn), len - st->hdrs_len);
423
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200424 shctx_lock(shctx);
425 fb = shctx_row_reserve_hot(shctx, st->first_block, to_append);
426 if (!fb) {
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100427 shctx_unlock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200428 disable_cache_entry(st, filter, shctx);
Christopher Faulet67658c92018-12-06 21:59:39 +0100429 unregister_data_filter(s, msg->chn, filter);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200430 return len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100431 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200432 shctx_unlock(shctx);
433
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200434 /* Skip remaining headers to fill the cache */
435 c_adv(msg->chn, st->hdrs_len);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200436 append = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
437 (unsigned char *)ci_head(msg->chn), to_append);
438 ret = st->hdrs_len + to_append - append;
439 /* Rewind the buffer to forward all data */
440 c_rew(msg->chn, st->hdrs_len);
441 st->hdrs_len = 0;
Christopher Faulet67658c92018-12-06 21:59:39 +0100442 if (ret < 0) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200443 disable_cache_entry(st, filter, shctx);
Christopher Faulet67658c92018-12-06 21:59:39 +0100444 unregister_data_filter(s, msg->chn, filter);
445 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100446 }
Christopher Faulet67658c92018-12-06 21:59:39 +0100447 else {
448 /* should never happen */
449 unregister_data_filter(s, msg->chn, filter);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200450 ret = len;
Christopher Faulet67658c92018-12-06 21:59:39 +0100451 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100452 }
453
454 if ((ret != len) ||
455 (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret))
456 task_wakeup(s->task, TASK_WOKEN_MSG);
457
458 return ret;
459}
460
461static int
462cache_store_http_end(struct stream *s, struct filter *filter,
463 struct http_msg *msg)
464{
465 struct cache_st *st = filter->ctx;
Christopher Faulet95220e22018-12-07 17:34:39 +0100466 struct cache_flt_conf *cconf = FLT_CONF(filter);
467 struct cache *cache = cconf->c.cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100468 struct shared_context *shctx = shctx_ptr(cache);
469 struct cache_entry *object;
470
471 if (!(msg->chn->flags & CF_ISRESP))
472 return 1;
473
474 if (st && st->first_block) {
475
476 object = (struct cache_entry *)st->first_block->data;
477
478 /* does not need to test if the insertion worked, if it
479 * doesn't, the blocks will be reused anyway */
480
481 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100482 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
483 object->eb.key = 0;
484 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100485 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100486 shctx_row_dec_hot(shctx, st->first_block);
487 shctx_unlock(shctx);
488
489 }
490 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100491 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100492 filter->ctx = NULL;
493 }
494
495 return 1;
496}
497
498 /*
499 * This intends to be used when checking HTTP headers for some
500 * word=value directive. Return a pointer to the first character of value, if
501 * the word was not found or if there wasn't any value assigned ot it return NULL
502 */
503char *directive_value(const char *sample, int slen, const char *word, int wlen)
504{
505 int st = 0;
506
507 if (slen < wlen)
508 return 0;
509
510 while (wlen) {
511 char c = *sample ^ *word;
512 if (c && c != ('A' ^ 'a'))
513 return NULL;
514 sample++;
515 word++;
516 slen--;
517 wlen--;
518 }
519
520 while (slen) {
521 if (st == 0) {
522 if (*sample != '=')
523 return NULL;
524 sample++;
525 slen--;
526 st = 1;
527 continue;
528 } else {
529 return (char *)sample;
530 }
531 }
532
533 return NULL;
534}
535
536/*
537 * Return the maxage in seconds of an HTTP response.
538 * Compute the maxage using either:
539 * - the assigned max-age of the cache
540 * - the s-maxage directive
541 * - the max-age directive
542 * - (Expires - Data) headers
543 * - the default-max-age of the cache
544 *
545 */
William Lallemand49b44532017-11-24 18:53:43 +0100546int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100547{
548 struct http_txn *txn = s->txn;
549 struct hdr_ctx ctx;
550
551 int smaxage = -1;
552 int maxage = -1;
553
554
William Lallemand4da3f8a2017-10-31 14:33:34 +0100555 ctx.idx = 0;
556
557 /* loop on the Cache-Control values */
Willy Tarreau178b9872018-06-19 07:13:36 +0200558 while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100559 char *directive = ctx.line + ctx.val;
560 char *value;
561
562 value = directive_value(directive, ctx.vlen, "s-maxage", 8);
563 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200564 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100565
566 chunk_strncat(chk, value, ctx.vlen - 8 + 1);
567 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200568 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100569 }
570
571 value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
572 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200573 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100574
575 chunk_strncat(chk, value, ctx.vlen - 7 + 1);
576 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200577 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100578 }
579 }
580
581 /* TODO: Expires - Data */
582
583
584 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100585 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100586
587 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100588 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100589
William Lallemand49b44532017-11-24 18:53:43 +0100590 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100591
592}
593
594
William Lallemanda400a3a2017-11-20 19:13:12 +0100595static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
596{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200597 struct cache_entry *object = (struct cache_entry *)block->data;
598
599 if (first == block && object->eb.key)
600 eb32_delete(&object->eb);
601 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100602}
603
William Lallemand41db4602017-10-30 11:15:51 +0100604/*
605 * This fonction will store the headers of the response in a buffer and then
606 * register a filter to store the data
607 */
608enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
609 struct session *sess, struct stream *s, int flags)
610{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200611 unsigned int age;
612 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100613 struct http_txn *txn = s->txn;
614 struct http_msg *msg = &txn->rsp;
615 struct filter *filter;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100616 struct shared_block *first = NULL;
Christopher Faulet95220e22018-12-07 17:34:39 +0100617 struct cache_flt_conf *cconf = rule->arg.act.p[0];
618 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100619 struct cache_entry *object;
620
William Lallemand4da3f8a2017-10-31 14:33:34 +0100621 /* Don't cache if the response came from a cache */
622 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
623 s->target == &http_cache_applet.obj_type) {
624 goto out;
625 }
626
627 /* cache only HTTP/1.1 */
628 if (!(txn->req.flags & HTTP_MSGF_VER_11))
629 goto out;
630
631 /* cache only GET method */
632 if (txn->meth != HTTP_METH_GET)
633 goto out;
634
635 /* cache only 200 status code */
636 if (txn->status != 200)
637 goto out;
638
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100639 if (IS_HTX_STRM(s)) {
640 struct htx *htx = htxbuf(&s->res.buf);
641 struct http_hdr_ctx ctx;
642 int32_t pos;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100643
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100644 /* Do not cache too big objects. */
645 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
646 htx->data + htx->extra > shctx->max_obj_size)
647 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100648
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100649 /* Does not manage Vary at the moment. We will need a secondary key later for that */
650 ctx.blk = NULL;
651 if (http_find_header(htx, ist("Vary"), &ctx, 0))
652 goto out;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100653
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100654 htx_check_response_for_cacheability(s, &s->res);
655
656 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
657 goto out;
658
659 age = 0;
660 ctx.blk = NULL;
661 if (http_find_header(htx, ist("Age"), &ctx, 0)) {
662 if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) {
663 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
664 hdr_age = CACHE_ENTRY_MAX_AGE;
665 age = hdr_age;
666 }
667 http_remove_header(htx, &ctx);
668 }
669
670 chunk_reset(&trash);
671 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
672 struct htx_blk *blk = htx_get_blk(htx, pos);
Christopher Fauletafd819c2018-12-11 08:57:45 +0100673 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100674 uint32_t sz = htx_get_blksz(blk);
675
676 chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info));
Christopher Fauletafd819c2018-12-11 08:57:45 +0100677 if (type == HTX_BLK_EOH)
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100678 break;
679 chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz);
680 }
681 }
682 else {
683 struct hdr_ctx ctx;
684
685 /* Do not cache too big objects. */
686 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
687 msg->sov + msg->body_len > shctx->max_obj_size)
688 goto out;
689
690 /* Does not manage Vary at the moment. We will need a secondary key later for that */
691 ctx.idx = 0;
692 if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx))
693 goto out;
694
695 check_response_for_cacheability(s, &s->res);
696
697 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
698 goto out;
699
700 age = 0;
701 ctx.idx = 0;
702 if (http_find_header2("Age", 3, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
703 if (!strl2llrc(ctx.line + ctx.val, ctx.vlen, &hdr_age) && hdr_age > 0) {
704 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
705 hdr_age = CACHE_ENTRY_MAX_AGE;
706 age = hdr_age;
707 }
708 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200709 }
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200710 }
711
William Lallemand4da3f8a2017-10-31 14:33:34 +0100712 shctx_lock(shctx);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100713 if (IS_HTX_STRM(s))
714 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data);
715 else
716 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + msg->sov);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100717 if (!first) {
718 shctx_unlock(shctx);
719 goto out;
720 }
721 shctx_unlock(shctx);
722
Willy Tarreau1093a452018-04-06 19:02:25 +0200723 /* the received memory is not initialized, we need at least to mark
724 * the object as not indexed yet.
725 */
726 object = (struct cache_entry *)first->data;
727 object->eb.node.leaf_p = NULL;
728 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200729 object->age = age;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100730 if (IS_HTX_STRM(s)) {
731 object->hdrs_len = trash.data;
732 object->data_len = 0;
733 }
734 else
735 object->eoh = msg->eoh;
Willy Tarreau1093a452018-04-06 19:02:25 +0200736
William Lallemand4da3f8a2017-10-31 14:33:34 +0100737 /* reserve space for the cache_entry structure */
738 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200739 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100740 /* cache the headers in a http action because it allows to chose what
741 * to cache, for example you might want to cache a response before
742 * modifying some HTTP headers, or on the contrary after modifying
743 * those headers.
744 */
745
746 /* does not need to be locked because it's in the "hot" list,
747 * copy the headers */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100748 if (IS_HTX_STRM(s)) {
749 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0)
750 goto out;
751 }
752 else {
753 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)ci_head(&s->res), msg->sov) < 0)
754 goto out;
755 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100756
757 /* register the buffer in the filter ctx for filling it with data*/
Christopher Faulet95220e22018-12-07 17:34:39 +0100758 list_for_each_entry(filter, &s->strm_flt.filters, list) {
759 if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) {
760 if (filter->ctx) {
761 struct cache_st *cache_ctx = filter->ctx;
762 struct cache_entry *old;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100763
Christopher Faulet95220e22018-12-07 17:34:39 +0100764 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100765
Christopher Faulet95220e22018-12-07 17:34:39 +0100766 object->eb.key = (*(unsigned int *)&txn->cache_hash);
767 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
768 /* Insert the node later on caching success */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100769
Christopher Faulet95220e22018-12-07 17:34:39 +0100770 shctx_lock(shctx);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100771
Christopher Faulet95220e22018-12-07 17:34:39 +0100772 old = entry_exist(cconf->c.cache, txn->cache_hash);
773 if (old) {
774 eb32_delete(&old->eb);
775 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100776 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100777 shctx_unlock(shctx);
778
779 /* store latest value and expiration time */
780 object->latest_validation = now.tv_sec;
781 object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100782 }
Christopher Faulet95220e22018-12-07 17:34:39 +0100783 return ACT_RET_CONT;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100784 }
785 }
786
787out:
788 /* if does not cache */
789 if (first) {
790 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100791 first->len = 0;
792 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100793 shctx_row_dec_hot(shctx, first);
794 shctx_unlock(shctx);
795 }
796
William Lallemand41db4602017-10-30 11:15:51 +0100797 return ACT_RET_CONT;
798}
799
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200800#define HTTP_CACHE_INIT 0 /* Initial state. */
801#define HTTP_CACHE_HEADER 1 /* Cache entry headers forwarded. */
802#define HTTP_CACHE_FWD 2 /* Cache entry completely forwarded. */
803#define HTTP_CACHE_END 3 /* Cache entry treatment terminated. */
William Lallemand77c11972017-10-31 20:43:01 +0100804
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100805#define HTX_CACHE_INIT 0 /* Initial state. */
806#define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */
807#define HTX_CACHE_DATA 2 /* Cache entry data forwarding */
808#define HTX_CACHE_EOD 3 /* Cache entry data forwarded. DATA->TLR transition */
809#define HTX_CACHE_TLR 4 /* Cache entry trailers forwarding */
810#define HTX_CACHE_EOM 5 /* Cache entry completely forwarded. Finish the HTX message */
811#define HTX_CACHE_END 6 /* Cache entry treatment terminated */
812
William Lallemandecb73b12017-11-24 14:33:55 +0100813static void http_cache_applet_release(struct appctx *appctx)
814{
Christopher Faulet95220e22018-12-07 17:34:39 +0100815 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
William Lallemandecb73b12017-11-24 14:33:55 +0100816 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
Christopher Faulet95220e22018-12-07 17:34:39 +0100817 struct cache *cache = cconf->c.cache;
William Lallemandecb73b12017-11-24 14:33:55 +0100818 struct shared_block *first = block_ptr(cache_ptr);
819
820 shctx_lock(shctx_ptr(cache));
821 shctx_row_dec_hot(shctx_ptr(cache), first);
822 shctx_unlock(shctx_ptr(cache));
823}
824
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100825static size_t htx_cache_dump_headers(struct appctx *appctx, struct htx *htx)
826{
Christopher Faulet95220e22018-12-07 17:34:39 +0100827 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
828 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100829 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
830 struct shared_block *shblk = appctx->ctx.cache.next;
831 struct buffer *tmp = get_trash_chunk();
832 char *end;
833 unsigned int offset, len, age;
834
835 offset = appctx->ctx.cache.offset;
836 len = cache_ptr->hdrs_len;
837
838 /* 1. Retrieve all headers from the cache */
839 list_for_each_entry_from(shblk, &shctx->hot, list) {
840 int sz;
841
842 sz = MIN(len, shctx->block_size - offset);
843 if (!chunk_memcat(tmp, (const char *)shblk->data + offset, sz))
844 return 0;
845
846 offset += sz;
847 len -= sz;
848 if (!len)
849 break;
850 offset = 0;
851 }
852 appctx->ctx.cache.offset = offset;
853 appctx->ctx.cache.next = shblk;
854 appctx->ctx.cache.sent += b_data(tmp);
855
856 /* 2. push these headers in the HTX message */
857 offset = 0;
858 while (offset < b_data(tmp)) {
859 struct htx_blk *blk;
860 enum htx_blk_type type;
861 uint32_t info, sz;
862
863 /* Read the header's info */
864 memcpy((char *)&info, b_peek(tmp, offset), 4);
865 type = (info >> 28);
866 sz = ((type == HTX_BLK_HDR)
867 ? (info & 0xff) + ((info >> 8) & 0xfffff)
868 : info & 0xfffffff);
869
870 /* Create the block with the right type and the right size */
871 blk = htx_add_blk(htx, type, sz);
872 if (!blk)
873 return 0;
874
875 /* Copy info and data */
876 blk->info = info;
877 memcpy(htx_get_blk_ptr(htx, blk), b_peek(tmp, offset+4), sz);
878
879 /* next header */
880 offset += 4 + sz;
881 }
882
883 /* 3. Append "age" header */
884 chunk_reset(tmp);
885 age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age;
886 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
887 age = CACHE_ENTRY_MAX_AGE;
888 end = ultoa_o(age, b_head(tmp), b_size(tmp));
889 b_set_data(tmp, end - b_head(tmp));
890
891 if (!http_add_header(htx, ist("Age"), ist2(b_head(tmp), b_data(tmp))))
892 return 0;
893
894 return htx->data;
895}
896
897static size_t htx_cache_dump_data(struct appctx *appctx, struct htx *htx,
898 enum htx_blk_type type, unsigned int len)
899{
Christopher Faulet95220e22018-12-07 17:34:39 +0100900 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
901 struct shared_context *shctx = shctx_ptr(cconf->c.cache);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100902 struct shared_block *shblk = appctx->ctx.cache.next;
903 uint32_t max = htx_free_data_space(htx);
904 unsigned int offset;
905 size_t total = 0;
906
907 offset = appctx->ctx.cache.offset;
908 if (len > max)
909 len = max;
910 if (!len)
911 goto end;
912
913 list_for_each_entry_from(shblk, &shctx->hot, list) {
914 struct ist data;
915 int sz;
916
917 sz = MIN(len, shctx->block_size - offset);
918 data = ist2((const char *)shblk->data + offset, sz);
919 if (type == HTX_BLK_DATA) {
920 if (!htx_add_data(htx, data))
921 break;
922 }
923 else { /* HTX_BLK_TLR */
924 if (!htx_add_trailer(htx, data))
925 break;
926 }
927
928 offset += sz;
929 len -= sz;
930 total += sz;
931 if (!len)
932 break;
933 offset = 0;
934 }
935 appctx->ctx.cache.offset = offset;
936 appctx->ctx.cache.next = shblk;
937 appctx->ctx.cache.sent += total;
938
939 end:
940 return total;
941}
942static void htx_cache_io_handler(struct appctx *appctx)
943{
944 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
945 struct shared_block *first = block_ptr(cache_ptr);
946 struct stream_interface *si = appctx->owner;
947 struct channel *req = si_oc(si);
948 struct channel *res = si_ic(si);
949 struct htx *req_htx, *res_htx;
950 struct buffer *errmsg;
951 size_t ret, total = 0;
952
953 res_htx = htxbuf(&res->buf);
954
955 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
956 goto out;
957
958 /* Check if the input buffer is avalaible. */
959 if (!b_size(&res->buf)) {
960 si_rx_room_blk(si);
961 goto out;
962 }
963
Willy Tarreauefef3232018-12-16 00:37:45 +0100964 if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW))
Willy Tarreau273e9642018-12-16 00:35:15 +0100965 appctx->st0 = HTX_CACHE_END;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +0100966
967 if (appctx->st0 == HTX_CACHE_INIT) {
968 appctx->ctx.cache.next = block_ptr(cache_ptr);
969 appctx->ctx.cache.offset = sizeof(*cache_ptr);
970 appctx->ctx.cache.sent = 0;
971 appctx->st0 = HTX_CACHE_HEADER;
972 }
973
974 if (appctx->st0 == HTX_CACHE_HEADER) {
975 /* Headers must be dump at once. Otherwise it is an error */
976 ret = htx_cache_dump_headers(appctx, res_htx);
977 if (!ret)
978 goto error;
979
980 total += ret;
981 if (cache_ptr->data_len)
982 appctx->st0 = HTX_CACHE_DATA;
983 else if (first->len > sizeof(*cache_ptr) + appctx->ctx.cache.sent) {
984 /* Headers have benn sent (hrds_len) and there is no data
985 * (data_len == 0). So, all the remaining is the
986 * trailers */
987 appctx->st0 = HTX_CACHE_EOD;
988 }
989 else
990 appctx->st0 = HTX_CACHE_EOM;
991 }
992
993 if (appctx->st0 == HTX_CACHE_DATA) {
994 unsigned int len = cache_ptr->hdrs_len + cache_ptr->data_len - appctx->ctx.cache.sent;
995
996 ret = htx_cache_dump_data(appctx, res_htx, HTX_BLK_DATA, len);
997 if (!ret) {
998 si_rx_room_blk(si);
999 goto out;
1000 }
1001
1002 total += ret;
1003 if (cache_ptr->hdrs_len + cache_ptr->data_len == appctx->ctx.cache.sent) {
1004 if (first->len > sizeof(*cache_ptr) + appctx->ctx.cache.sent) {
1005 /* Headers and all data have been sent
1006 * (hrds_len + data_len == sent). So, all the remaining
1007 * is the trailers */
1008 appctx->st0 = HTX_CACHE_EOD;
1009 }
1010 else
1011 appctx->st0 = HTX_CACHE_EOM;
1012 }
1013 }
1014
1015 if (appctx->st0 == HTX_CACHE_EOD) {
1016 if (!htx_add_endof(res_htx, HTX_BLK_EOD)) {
1017 si_rx_room_blk(si);
1018 goto out;
1019 }
1020
1021 total++;
1022 appctx->st0 = HTX_CACHE_TLR;
1023 }
1024
1025 if (appctx->st0 == HTX_CACHE_TLR) {
1026 unsigned int len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1027
1028 ret = htx_cache_dump_data(appctx, res_htx, HTX_BLK_TLR, len);
1029 if (!ret) {
1030 si_rx_room_blk(si);
1031 goto out;
1032 }
1033
1034 total += ret;
1035 if (first->len == sizeof(*cache_ptr) + appctx->ctx.cache.sent)
1036 appctx->st0 = HTX_CACHE_EOM;
1037 }
1038
1039 if (appctx->st0 == HTX_CACHE_EOM) {
1040 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1041 si_rx_room_blk(si);
1042 goto out;
1043 }
1044
1045 total++;
1046 appctx->st0 = HTX_CACHE_END;
1047 }
1048
1049 end:
1050 if (appctx->st0 == HTX_CACHE_END) {
1051 /* eat the whole request */
1052 req_htx = htxbuf(&req->buf);
1053 htx_reset(req_htx);
1054 htx_to_buf(req_htx, &req->buf);
1055 co_set_data(req, 0);
1056 res->flags |= CF_READ_NULL;
1057 si_shutr(si);
1058 }
1059
1060 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
1061 si_shutw(si);
1062
1063 if (appctx->st0 == HTX_CACHE_END) {
1064 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
1065 si_shutr(si);
1066 res->flags |= CF_READ_NULL;
1067 }
1068 }
1069 out:
1070 if (total) {
1071 res->total += total;
1072 res->flags |= CF_READ_PARTIAL;
1073 }
1074
1075 /* we have left the request in the buffer for the case where we
1076 * process a POST, and this automatically re-enables activity on
1077 * read. It's better to indicate that we want to stop reading when
1078 * we're sending, so that we know there's at most one direction
1079 * deciding to wake the applet up. It saves it from looping when
1080 * emitting large blocks into small TCP windows.
1081 */
1082 htx_to_buf(res_htx, &res->buf);
1083 if (!channel_is_empty(res))
1084 si_stop_get(si);
1085 return;
1086
1087 error:
1088 /* Sent and HTTP error 500 */
1089 b_reset(&res->buf);
1090 errmsg = &htx_err_chunks[HTTP_ERR_500];
1091 res->buf.data = b_data(errmsg);
1092 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1093 res_htx = htx_from_buf(&res->buf);
1094
1095 total = res_htx->data;
1096 appctx->st0 = HTX_CACHE_END;
1097 goto end;
1098}
1099
1100
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001101/*
1102 * Append an "Age" header into <chn> channel for this <ce> cache entry.
Joseph Herlant8dae5b32018-11-15 14:07:53 -08001103 * This is the responsibility of the caller to insure there is enough
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001104 * data in the channel.
1105 *
1106 * Returns the number of bytes inserted if succeeded, 0 if failed.
1107 */
1108static int cache_channel_append_age_header(struct cache_entry *ce, struct channel *chn)
1109{
1110 unsigned int age;
1111
1112 age = MAX(0, (int)(now.tv_sec - ce->latest_validation)) + ce->age;
1113 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1114 age = CACHE_ENTRY_MAX_AGE;
1115
1116 chunk_reset(&trash);
1117 chunk_printf(&trash, "Age: %u", age);
1118
1119 return ci_insert_line2(chn, ce->eoh, trash.area, trash.data);
1120}
1121
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001122static int cache_channel_row_data_get(struct appctx *appctx, int len)
William Lallemand77c11972017-10-31 20:43:01 +01001123{
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001124 int ret, total;
William Lallemand77c11972017-10-31 20:43:01 +01001125 struct stream_interface *si = appctx->owner;
1126 struct channel *res = si_ic(si);
Christopher Faulet95220e22018-12-07 17:34:39 +01001127 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1128 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001129 struct shared_context *shctx = shctx_ptr(cache);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001130 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1131 struct shared_block *blk, *next = appctx->ctx.cache.next;
1132 int offset;
1133
1134 total = 0;
1135 offset = 0;
1136
1137 if (!next) {
1138 offset = sizeof(struct cache_entry);
1139 next = block_ptr(cache_ptr);
1140 }
1141
1142 blk = next;
1143 list_for_each_entry_from(blk, &shctx->hot, list) {
1144 int sz;
1145
1146 if (len <= 0)
1147 break;
1148
1149 sz = MIN(len, shctx->block_size - offset);
1150
1151 ret = ci_putblk(res, (const char *)blk->data + offset, sz);
1152 if (unlikely(offset))
1153 offset = 0;
1154 if (ret <= 0) {
1155 if (ret == -3 || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001156 si_rx_room_blk(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001157 break;
1158 }
1159 return -1;
1160 }
1161
1162 total += sz;
1163 len -= sz;
1164 }
1165 appctx->ctx.cache.next = blk;
1166
1167 return total;
1168}
1169
1170static void http_cache_io_handler(struct appctx *appctx)
1171{
1172 struct stream_interface *si = appctx->owner;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001173 struct stream *s = si_strm(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001174 struct channel *res = si_ic(si);
1175 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
William Lallemand77c11972017-10-31 20:43:01 +01001176 struct shared_block *first = block_ptr(cache_ptr);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001177 unsigned int *sent = &appctx->ctx.cache.sent;
1178
1179 if (IS_HTX_STRM(s))
1180 return htx_cache_io_handler(appctx);
William Lallemand77c11972017-10-31 20:43:01 +01001181
1182 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1183 goto out;
1184
Joseph Herlant8dae5b32018-11-15 14:07:53 -08001185 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001186 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01001187 /* buf.size==0 means we failed to get a buffer and were
1188 * already subscribed to a wait list to get a buffer.
1189 */
William Lallemand77c11972017-10-31 20:43:01 +01001190 goto out;
1191 }
1192
Willy Tarreauefef3232018-12-16 00:37:45 +01001193 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
William Lallemand77c11972017-10-31 20:43:01 +01001194 appctx->st0 = HTTP_CACHE_END;
1195
1196 /* buffer are aligned there, should be fine */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001197 if (appctx->st0 == HTTP_CACHE_HEADER || appctx->st0 == HTTP_CACHE_INIT) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001198 int len = first->len - *sent - sizeof(struct cache_entry);
William Lallemand55e76742017-11-21 20:01:28 +01001199
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001200 if (len > 0) {
1201 int ret;
1202
1203 ret = cache_channel_row_data_get(appctx, len);
1204 if (ret == -1)
1205 appctx->st0 = HTTP_CACHE_END;
1206 else
1207 *sent += ret;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001208 if (appctx->st0 == HTTP_CACHE_INIT && *sent > cache_ptr->eoh &&
1209 cache_channel_append_age_header(cache_ptr, res))
1210 appctx->st0 = HTTP_CACHE_HEADER;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001211 }
1212 else {
1213 *sent = 0;
1214 appctx->st0 = HTTP_CACHE_FWD;
William Lallemand77c11972017-10-31 20:43:01 +01001215 }
William Lallemand77c11972017-10-31 20:43:01 +01001216 }
1217
1218 if (appctx->st0 == HTTP_CACHE_FWD) {
1219 /* eat the whole request */
Willy Tarreau178b9872018-06-19 07:13:36 +02001220 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 +01001221 res->flags |= CF_READ_NULL;
1222 si_shutr(si);
1223 }
1224
1225 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
1226 si_shutw(si);
1227out:
1228 ;
1229}
1230
Christopher Faulet95220e22018-12-07 17:34:39 +01001231static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001232{
1233 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001234 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001235
Christopher Faulet95220e22018-12-07 17:34:39 +01001236 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001237 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001238 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001239 }
1240
1241 /* check if a cache filter was already registered with this cache
1242 * name, if that's the case, must use it. */
1243 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001244 if (fconf->id == cache_store_flt_id) {
1245 cconf = fconf->conf;
1246 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1247 rule->arg.act.p[0] = cconf;
1248 return 1;
1249 }
William Lallemand41db4602017-10-30 11:15:51 +01001250 }
1251 }
1252
Christopher Faulet95220e22018-12-07 17:34:39 +01001253 /* Create the filter cache config */
1254 cconf = calloc(1, sizeof(*cconf));
1255 if (!cconf) {
1256 memprintf(err, "out of memory\n");
1257 goto err;
1258 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001259 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001260 cconf->c.name = strdup(name);
1261 if (!cconf->c.name) {
1262 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001263 goto err;
1264 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001265
William Lallemand41db4602017-10-30 11:15:51 +01001266 /* register a filter to fill the cache buffer */
1267 fconf = calloc(1, sizeof(*fconf));
1268 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001269 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001270 goto err;
1271 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001272 fconf->id = cache_store_flt_id;
1273 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001274 fconf->ops = &cache_ops;
1275 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1276
Christopher Faulet95220e22018-12-07 17:34:39 +01001277 rule->arg.act.p[0] = cconf;
1278 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001279
Christopher Faulet95220e22018-12-07 17:34:39 +01001280 err:
1281 free(cconf);
1282 return 0;
1283}
1284
1285enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1286 struct act_rule *rule, char **err)
1287{
1288 rule->action = ACT_CUSTOM;
1289 rule->action_ptr = http_action_store_cache;
1290
1291 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1292 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001293
Christopher Faulet95220e22018-12-07 17:34:39 +01001294 (*orig_arg)++;
1295 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001296}
1297
William Lallemandf528fff2017-11-23 19:43:17 +01001298/* This produces a sha1 hash of the concatenation of the first
1299 * occurrence of the Host header followed by the path component if it
1300 * begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001301int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001302{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001303 struct http_txn *txn = s->txn;
William Lallemandf528fff2017-11-23 19:43:17 +01001304 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001305 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001306
William Lallemandf528fff2017-11-23 19:43:17 +01001307 trash = get_trash_chunk();
1308
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001309 if (IS_HTX_STRM(s)) {
1310 struct htx *htx = htxbuf(&s->req.buf);
1311 struct htx_sl *sl;
1312 struct http_hdr_ctx ctx;
1313 struct ist path;
William Lallemandf528fff2017-11-23 19:43:17 +01001314
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001315 ctx.blk = NULL;
1316 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1317 return 0;
1318 chunk_memcat(trash, ctx.value.ptr, ctx.value.len);
1319
1320 sl = http_find_stline(htx);
1321 path = http_get_path(htx_sl_req_uri(sl));
1322 if (!path.ptr)
1323 return 0;
1324 chunk_memcat(trash, path.ptr, path.len);
1325 }
1326 else {
1327 struct hdr_ctx ctx;
1328 char *path;
1329 char *end;
1330
1331 /* retrive the host */
1332 ctx.idx = 0;
1333 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx))
1334 return 0;
1335 chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen);
1336
1337 /* now retrieve the path */
1338 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1339 path = http_txn_get_path(txn);
1340 if (!path)
1341 return 0;
1342 chunk_strncat(trash, path, end - path);
1343 }
William Lallemandf528fff2017-11-23 19:43:17 +01001344
1345 /* hash everything */
1346 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001347 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001348 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1349
1350 return 1;
1351}
1352
William Lallemand41db4602017-10-30 11:15:51 +01001353enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1354 struct session *sess, struct stream *s, int flags)
1355{
William Lallemand77c11972017-10-31 20:43:01 +01001356
William Lallemand77c11972017-10-31 20:43:01 +01001357 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001358 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1359 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001360
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001361 if (IS_HTX_STRM(s))
1362 htx_check_request_for_cacheability(s, &s->req);
1363 else
1364 check_request_for_cacheability(s, &s->req);
1365
Willy Tarreau504455c2017-12-22 17:47:35 +01001366 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1367 return ACT_RET_CONT;
1368
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001369 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001370 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001371
Willy Tarreau504455c2017-12-22 17:47:35 +01001372 if (s->txn->flags & TX_CACHE_IGNORE)
1373 return ACT_RET_CONT;
1374
Willy Tarreaua1214a52018-12-14 14:00:25 +01001375 if (px == strm_fe(s))
1376 HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
1377 else
1378 HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
1379
William Lallemanda400a3a2017-11-20 19:13:12 +01001380 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001381 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001382 if (res) {
1383 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001384 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1385 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001386 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001387 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
William Lallemand77c11972017-10-31 20:43:01 +01001388 appctx->st0 = HTTP_CACHE_INIT;
1389 appctx->rule = rule;
1390 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001391 appctx->ctx.cache.next = NULL;
1392 appctx->ctx.cache.sent = 0;
Willy Tarreaua1214a52018-12-14 14:00:25 +01001393
1394 if (px == strm_fe(s))
1395 HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
1396 else
1397 HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001398 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001399 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001400 shctx_lock(shctx_ptr(cache));
1401 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1402 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001403 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001404 }
1405 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001406 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001407 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001408}
1409
1410
1411enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1412 struct act_rule *rule, char **err)
1413{
William Lallemand41db4602017-10-30 11:15:51 +01001414 rule->action = ACT_CUSTOM;
1415 rule->action_ptr = http_action_req_cache_use;
1416
Christopher Faulet95220e22018-12-07 17:34:39 +01001417 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001418 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001419
1420 (*orig_arg)++;
1421 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001422}
1423
1424int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1425{
1426 int err_code = 0;
1427
1428 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1429
1430 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001431 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1432 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001433 err_code |= ERR_ALERT | ERR_ABORT;
1434 goto out;
1435 }
1436
1437 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1438 err_code |= ERR_ABORT;
1439 goto out;
1440 }
1441
1442 if (tmp_cache_config == NULL) {
1443 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1444 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001445 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001446 err_code |= ERR_ALERT | ERR_ABORT;
1447 goto out;
1448 }
1449
1450 strlcpy2(tmp_cache_config->id, args[1], 33);
1451 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001452 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1453 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001454 err_code |= ERR_WARN;
1455 }
William Lallemand49b44532017-11-24 18:53:43 +01001456 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001457 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001458 tmp_cache_config->maxobjsz = 0;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001459 tmp_cache_config->flags = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001460 }
1461 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001462 unsigned long int maxsize;
1463 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001464
1465 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1466 err_code |= ERR_ABORT;
1467 goto out;
1468 }
1469
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001470 maxsize = strtoul(args[1], &err, 10);
1471 if (err == args[1] || *err != '\0') {
1472 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1473 file, linenum, args[1]);
1474 err_code |= ERR_ABORT;
1475 goto out;
1476 }
1477
1478 if (maxsize > (UINT_MAX >> 20)) {
1479 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1480 file, linenum, args[1], UINT_MAX >> 20);
1481 err_code |= ERR_ABORT;
1482 goto out;
1483 }
1484
William Lallemand41db4602017-10-30 11:15:51 +01001485 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001486 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001487 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001488 } else if (strcmp(args[0], "max-age") == 0) {
1489 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1490 err_code |= ERR_ABORT;
1491 goto out;
1492 }
1493
1494 if (!*args[1]) {
1495 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1496 file, linenum, args[0]);
1497 err_code |= ERR_WARN;
1498 }
1499
1500 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001501 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001502 unsigned int maxobjsz;
1503 char *err;
1504
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001505 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1506 err_code |= ERR_ABORT;
1507 goto out;
1508 }
1509
1510 if (!*args[1]) {
1511 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1512 file, linenum, args[0]);
1513 err_code |= ERR_WARN;
1514 }
1515
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001516 maxobjsz = strtoul(args[1], &err, 10);
1517 if (err == args[1] || *err != '\0') {
1518 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1519 file, linenum, args[1]);
1520 err_code |= ERR_ABORT;
1521 goto out;
1522 }
1523 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001524 }
1525 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001526 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001527 err_code |= ERR_ALERT | ERR_FATAL;
1528 goto out;
1529 }
1530out:
1531 return err_code;
1532}
1533
1534/* once the cache section is parsed */
1535
1536int cfg_post_parse_section_cache()
1537{
1538 struct shared_context *shctx;
1539 int err_code = 0;
1540 int ret_shctx;
1541
1542 if (tmp_cache_config) {
1543 struct cache *cache;
1544
1545 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001546 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001547 err_code |= ERR_FATAL | ERR_ALERT;
1548 goto out;
1549 }
1550
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001551 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001552 /* Default max. file size is a 256th of the cache size. */
1553 tmp_cache_config->maxobjsz =
1554 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001555 }
1556 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1557 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1558 err_code |= ERR_FATAL | ERR_ALERT;
1559 goto out;
1560 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001561
1562 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE,
1563 tmp_cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001564
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001565 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001566 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001567 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001568 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001569 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001570
1571 err_code |= ERR_FATAL | ERR_ALERT;
1572 goto out;
1573 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001574 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +01001575 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
1576 cache = (struct cache *)shctx->data;
1577 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001578 LIST_ADDQ(&caches, &cache->list);
1579 }
1580out:
1581 free(tmp_cache_config);
1582 tmp_cache_config = NULL;
1583 return err_code;
1584
1585}
1586
1587/*
1588 * Resolve the cache name to a pointer once the file is completely read.
1589 */
1590int cfg_cache_postparser()
1591{
William Lallemand41db4602017-10-30 11:15:51 +01001592 struct cache *cache;
William Lallemand41db4602017-10-30 11:15:51 +01001593 int err = 0;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001594
1595 /* Check if the cache is used by HTX and legacy HTTP proxies in same
1596 * time
1597 */
1598 list_for_each_entry(cache, &caches, list) {
1599 if ((cache->flags & (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) == (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) {
1600 ha_alert("Cache '%s': cannot be used by HTX and legacy HTTP proxies in same time.\n",
1601 cache->id);
1602 err++;
1603 }
1604 }
1605
William Lallemand41db4602017-10-30 11:15:51 +01001606 return err;
1607}
1608
1609
1610struct flt_ops cache_ops = {
1611 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001612 .check = cache_store_check,
1613 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001614
William Lallemand4da3f8a2017-10-31 14:33:34 +01001615 /* Handle channels activity */
1616 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001617 .channel_end_analyze = cache_store_chn_end_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001618
1619 /* Filter HTTP requests and responses */
1620 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001621 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001622 .http_end = cache_store_http_end,
1623
1624 .http_forward_data = cache_store_http_forward_data,
1625
William Lallemand41db4602017-10-30 11:15:51 +01001626};
1627
Christopher Faulet99a17a22018-12-11 09:18:27 +01001628
1629
1630static int
1631parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1632 struct flt_conf *fconf, char **err, void *private)
1633{
1634 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001635 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001636 char *name = NULL;
1637 int pos = *cur_arg;
1638
1639 /* Get the cache filter name*/
1640 if (!strcmp(args[pos], "cache")) {
1641 if (!*args[pos + 1]) {
1642 memprintf(err, "%s : expects an <id> argument", args[pos]);
1643 goto error;
1644 }
1645 name = strdup(args[pos + 1]);
1646 if (!name) {
1647 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1648 goto error;
1649 }
1650 pos += 2;
1651 }
1652
1653 /* Check if an implicit filter with the same name already exists. If so,
1654 * we remove the implicit filter to use the explicit one. */
1655 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1656 if (f->id != cache_store_flt_id)
1657 continue;
1658
1659 cconf = f->conf;
1660 if (strcmp(name, cconf->c.name)) {
1661 cconf = NULL;
1662 continue;
1663 }
1664
1665 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1666 cconf = NULL;
1667 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1668 px->id, name);
1669 return -1;
1670 }
1671
1672 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1673 LIST_DEL(&f->list);
1674 free(f);
1675 free(name);
1676 break;
1677 }
1678
1679 /* No implicit cache filter found, create configuration for the explicit one */
1680 if (!cconf) {
1681 cconf = calloc(1, sizeof(*cconf));
1682 if (!cconf) {
1683 memprintf(err, "%s: out of memory", args[*cur_arg]);
1684 goto error;
1685 }
1686 cconf->c.name = name;
1687 }
1688
1689 cconf->flags = 0;
1690 fconf->id = cache_store_flt_id;
1691 fconf->conf = cconf;
1692 fconf->ops = &cache_ops;
1693
1694 *cur_arg = pos;
1695 return 0;
1696
1697 error:
1698 free(name);
1699 free(cconf);
1700 return -1;
1701}
1702
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001703static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001704{
1705 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1706 return 1;
1707
1708 return 0;
1709}
1710
1711static int cli_io_handler_show_cache(struct appctx *appctx)
1712{
1713 struct cache* cache = appctx->ctx.cli.p0;
1714 struct stream_interface *si = appctx->owner;
1715
William Lallemand1f49a362017-11-21 20:01:26 +01001716 if (cache == NULL) {
1717 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1718 }
1719
1720 list_for_each_entry_from(cache, &caches, list) {
1721 struct eb32_node *node = NULL;
1722 unsigned int next_key;
1723 struct cache_entry *entry;
1724
William Lallemand1f49a362017-11-21 20:01:26 +01001725 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001726 if (!next_key) {
1727 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1728 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001729 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001730 return 0;
1731 }
1732 }
William Lallemand1f49a362017-11-21 20:01:26 +01001733
1734 appctx->ctx.cli.p0 = cache;
1735
1736 while (1) {
1737
1738 shctx_lock(shctx_ptr(cache));
1739 node = eb32_lookup_ge(&cache->entries, next_key);
1740 if (!node) {
1741 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001742 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001743 break;
1744 }
1745
1746 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001747 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 +01001748
1749 next_key = node->key + 1;
1750 appctx->ctx.cli.i0 = next_key;
1751
1752 shctx_unlock(shctx_ptr(cache));
1753
1754 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001755 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001756 return 0;
1757 }
1758 }
1759
1760 }
1761
1762 return 1;
1763
1764}
1765
Christopher Faulet99a17a22018-12-11 09:18:27 +01001766/* Declare the filter parser for "cache" keyword */
1767static struct flt_kw_list filter_kws = { "CACHE", { }, {
1768 { "cache", parse_cache_flt, NULL },
1769 { NULL, NULL, NULL },
1770 }
1771};
1772
1773INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1774
William Lallemand1f49a362017-11-21 20:01:26 +01001775static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001776 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1777 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001778}};
1779
Willy Tarreau0108d902018-11-25 19:14:37 +01001780INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001781
William Lallemand41db4602017-10-30 11:15:51 +01001782static struct action_kw_list http_res_actions = {
1783 .kw = {
1784 { "cache-store", parse_cache_store },
1785 { NULL, NULL }
1786 }
1787};
1788
Willy Tarreau0108d902018-11-25 19:14:37 +01001789INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1790
William Lallemand41db4602017-10-30 11:15:51 +01001791static struct action_kw_list http_req_actions = {
1792 .kw = {
1793 { "cache-use", parse_cache_use },
1794 { NULL, NULL }
1795 }
1796};
1797
Willy Tarreau0108d902018-11-25 19:14:37 +01001798INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1799
William Lallemand41db4602017-10-30 11:15:51 +01001800struct applet http_cache_applet = {
1801 .obj_type = OBJ_TYPE_APPLET,
1802 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001803 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001804 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001805};
1806
Willy Tarreaue6552512018-11-26 11:33:13 +01001807/* config parsers for this section */
1808REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
1809REGISTER_CONFIG_POSTPARSER("cache", cfg_cache_postparser);