blob: d80ee904f9c99d7b6825780275a484c201456d53 [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);
Christopher Faulet61123912019-01-02 14:10:01 +0100997 total += ret;
998 res_htx->extra = (len - ret);
999 if (ret < len) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001000 si_rx_room_blk(si);
1001 goto out;
1002 }
1003
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001004 if (cache_ptr->hdrs_len + cache_ptr->data_len == appctx->ctx.cache.sent) {
1005 if (first->len > sizeof(*cache_ptr) + appctx->ctx.cache.sent) {
1006 /* Headers and all data have been sent
1007 * (hrds_len + data_len == sent). So, all the remaining
1008 * is the trailers */
1009 appctx->st0 = HTX_CACHE_EOD;
1010 }
1011 else
1012 appctx->st0 = HTX_CACHE_EOM;
1013 }
1014 }
1015
1016 if (appctx->st0 == HTX_CACHE_EOD) {
1017 if (!htx_add_endof(res_htx, HTX_BLK_EOD)) {
1018 si_rx_room_blk(si);
1019 goto out;
1020 }
1021
1022 total++;
1023 appctx->st0 = HTX_CACHE_TLR;
1024 }
1025
1026 if (appctx->st0 == HTX_CACHE_TLR) {
1027 unsigned int len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent;
1028
1029 ret = htx_cache_dump_data(appctx, res_htx, HTX_BLK_TLR, len);
Christopher Faulet61123912019-01-02 14:10:01 +01001030 if (ret < len) {
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001031 si_rx_room_blk(si);
1032 goto out;
1033 }
1034
1035 total += ret;
1036 if (first->len == sizeof(*cache_ptr) + appctx->ctx.cache.sent)
1037 appctx->st0 = HTX_CACHE_EOM;
1038 }
1039
1040 if (appctx->st0 == HTX_CACHE_EOM) {
1041 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1042 si_rx_room_blk(si);
1043 goto out;
1044 }
1045
1046 total++;
1047 appctx->st0 = HTX_CACHE_END;
1048 }
1049
1050 end:
1051 if (appctx->st0 == HTX_CACHE_END) {
1052 /* eat the whole request */
1053 req_htx = htxbuf(&req->buf);
1054 htx_reset(req_htx);
1055 htx_to_buf(req_htx, &req->buf);
1056 co_set_data(req, 0);
1057 res->flags |= CF_READ_NULL;
1058 si_shutr(si);
1059 }
1060
1061 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
1062 si_shutw(si);
1063
1064 if (appctx->st0 == HTX_CACHE_END) {
1065 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
1066 si_shutr(si);
1067 res->flags |= CF_READ_NULL;
1068 }
1069 }
1070 out:
Christopher Faulet61123912019-01-02 14:10:01 +01001071 if (total)
1072 channel_add_input(res, total);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001073
1074 /* we have left the request in the buffer for the case where we
1075 * process a POST, and this automatically re-enables activity on
1076 * read. It's better to indicate that we want to stop reading when
1077 * we're sending, so that we know there's at most one direction
1078 * deciding to wake the applet up. It saves it from looping when
1079 * emitting large blocks into small TCP windows.
1080 */
1081 htx_to_buf(res_htx, &res->buf);
1082 if (!channel_is_empty(res))
1083 si_stop_get(si);
1084 return;
1085
1086 error:
1087 /* Sent and HTTP error 500 */
1088 b_reset(&res->buf);
1089 errmsg = &htx_err_chunks[HTTP_ERR_500];
1090 res->buf.data = b_data(errmsg);
1091 memcpy(res->buf.area, b_head(errmsg), b_data(errmsg));
1092 res_htx = htx_from_buf(&res->buf);
1093
1094 total = res_htx->data;
1095 appctx->st0 = HTX_CACHE_END;
1096 goto end;
1097}
1098
1099
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001100/*
1101 * Append an "Age" header into <chn> channel for this <ce> cache entry.
Joseph Herlant8dae5b32018-11-15 14:07:53 -08001102 * This is the responsibility of the caller to insure there is enough
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001103 * data in the channel.
1104 *
1105 * Returns the number of bytes inserted if succeeded, 0 if failed.
1106 */
1107static int cache_channel_append_age_header(struct cache_entry *ce, struct channel *chn)
1108{
1109 unsigned int age;
1110
1111 age = MAX(0, (int)(now.tv_sec - ce->latest_validation)) + ce->age;
1112 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
1113 age = CACHE_ENTRY_MAX_AGE;
1114
1115 chunk_reset(&trash);
1116 chunk_printf(&trash, "Age: %u", age);
1117
1118 return ci_insert_line2(chn, ce->eoh, trash.area, trash.data);
1119}
1120
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001121static int cache_channel_row_data_get(struct appctx *appctx, int len)
William Lallemand77c11972017-10-31 20:43:01 +01001122{
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001123 int ret, total;
William Lallemand77c11972017-10-31 20:43:01 +01001124 struct stream_interface *si = appctx->owner;
1125 struct channel *res = si_ic(si);
Christopher Faulet95220e22018-12-07 17:34:39 +01001126 struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0];
1127 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001128 struct shared_context *shctx = shctx_ptr(cache);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001129 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
1130 struct shared_block *blk, *next = appctx->ctx.cache.next;
1131 int offset;
1132
1133 total = 0;
1134 offset = 0;
1135
1136 if (!next) {
1137 offset = sizeof(struct cache_entry);
1138 next = block_ptr(cache_ptr);
1139 }
1140
1141 blk = next;
1142 list_for_each_entry_from(blk, &shctx->hot, list) {
1143 int sz;
1144
1145 if (len <= 0)
1146 break;
1147
1148 sz = MIN(len, shctx->block_size - offset);
1149
1150 ret = ci_putblk(res, (const char *)blk->data + offset, sz);
1151 if (unlikely(offset))
1152 offset = 0;
1153 if (ret <= 0) {
1154 if (ret == -3 || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001155 si_rx_room_blk(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001156 break;
1157 }
1158 return -1;
1159 }
1160
1161 total += sz;
1162 len -= sz;
1163 }
1164 appctx->ctx.cache.next = blk;
1165
1166 return total;
1167}
1168
1169static void http_cache_io_handler(struct appctx *appctx)
1170{
1171 struct stream_interface *si = appctx->owner;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001172 struct stream *s = si_strm(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001173 struct channel *res = si_ic(si);
1174 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
William Lallemand77c11972017-10-31 20:43:01 +01001175 struct shared_block *first = block_ptr(cache_ptr);
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001176 unsigned int *sent = &appctx->ctx.cache.sent;
1177
1178 if (IS_HTX_STRM(s))
1179 return htx_cache_io_handler(appctx);
William Lallemand77c11972017-10-31 20:43:01 +01001180
1181 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1182 goto out;
1183
Joseph Herlant8dae5b32018-11-15 14:07:53 -08001184 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001185 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01001186 /* buf.size==0 means we failed to get a buffer and were
1187 * already subscribed to a wait list to get a buffer.
1188 */
William Lallemand77c11972017-10-31 20:43:01 +01001189 goto out;
1190 }
1191
Willy Tarreauefef3232018-12-16 00:37:45 +01001192 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
William Lallemand77c11972017-10-31 20:43:01 +01001193 appctx->st0 = HTTP_CACHE_END;
1194
1195 /* buffer are aligned there, should be fine */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001196 if (appctx->st0 == HTTP_CACHE_HEADER || appctx->st0 == HTTP_CACHE_INIT) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001197 int len = first->len - *sent - sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001198 if (len > 0) {
1199 int ret;
1200
1201 ret = cache_channel_row_data_get(appctx, len);
1202 if (ret == -1)
1203 appctx->st0 = HTTP_CACHE_END;
1204 else
1205 *sent += ret;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +02001206 if (appctx->st0 == HTTP_CACHE_INIT && *sent > cache_ptr->eoh &&
1207 cache_channel_append_age_header(cache_ptr, res))
1208 appctx->st0 = HTTP_CACHE_HEADER;
Christopher Faulet61123912019-01-02 14:10:01 +01001209 else if (ret == len) {
1210 *sent = 0;
1211 appctx->st0 = HTTP_CACHE_FWD;
1212 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001213 }
1214 else {
1215 *sent = 0;
1216 appctx->st0 = HTTP_CACHE_FWD;
William Lallemand77c11972017-10-31 20:43:01 +01001217 }
William Lallemand77c11972017-10-31 20:43:01 +01001218 }
1219
1220 if (appctx->st0 == HTTP_CACHE_FWD) {
1221 /* eat the whole request */
Willy Tarreau178b9872018-06-19 07:13:36 +02001222 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 +01001223 res->flags |= CF_READ_NULL;
1224 si_shutr(si);
1225 }
1226
1227 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
1228 si_shutw(si);
1229out:
1230 ;
1231}
1232
Christopher Faulet95220e22018-12-07 17:34:39 +01001233static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err)
William Lallemand41db4602017-10-30 11:15:51 +01001234{
1235 struct flt_conf *fconf;
Christopher Faulet95220e22018-12-07 17:34:39 +01001236 struct cache_flt_conf *cconf = NULL;
William Lallemand41db4602017-10-30 11:15:51 +01001237
Christopher Faulet95220e22018-12-07 17:34:39 +01001238 if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001239 memprintf(err, "expects a cache name");
Christopher Faulet95220e22018-12-07 17:34:39 +01001240 goto err;
William Lallemand41db4602017-10-30 11:15:51 +01001241 }
1242
1243 /* check if a cache filter was already registered with this cache
1244 * name, if that's the case, must use it. */
1245 list_for_each_entry(fconf, &proxy->filter_configs, list) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001246 if (fconf->id == cache_store_flt_id) {
1247 cconf = fconf->conf;
1248 if (cconf && !strcmp((char *)cconf->c.name, name)) {
1249 rule->arg.act.p[0] = cconf;
1250 return 1;
1251 }
William Lallemand41db4602017-10-30 11:15:51 +01001252 }
1253 }
1254
Christopher Faulet95220e22018-12-07 17:34:39 +01001255 /* Create the filter cache config */
1256 cconf = calloc(1, sizeof(*cconf));
1257 if (!cconf) {
1258 memprintf(err, "out of memory\n");
1259 goto err;
1260 }
Christopher Faulet99a17a22018-12-11 09:18:27 +01001261 cconf->flags = CACHE_FLT_F_IMPLICIT_DECL;
Christopher Faulet95220e22018-12-07 17:34:39 +01001262 cconf->c.name = strdup(name);
1263 if (!cconf->c.name) {
1264 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001265 goto err;
1266 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001267
William Lallemand41db4602017-10-30 11:15:51 +01001268 /* register a filter to fill the cache buffer */
1269 fconf = calloc(1, sizeof(*fconf));
1270 if (!fconf) {
Christopher Faulet95220e22018-12-07 17:34:39 +01001271 memprintf(err, "out of memory\n");
William Lallemand41db4602017-10-30 11:15:51 +01001272 goto err;
1273 }
Christopher Faulet95220e22018-12-07 17:34:39 +01001274 fconf->id = cache_store_flt_id;
1275 fconf->conf = cconf;
William Lallemand41db4602017-10-30 11:15:51 +01001276 fconf->ops = &cache_ops;
1277 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
1278
Christopher Faulet95220e22018-12-07 17:34:39 +01001279 rule->arg.act.p[0] = cconf;
1280 return 1;
William Lallemand41db4602017-10-30 11:15:51 +01001281
Christopher Faulet95220e22018-12-07 17:34:39 +01001282 err:
1283 free(cconf);
1284 return 0;
1285}
1286
1287enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
1288 struct act_rule *rule, char **err)
1289{
1290 rule->action = ACT_CUSTOM;
1291 rule->action_ptr = http_action_store_cache;
1292
1293 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
1294 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001295
Christopher Faulet95220e22018-12-07 17:34:39 +01001296 (*orig_arg)++;
1297 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001298}
1299
William Lallemandf528fff2017-11-23 19:43:17 +01001300/* This produces a sha1 hash of the concatenation of the first
1301 * occurrence of the Host header followed by the path component if it
1302 * begins with a slash ('/'). */
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001303int sha1_hosturi(struct stream *s)
William Lallemandf528fff2017-11-23 19:43:17 +01001304{
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001305 struct http_txn *txn = s->txn;
William Lallemandf528fff2017-11-23 19:43:17 +01001306 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001307 struct buffer *trash;
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001308
William Lallemandf528fff2017-11-23 19:43:17 +01001309 trash = get_trash_chunk();
1310
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001311 if (IS_HTX_STRM(s)) {
1312 struct htx *htx = htxbuf(&s->req.buf);
1313 struct htx_sl *sl;
1314 struct http_hdr_ctx ctx;
1315 struct ist path;
William Lallemandf528fff2017-11-23 19:43:17 +01001316
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001317 ctx.blk = NULL;
1318 if (!http_find_header(htx, ist("Host"), &ctx, 0))
1319 return 0;
1320 chunk_memcat(trash, ctx.value.ptr, ctx.value.len);
1321
1322 sl = http_find_stline(htx);
1323 path = http_get_path(htx_sl_req_uri(sl));
1324 if (!path.ptr)
1325 return 0;
1326 chunk_memcat(trash, path.ptr, path.len);
1327 }
1328 else {
1329 struct hdr_ctx ctx;
1330 char *path;
1331 char *end;
1332
1333 /* retrive the host */
1334 ctx.idx = 0;
1335 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx))
1336 return 0;
1337 chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen);
1338
1339 /* now retrieve the path */
1340 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1341 path = http_txn_get_path(txn);
1342 if (!path)
1343 return 0;
1344 chunk_strncat(trash, path, end - path);
1345 }
William Lallemandf528fff2017-11-23 19:43:17 +01001346
1347 /* hash everything */
1348 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001349 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +01001350 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
1351
1352 return 1;
1353}
1354
William Lallemand41db4602017-10-30 11:15:51 +01001355enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
1356 struct session *sess, struct stream *s, int flags)
1357{
William Lallemand77c11972017-10-31 20:43:01 +01001358
William Lallemand77c11972017-10-31 20:43:01 +01001359 struct cache_entry *res;
Christopher Faulet95220e22018-12-07 17:34:39 +01001360 struct cache_flt_conf *cconf = rule->arg.act.p[0];
1361 struct cache *cache = cconf->c.cache;
William Lallemand77c11972017-10-31 20:43:01 +01001362
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001363 if (IS_HTX_STRM(s))
1364 htx_check_request_for_cacheability(s, &s->req);
1365 else
1366 check_request_for_cacheability(s, &s->req);
1367
Willy Tarreau504455c2017-12-22 17:47:35 +01001368 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
1369 return ACT_RET_CONT;
1370
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001371 if (!sha1_hosturi(s))
Willy Tarreau7704b1e2017-12-22 16:32:43 +01001372 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +01001373
Willy Tarreau504455c2017-12-22 17:47:35 +01001374 if (s->txn->flags & TX_CACHE_IGNORE)
1375 return ACT_RET_CONT;
1376
Willy Tarreaua1214a52018-12-14 14:00:25 +01001377 if (px == strm_fe(s))
1378 HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1);
1379 else
1380 HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1);
1381
William Lallemanda400a3a2017-11-20 19:13:12 +01001382 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +01001383 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +01001384 if (res) {
1385 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +01001386 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
1387 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +01001388 s->target = &http_cache_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001389 if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) {
William Lallemand77c11972017-10-31 20:43:01 +01001390 appctx->st0 = HTTP_CACHE_INIT;
1391 appctx->rule = rule;
1392 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +02001393 appctx->ctx.cache.next = NULL;
1394 appctx->ctx.cache.sent = 0;
Willy Tarreaua1214a52018-12-14 14:00:25 +01001395
1396 if (px == strm_fe(s))
1397 HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1);
1398 else
1399 HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1);
Olivier Houchardfccf8402017-11-01 14:04:02 +01001400 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +01001401 } else {
William Lallemand55e76742017-11-21 20:01:28 +01001402 shctx_lock(shctx_ptr(cache));
1403 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
1404 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001405 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +01001406 }
1407 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001408 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +01001409 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +01001410}
1411
1412
1413enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
1414 struct act_rule *rule, char **err)
1415{
William Lallemand41db4602017-10-30 11:15:51 +01001416 rule->action = ACT_CUSTOM;
1417 rule->action_ptr = http_action_req_cache_use;
1418
Christopher Faulet95220e22018-12-07 17:34:39 +01001419 if (!parse_cache_rule(proxy, args[*orig_arg], rule, err))
William Lallemand41db4602017-10-30 11:15:51 +01001420 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +01001421
1422 (*orig_arg)++;
1423 return ACT_RET_PRS_OK;
William Lallemand41db4602017-10-30 11:15:51 +01001424}
1425
1426int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
1427{
1428 int err_code = 0;
1429
1430 if (strcmp(args[0], "cache") == 0) { /* new cache section */
1431
1432 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001433 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
1434 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001435 err_code |= ERR_ALERT | ERR_ABORT;
1436 goto out;
1437 }
1438
1439 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1440 err_code |= ERR_ABORT;
1441 goto out;
1442 }
1443
1444 if (tmp_cache_config == NULL) {
1445 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
1446 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001447 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +01001448 err_code |= ERR_ALERT | ERR_ABORT;
1449 goto out;
1450 }
1451
1452 strlcpy2(tmp_cache_config->id, args[1], 33);
1453 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001454 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
1455 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001456 err_code |= ERR_WARN;
1457 }
William Lallemand49b44532017-11-24 18:53:43 +01001458 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +01001459 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001460 tmp_cache_config->maxobjsz = 0;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001461 tmp_cache_config->flags = 0;
William Lallemand41db4602017-10-30 11:15:51 +01001462 }
1463 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001464 unsigned long int maxsize;
1465 char *err;
William Lallemand41db4602017-10-30 11:15:51 +01001466
1467 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1468 err_code |= ERR_ABORT;
1469 goto out;
1470 }
1471
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001472 maxsize = strtoul(args[1], &err, 10);
1473 if (err == args[1] || *err != '\0') {
1474 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
1475 file, linenum, args[1]);
1476 err_code |= ERR_ABORT;
1477 goto out;
1478 }
1479
1480 if (maxsize > (UINT_MAX >> 20)) {
1481 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
1482 file, linenum, args[1], UINT_MAX >> 20);
1483 err_code |= ERR_ABORT;
1484 goto out;
1485 }
1486
William Lallemand41db4602017-10-30 11:15:51 +01001487 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +02001488 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +01001489 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +01001490 } else if (strcmp(args[0], "max-age") == 0) {
1491 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1492 err_code |= ERR_ABORT;
1493 goto out;
1494 }
1495
1496 if (!*args[1]) {
1497 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
1498 file, linenum, args[0]);
1499 err_code |= ERR_WARN;
1500 }
1501
1502 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001503 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001504 unsigned int maxobjsz;
1505 char *err;
1506
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001507 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
1508 err_code |= ERR_ABORT;
1509 goto out;
1510 }
1511
1512 if (!*args[1]) {
1513 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
1514 file, linenum, args[0]);
1515 err_code |= ERR_WARN;
1516 }
1517
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001518 maxobjsz = strtoul(args[1], &err, 10);
1519 if (err == args[1] || *err != '\0') {
1520 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
1521 file, linenum, args[1]);
1522 err_code |= ERR_ABORT;
1523 goto out;
1524 }
1525 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001526 }
1527 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001528 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001529 err_code |= ERR_ALERT | ERR_FATAL;
1530 goto out;
1531 }
1532out:
1533 return err_code;
1534}
1535
1536/* once the cache section is parsed */
1537
1538int cfg_post_parse_section_cache()
1539{
1540 struct shared_context *shctx;
1541 int err_code = 0;
1542 int ret_shctx;
1543
1544 if (tmp_cache_config) {
1545 struct cache *cache;
1546
1547 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001548 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +01001549 err_code |= ERR_FATAL | ERR_ALERT;
1550 goto out;
1551 }
1552
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001553 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001554 /* Default max. file size is a 256th of the cache size. */
1555 tmp_cache_config->maxobjsz =
1556 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +02001557 }
1558 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
1559 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
1560 err_code |= ERR_FATAL | ERR_ALERT;
1561 goto out;
1562 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001563
1564 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE,
1565 tmp_cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001566
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001567 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001568 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001569 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001570 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001571 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001572
1573 err_code |= ERR_FATAL | ERR_ALERT;
1574 goto out;
1575 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001576 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +01001577 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
1578 cache = (struct cache *)shctx->data;
1579 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001580 LIST_ADDQ(&caches, &cache->list);
1581 }
1582out:
1583 free(tmp_cache_config);
1584 tmp_cache_config = NULL;
1585 return err_code;
1586
1587}
1588
1589/*
1590 * Resolve the cache name to a pointer once the file is completely read.
1591 */
1592int cfg_cache_postparser()
1593{
William Lallemand41db4602017-10-30 11:15:51 +01001594 struct cache *cache;
William Lallemand41db4602017-10-30 11:15:51 +01001595 int err = 0;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001596
1597 /* Check if the cache is used by HTX and legacy HTTP proxies in same
1598 * time
1599 */
1600 list_for_each_entry(cache, &caches, list) {
1601 if ((cache->flags & (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) == (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) {
1602 ha_alert("Cache '%s': cannot be used by HTX and legacy HTTP proxies in same time.\n",
1603 cache->id);
1604 err++;
1605 }
1606 }
1607
William Lallemand41db4602017-10-30 11:15:51 +01001608 return err;
1609}
1610
1611
1612struct flt_ops cache_ops = {
1613 .init = cache_store_init,
Christopher Faulet95220e22018-12-07 17:34:39 +01001614 .check = cache_store_check,
1615 .deinit = cache_store_deinit,
William Lallemand41db4602017-10-30 11:15:51 +01001616
William Lallemand4da3f8a2017-10-31 14:33:34 +01001617 /* Handle channels activity */
1618 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001619 .channel_end_analyze = cache_store_chn_end_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001620
1621 /* Filter HTTP requests and responses */
1622 .http_headers = cache_store_http_headers,
Christopher Faulet54a8d5a2018-12-07 12:21:11 +01001623 .http_payload = cache_store_http_payload,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001624 .http_end = cache_store_http_end,
1625
1626 .http_forward_data = cache_store_http_forward_data,
1627
William Lallemand41db4602017-10-30 11:15:51 +01001628};
1629
Christopher Faulet99a17a22018-12-11 09:18:27 +01001630
1631
1632static int
1633parse_cache_flt(char **args, int *cur_arg, struct proxy *px,
1634 struct flt_conf *fconf, char **err, void *private)
1635{
1636 struct flt_conf *f, *back;
Willy Tarreaua73da1e2018-12-14 10:19:28 +01001637 struct cache_flt_conf *cconf = NULL;
Christopher Faulet99a17a22018-12-11 09:18:27 +01001638 char *name = NULL;
1639 int pos = *cur_arg;
1640
1641 /* Get the cache filter name*/
1642 if (!strcmp(args[pos], "cache")) {
1643 if (!*args[pos + 1]) {
1644 memprintf(err, "%s : expects an <id> argument", args[pos]);
1645 goto error;
1646 }
1647 name = strdup(args[pos + 1]);
1648 if (!name) {
1649 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
1650 goto error;
1651 }
1652 pos += 2;
1653 }
1654
1655 /* Check if an implicit filter with the same name already exists. If so,
1656 * we remove the implicit filter to use the explicit one. */
1657 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
1658 if (f->id != cache_store_flt_id)
1659 continue;
1660
1661 cconf = f->conf;
1662 if (strcmp(name, cconf->c.name)) {
1663 cconf = NULL;
1664 continue;
1665 }
1666
1667 if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) {
1668 cconf = NULL;
1669 memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'",
1670 px->id, name);
1671 return -1;
1672 }
1673
1674 /* Remove the implicit filter. <cconf> is kept for the explicit one */
1675 LIST_DEL(&f->list);
1676 free(f);
1677 free(name);
1678 break;
1679 }
1680
1681 /* No implicit cache filter found, create configuration for the explicit one */
1682 if (!cconf) {
1683 cconf = calloc(1, sizeof(*cconf));
1684 if (!cconf) {
1685 memprintf(err, "%s: out of memory", args[*cur_arg]);
1686 goto error;
1687 }
1688 cconf->c.name = name;
1689 }
1690
1691 cconf->flags = 0;
1692 fconf->id = cache_store_flt_id;
1693 fconf->conf = cconf;
1694 fconf->ops = &cache_ops;
1695
1696 *cur_arg = pos;
1697 return 0;
1698
1699 error:
1700 free(name);
1701 free(cconf);
1702 return -1;
1703}
1704
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001705static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001706{
1707 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1708 return 1;
1709
1710 return 0;
1711}
1712
1713static int cli_io_handler_show_cache(struct appctx *appctx)
1714{
1715 struct cache* cache = appctx->ctx.cli.p0;
1716 struct stream_interface *si = appctx->owner;
1717
William Lallemand1f49a362017-11-21 20:01:26 +01001718 if (cache == NULL) {
1719 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1720 }
1721
1722 list_for_each_entry_from(cache, &caches, list) {
1723 struct eb32_node *node = NULL;
1724 unsigned int next_key;
1725 struct cache_entry *entry;
1726
William Lallemand1f49a362017-11-21 20:01:26 +01001727 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001728 if (!next_key) {
1729 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1730 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001731 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001732 return 0;
1733 }
1734 }
William Lallemand1f49a362017-11-21 20:01:26 +01001735
1736 appctx->ctx.cli.p0 = cache;
1737
1738 while (1) {
1739
1740 shctx_lock(shctx_ptr(cache));
1741 node = eb32_lookup_ge(&cache->entries, next_key);
1742 if (!node) {
1743 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001744 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001745 break;
1746 }
1747
1748 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001749 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 +01001750
1751 next_key = node->key + 1;
1752 appctx->ctx.cli.i0 = next_key;
1753
1754 shctx_unlock(shctx_ptr(cache));
1755
1756 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001757 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001758 return 0;
1759 }
1760 }
1761
1762 }
1763
1764 return 1;
1765
1766}
1767
Christopher Faulet99a17a22018-12-11 09:18:27 +01001768/* Declare the filter parser for "cache" keyword */
1769static struct flt_kw_list filter_kws = { "CACHE", { }, {
1770 { "cache", parse_cache_flt, NULL },
1771 { NULL, NULL, NULL },
1772 }
1773};
1774
1775INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1776
William Lallemand1f49a362017-11-21 20:01:26 +01001777static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001778 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1779 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001780}};
1781
Willy Tarreau0108d902018-11-25 19:14:37 +01001782INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001783
William Lallemand41db4602017-10-30 11:15:51 +01001784static struct action_kw_list http_res_actions = {
1785 .kw = {
1786 { "cache-store", parse_cache_store },
1787 { NULL, NULL }
1788 }
1789};
1790
Willy Tarreau0108d902018-11-25 19:14:37 +01001791INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1792
William Lallemand41db4602017-10-30 11:15:51 +01001793static struct action_kw_list http_req_actions = {
1794 .kw = {
1795 { "cache-use", parse_cache_use },
1796 { NULL, NULL }
1797 }
1798};
1799
Willy Tarreau0108d902018-11-25 19:14:37 +01001800INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1801
William Lallemand41db4602017-10-30 11:15:51 +01001802struct applet http_cache_applet = {
1803 .obj_type = OBJ_TYPE_APPLET,
1804 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001805 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001806 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001807};
1808
Willy Tarreaue6552512018-11-26 11:33:13 +01001809/* config parsers for this section */
1810REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
1811REGISTER_CONFIG_POSTPARSER("cache", cfg_cache_postparser);