William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1 | /* |
| 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 Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 13 | #include <eb32tree.h> |
| 14 | |
| 15 | #include <proto/channel.h> |
| 16 | #include <proto/proxy.h> |
| 17 | #include <proto/hdr_idx.h> |
| 18 | #include <proto/filters.h> |
| 19 | #include <proto/proto_http.h> |
| 20 | #include <proto/log.h> |
| 21 | #include <proto/stream.h> |
| 22 | #include <proto/stream_interface.h> |
| 23 | #include <proto/shctx.h> |
| 24 | |
| 25 | #include <types/action.h> |
| 26 | #include <types/filters.h> |
| 27 | #include <types/proxy.h> |
| 28 | #include <types/shctx.h> |
| 29 | |
| 30 | #include <common/cfgparse.h> |
| 31 | #include <common/hash.h> |
| 32 | |
| 33 | /* flt_cache_store */ |
| 34 | |
| 35 | static const char *cache_store_flt_id = "cache store filter"; |
| 36 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 37 | static struct pool_head *pool2_cache_st = NULL; |
| 38 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 39 | struct applet http_cache_applet; |
| 40 | |
| 41 | struct flt_ops cache_ops; |
| 42 | |
| 43 | struct cache { |
| 44 | char id[33]; /* cache name */ |
| 45 | unsigned int maxage; /* max-age */ |
| 46 | unsigned int maxblocks; |
| 47 | struct list list; /* cache linked list */ |
| 48 | struct eb_root entries; /* head of cache entries based on keys */ |
| 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * cache ctx for filters |
| 53 | */ |
| 54 | struct cache_st { |
| 55 | int hdrs_len; |
| 56 | struct shared_block *first_block; |
| 57 | }; |
| 58 | |
| 59 | struct cache_entry { |
| 60 | unsigned int latest_validation; /* latest validation date */ |
| 61 | unsigned int expire; /* expiration date */ |
| 62 | struct eb32_node eb; /* ebtree node used to hold the cache object */ |
| 63 | unsigned char data[0]; |
| 64 | }; |
| 65 | |
| 66 | #define CACHE_BLOCKSIZE 1024 |
| 67 | |
| 68 | static struct list caches = LIST_HEAD_INIT(caches); |
| 69 | static struct cache *tmp_cache_config = NULL; |
| 70 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 71 | struct cache_entry *entry_exist(struct cache *cache, struct cache_entry *new_entry) |
| 72 | { |
| 73 | struct eb32_node *node; |
| 74 | struct cache_entry *entry; |
| 75 | |
| 76 | node = eb32_lookup(&cache->entries, new_entry->eb.key); |
| 77 | if (!node) |
| 78 | return NULL; |
| 79 | |
| 80 | entry = eb32_entry(node, struct cache_entry, eb); |
| 81 | if (entry->expire > now.tv_sec) |
| 82 | return entry; |
| 83 | else |
| 84 | eb32_delete(node); |
| 85 | return NULL; |
| 86 | |
| 87 | } |
| 88 | |
| 89 | static inline struct shared_context *shctx_ptr(struct cache *cache) |
| 90 | { |
| 91 | return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data); |
| 92 | } |
| 93 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 94 | static inline struct shared_block *block_ptr(struct cache_entry *entry) |
| 95 | { |
| 96 | return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 101 | static int |
| 102 | cache_store_init(struct proxy *px, struct flt_conf *f1conf) |
| 103 | { |
| 104 | return 0; |
| 105 | } |
| 106 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 107 | static int |
| 108 | cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 109 | { |
| 110 | if (!(chn->flags & CF_ISRESP)) |
| 111 | return 1; |
| 112 | |
| 113 | if (filter->ctx == NULL) { |
| 114 | struct cache_st *st; |
| 115 | |
| 116 | st = pool_alloc_dirty(pool2_cache_st); |
| 117 | if (st == NULL) |
| 118 | return -1; |
| 119 | |
| 120 | st->hdrs_len = 0; |
| 121 | st->first_block = NULL; |
| 122 | filter->ctx = st; |
| 123 | } |
| 124 | |
| 125 | register_data_filter(s, chn, filter); |
| 126 | |
| 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | static int |
| 131 | cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 132 | { |
| 133 | struct cache_st *st = filter->ctx; |
| 134 | |
| 135 | /* end of headers, exclude the final \r\n allow to forward the final |
| 136 | * \r\n in the data filter */ |
| 137 | if (!(msg->chn->flags & CF_ISRESP) || !st) |
| 138 | return 1; |
| 139 | |
William Lallemand | 9d5f54d | 2017-11-14 14:39:22 +0100 | [diff] [blame] | 140 | st->hdrs_len = msg->sov; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 141 | |
| 142 | return 1; |
| 143 | } |
| 144 | |
| 145 | static int |
| 146 | cache_store_http_forward_data(struct stream *s, struct filter *filter, |
| 147 | struct http_msg *msg, unsigned int len) |
| 148 | { |
| 149 | struct cache_st *st = filter->ctx; |
| 150 | struct shared_context *shctx = shctx_ptr((struct cache *)filter->config->conf); |
| 151 | int ret; |
| 152 | |
| 153 | /* |
| 154 | * We need to skip the HTTP headers first, because we saved them in the |
| 155 | * http-response action. |
| 156 | */ |
| 157 | if (!(msg->chn->flags & CF_ISRESP) || !st) |
| 158 | return len; |
| 159 | |
| 160 | if (!len) { |
| 161 | /* Nothing to foward */ |
| 162 | ret = len; |
| 163 | } |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 164 | else if (st->hdrs_len >= len) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 165 | /* Forward part of headers */ |
| 166 | ret = len; |
| 167 | st->hdrs_len -= len; |
| 168 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 169 | else { |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 170 | /* Forward data */ |
Olivier Houchard | cd2867a | 2017-11-01 13:58:21 +0100 | [diff] [blame] | 171 | if (filter->ctx && st->first_block) { |
| 172 | /* disable buffering if too much data (never greater than a buffer size */ |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 173 | if (len - st->hdrs_len > global.tune.bufsize - global.tune.maxrewrite - st->first_block->len) { |
William Lallemand | e1533f5 | 2017-11-14 14:39:24 +0100 | [diff] [blame] | 174 | disable_cache: |
Olivier Houchard | cd2867a | 2017-11-01 13:58:21 +0100 | [diff] [blame] | 175 | filter->ctx = NULL; /* disable cache */ |
| 176 | shctx_lock(shctx); |
| 177 | shctx_row_dec_hot(shctx, st->first_block); |
| 178 | shctx_unlock(shctx); |
| 179 | pool_free2(pool2_cache_st, st); |
Olivier Houchard | cd2867a | 2017-11-01 13:58:21 +0100 | [diff] [blame] | 180 | } else { |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 181 | /* Skip remaining headers to fill the cache */ |
| 182 | b_adv(msg->chn->buf, st->hdrs_len); |
| 183 | ret = shctx_row_data_append(shctx, |
| 184 | st->first_block, |
| 185 | (unsigned char *)bi_ptr(msg->chn->buf), |
| 186 | MIN(bi_contig_data(msg->chn->buf), len - st->hdrs_len)); |
| 187 | /* Rewind the buffer to forward all data */ |
| 188 | b_rew(msg->chn->buf, st->hdrs_len); |
William Lallemand | e1533f5 | 2017-11-14 14:39:24 +0100 | [diff] [blame] | 189 | if (ret) |
| 190 | goto disable_cache; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 191 | } |
| 192 | } |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 193 | ret = len; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | if ((ret != len) || |
| 197 | (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret)) |
| 198 | task_wakeup(s->task, TASK_WOKEN_MSG); |
| 199 | |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | static int |
| 204 | cache_store_http_end(struct stream *s, struct filter *filter, |
| 205 | struct http_msg *msg) |
| 206 | { |
| 207 | struct cache_st *st = filter->ctx; |
| 208 | struct cache *cache = filter->config->conf; |
| 209 | struct shared_context *shctx = shctx_ptr(cache); |
| 210 | struct cache_entry *object; |
| 211 | |
| 212 | if (!(msg->chn->flags & CF_ISRESP)) |
| 213 | return 1; |
| 214 | |
| 215 | if (st && st->first_block) { |
| 216 | |
| 217 | object = (struct cache_entry *)st->first_block->data; |
| 218 | |
| 219 | /* does not need to test if the insertion worked, if it |
| 220 | * doesn't, the blocks will be reused anyway */ |
| 221 | |
| 222 | shctx_lock(shctx); |
| 223 | eb32_insert(&cache->entries, &object->eb); |
| 224 | shctx_unlock(shctx); |
| 225 | |
| 226 | /* remove from the hotlist */ |
| 227 | shctx_lock(shctx); |
| 228 | shctx_row_dec_hot(shctx, st->first_block); |
| 229 | shctx_unlock(shctx); |
| 230 | |
| 231 | } |
| 232 | if (st) { |
| 233 | pool_free2(pool2_cache_st, st); |
| 234 | filter->ctx = NULL; |
| 235 | } |
| 236 | |
| 237 | return 1; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * This intends to be used when checking HTTP headers for some |
| 242 | * word=value directive. Return a pointer to the first character of value, if |
| 243 | * the word was not found or if there wasn't any value assigned ot it return NULL |
| 244 | */ |
| 245 | char *directive_value(const char *sample, int slen, const char *word, int wlen) |
| 246 | { |
| 247 | int st = 0; |
| 248 | |
| 249 | if (slen < wlen) |
| 250 | return 0; |
| 251 | |
| 252 | while (wlen) { |
| 253 | char c = *sample ^ *word; |
| 254 | if (c && c != ('A' ^ 'a')) |
| 255 | return NULL; |
| 256 | sample++; |
| 257 | word++; |
| 258 | slen--; |
| 259 | wlen--; |
| 260 | } |
| 261 | |
| 262 | while (slen) { |
| 263 | if (st == 0) { |
| 264 | if (*sample != '=') |
| 265 | return NULL; |
| 266 | sample++; |
| 267 | slen--; |
| 268 | st = 1; |
| 269 | continue; |
| 270 | } else { |
| 271 | return (char *)sample; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | return NULL; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Return the maxage in seconds of an HTTP response. |
| 280 | * Compute the maxage using either: |
| 281 | * - the assigned max-age of the cache |
| 282 | * - the s-maxage directive |
| 283 | * - the max-age directive |
| 284 | * - (Expires - Data) headers |
| 285 | * - the default-max-age of the cache |
| 286 | * |
| 287 | */ |
| 288 | int http_calc_maxage(struct stream *s) |
| 289 | { |
| 290 | struct http_txn *txn = s->txn; |
| 291 | struct hdr_ctx ctx; |
| 292 | |
| 293 | int smaxage = -1; |
| 294 | int maxage = -1; |
| 295 | |
| 296 | |
| 297 | /* TODO: forced maxage configuration */ |
| 298 | |
| 299 | ctx.idx = 0; |
| 300 | |
| 301 | /* loop on the Cache-Control values */ |
| 302 | while (http_find_header2("Cache-Control", 13, s->res.buf->p, &txn->hdr_idx, &ctx)) { |
| 303 | char *directive = ctx.line + ctx.val; |
| 304 | char *value; |
| 305 | |
| 306 | value = directive_value(directive, ctx.vlen, "s-maxage", 8); |
| 307 | if (value) { |
| 308 | struct chunk *chk = get_trash_chunk(); |
| 309 | |
| 310 | chunk_strncat(chk, value, ctx.vlen - 8 + 1); |
| 311 | chunk_strncat(chk, "", 1); |
| 312 | maxage = atoi(chk->str); |
| 313 | } |
| 314 | |
| 315 | value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7); |
| 316 | if (value) { |
| 317 | struct chunk *chk = get_trash_chunk(); |
| 318 | |
| 319 | chunk_strncat(chk, value, ctx.vlen - 7 + 1); |
| 320 | chunk_strncat(chk, "", 1); |
| 321 | smaxage = atoi(chk->str); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /* TODO: Expires - Data */ |
| 326 | |
| 327 | |
| 328 | if (smaxage > 0) |
| 329 | return smaxage; |
| 330 | |
| 331 | if (maxage > 0) |
| 332 | return maxage; |
| 333 | |
| 334 | /* TODO: return default value */ |
| 335 | |
| 336 | return 60; |
| 337 | |
| 338 | } |
| 339 | |
| 340 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 341 | /* |
| 342 | * This fonction will store the headers of the response in a buffer and then |
| 343 | * register a filter to store the data |
| 344 | */ |
| 345 | enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px, |
| 346 | struct session *sess, struct stream *s, int flags) |
| 347 | { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 348 | struct http_txn *txn = s->txn; |
| 349 | struct http_msg *msg = &txn->rsp; |
| 350 | struct filter *filter; |
| 351 | struct hdr_ctx ctx; |
| 352 | struct shared_block *first = NULL; |
| 353 | struct shared_context *shctx = shctx_ptr((struct cache *)rule->arg.act.p[0]); |
| 354 | struct cache_entry *object; |
| 355 | |
| 356 | |
| 357 | /* Don't cache if the response came from a cache */ |
| 358 | if ((obj_type(s->target) == OBJ_TYPE_APPLET) && |
| 359 | s->target == &http_cache_applet.obj_type) { |
| 360 | goto out; |
| 361 | } |
| 362 | |
| 363 | /* cache only HTTP/1.1 */ |
| 364 | if (!(txn->req.flags & HTTP_MSGF_VER_11)) |
| 365 | goto out; |
| 366 | |
William Lallemand | 18f133a | 2017-11-08 11:25:15 +0100 | [diff] [blame] | 367 | /* does not cache if Content-Length unknown */ |
| 368 | if (!(msg->flags & HTTP_MSGF_CNT_LEN)) |
| 369 | goto out; |
| 370 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 371 | /* cache only GET method */ |
| 372 | if (txn->meth != HTTP_METH_GET) |
| 373 | goto out; |
| 374 | |
| 375 | /* cache only 200 status code */ |
| 376 | if (txn->status != 200) |
| 377 | goto out; |
| 378 | |
| 379 | /* Does not manage Vary at the moment. We will need a secondary key later for that */ |
| 380 | ctx.idx = 0; |
| 381 | if (http_find_header2("Vary", 4, txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) |
| 382 | goto out; |
| 383 | |
| 384 | /* we need to put this flag before using check_response_for_cacheability */ |
| 385 | txn->flags |= TX_CACHEABLE; |
| 386 | |
| 387 | if (txn->status != 101) |
| 388 | check_response_for_cacheability(s, &s->res); |
| 389 | |
| 390 | if (!(txn->flags & TX_CACHEABLE)) |
| 391 | goto out; |
| 392 | |
William Lallemand | 9d5f54d | 2017-11-14 14:39:22 +0100 | [diff] [blame] | 393 | if ((msg->sov + msg->body_len) > (global.tune.bufsize - global.tune.maxrewrite)) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 394 | goto out; |
| 395 | |
| 396 | shctx_lock(shctx); |
| 397 | |
William Lallemand | 9d5f54d | 2017-11-14 14:39:22 +0100 | [diff] [blame] | 398 | first = shctx_row_reserve_hot(shctx, sizeof(struct cache_entry) + msg->sov + msg->body_len); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 399 | if (!first) { |
| 400 | shctx_unlock(shctx); |
| 401 | goto out; |
| 402 | } |
| 403 | shctx_unlock(shctx); |
| 404 | |
| 405 | /* reserve space for the cache_entry structure */ |
| 406 | first->len = sizeof(struct cache_entry); |
| 407 | |
| 408 | /* cache the headers in a http action because it allows to chose what |
| 409 | * to cache, for example you might want to cache a response before |
| 410 | * modifying some HTTP headers, or on the contrary after modifying |
| 411 | * those headers. |
| 412 | */ |
| 413 | |
| 414 | /* does not need to be locked because it's in the "hot" list, |
| 415 | * copy the headers */ |
William Lallemand | 9d5f54d | 2017-11-14 14:39:22 +0100 | [diff] [blame] | 416 | if (shctx_row_data_append(shctx, first, (unsigned char *)s->res.buf->p, msg->sov) < 0) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 417 | goto out; |
| 418 | |
| 419 | /* register the buffer in the filter ctx for filling it with data*/ |
| 420 | if (!LIST_ISEMPTY(&s->strm_flt.filters)) { |
| 421 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 422 | if (filter->config->id == cache_store_flt_id && |
| 423 | filter->config->conf == rule->arg.act.p[0]) { |
| 424 | if (filter->ctx) { |
| 425 | struct cache_st *cache_ctx = filter->ctx; |
| 426 | |
| 427 | cache_ctx->first_block = first; |
| 428 | object = (struct cache_entry *)first->data; |
| 429 | |
| 430 | object->eb.key = hash_djb2(txn->uri, strlen(txn->uri)); |
| 431 | /* Insert the node later on caching success */ |
| 432 | |
| 433 | shctx_lock(shctx); |
| 434 | if (entry_exist((struct cache *)rule->arg.act.p[0], object)) { |
| 435 | shctx_unlock(shctx); |
| 436 | if (filter->ctx) { |
| 437 | pool_free2(pool2_cache_st, filter->ctx); |
| 438 | filter->ctx = NULL; |
| 439 | } |
| 440 | goto out; |
| 441 | } |
| 442 | shctx_unlock(shctx); |
| 443 | |
| 444 | /* store latest value and expiration time */ |
| 445 | object->latest_validation = now.tv_sec; |
| 446 | object->expire = now.tv_sec + http_calc_maxage(s); |
| 447 | |
| 448 | } |
| 449 | return ACT_RET_CONT; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | out: |
| 455 | /* if does not cache */ |
| 456 | if (first) { |
| 457 | shctx_lock(shctx); |
| 458 | shctx_row_dec_hot(shctx, first); |
| 459 | shctx_unlock(shctx); |
| 460 | } |
| 461 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 462 | return ACT_RET_CONT; |
| 463 | } |
| 464 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 465 | #define HTTP_CACHE_INIT 0 |
| 466 | #define HTTP_CACHE_FWD 1 |
| 467 | #define HTTP_CACHE_END 2 |
| 468 | |
| 469 | static void http_cache_io_handler(struct appctx *appctx) |
| 470 | { |
| 471 | struct stream_interface *si = appctx->owner; |
| 472 | struct channel *res = si_ic(si); |
| 473 | struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0]; |
| 474 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 475 | struct shared_context *shctx = shctx_ptr(cache); |
| 476 | struct shared_block *first = block_ptr(cache_ptr); |
| 477 | |
| 478 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 479 | goto out; |
| 480 | |
| 481 | /* Check if the input buffer is avalaible. */ |
| 482 | if (res->buf->size == 0) { |
| 483 | si_applet_cant_put(si); |
| 484 | goto out; |
| 485 | } |
| 486 | |
| 487 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
| 488 | appctx->st0 = HTTP_CACHE_END; |
| 489 | |
| 490 | /* buffer are aligned there, should be fine */ |
| 491 | if (appctx->st0 == HTTP_CACHE_INIT) { |
| 492 | int len = first->len - sizeof(struct cache_entry); |
| 493 | if ((shctx_row_data_get(shctx, first, (unsigned char *)bi_end(res->buf), sizeof(struct cache_entry), len)) != 0) { |
| 494 | fprintf(stderr, "cache error too big: %d\n", first->len - (int)sizeof(struct cache_entry)); |
| 495 | si_applet_cant_put(si); |
| 496 | goto out; |
| 497 | } |
| 498 | res->buf->i += len; |
| 499 | res->total += len; |
| 500 | appctx->st0 = HTTP_CACHE_FWD; |
| 501 | } |
| 502 | |
| 503 | if (appctx->st0 == HTTP_CACHE_FWD) { |
| 504 | /* eat the whole request */ |
| 505 | co_skip(si_oc(si), si_ob(si)->o); // NOTE: when disabled does not repport the correct status code |
| 506 | res->flags |= CF_READ_NULL; |
| 507 | si_shutr(si); |
| 508 | } |
| 509 | |
| 510 | if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) |
| 511 | si_shutw(si); |
| 512 | out: |
| 513 | ; |
| 514 | } |
| 515 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 516 | enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy, |
| 517 | struct act_rule *rule, char **err) |
| 518 | { |
| 519 | struct flt_conf *fconf; |
| 520 | int cur_arg = *orig_arg; |
| 521 | rule->action = ACT_CUSTOM; |
| 522 | rule->action_ptr = http_action_store_cache; |
| 523 | |
| 524 | if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 525 | memprintf(err, "expects a cache name"); |
| 526 | return ACT_RET_PRS_ERR; |
| 527 | } |
| 528 | |
| 529 | /* check if a cache filter was already registered with this cache |
| 530 | * name, if that's the case, must use it. */ |
| 531 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
| 532 | if (fconf->id == cache_store_flt_id && !strcmp((char *)fconf->conf, args[cur_arg])) { |
| 533 | rule->arg.act.p[0] = fconf->conf; |
| 534 | (*orig_arg)++; |
| 535 | /* filter already registered */ |
| 536 | return ACT_RET_PRS_OK; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | rule->arg.act.p[0] = strdup(args[cur_arg]); |
| 541 | if (!rule->arg.act.p[0]) { |
| 542 | Alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id); |
| 543 | err++; |
| 544 | goto err; |
| 545 | } |
| 546 | /* register a filter to fill the cache buffer */ |
| 547 | fconf = calloc(1, sizeof(*fconf)); |
| 548 | if (!fconf) { |
| 549 | Alert("config: %s '%s': out of memory\n", |
| 550 | proxy_type_str(proxy), proxy->id); |
| 551 | err++; |
| 552 | goto err; |
| 553 | } |
| 554 | fconf->id = cache_store_flt_id; |
| 555 | fconf->conf = rule->arg.act.p[0]; /* store the proxy name */ |
| 556 | fconf->ops = &cache_ops; |
| 557 | LIST_ADDQ(&proxy->filter_configs, &fconf->list); |
| 558 | |
| 559 | (*orig_arg)++; |
| 560 | |
| 561 | return ACT_RET_PRS_OK; |
| 562 | |
| 563 | err: |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 564 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | |
| 568 | enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px, |
| 569 | struct session *sess, struct stream *s, int flags) |
| 570 | { |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 571 | |
| 572 | struct cache_entry search_entry; |
| 573 | struct cache_entry *res; |
| 574 | |
| 575 | struct cache *cache = (struct cache *)rule->arg.act.p[0]; |
| 576 | |
| 577 | search_entry.eb.key = hash_djb2(s->txn->uri, strlen(s->txn->uri)); |
| 578 | res = entry_exist(cache, &search_entry); |
| 579 | if (res) { |
| 580 | struct appctx *appctx; |
| 581 | |
| 582 | s->target = &http_cache_applet.obj_type; |
| 583 | if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) { |
| 584 | appctx->st0 = HTTP_CACHE_INIT; |
| 585 | appctx->rule = rule; |
| 586 | appctx->ctx.cache.entry = res; |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 587 | return ACT_RET_CONT; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 588 | } else { |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 589 | return ACT_RET_YIELD; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 590 | } |
| 591 | } |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 592 | return ACT_RET_CONT; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | |
| 596 | enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy, |
| 597 | struct act_rule *rule, char **err) |
| 598 | { |
| 599 | int cur_arg = *orig_arg; |
| 600 | |
| 601 | rule->action = ACT_CUSTOM; |
| 602 | rule->action_ptr = http_action_req_cache_use; |
| 603 | |
| 604 | if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 605 | memprintf(err, "expects a cache name"); |
| 606 | return ACT_RET_PRS_ERR; |
| 607 | } |
| 608 | |
| 609 | rule->arg.act.p[0] = strdup(args[cur_arg]); |
| 610 | if (!rule->arg.act.p[0]) { |
| 611 | Alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id); |
| 612 | err++; |
| 613 | goto err; |
| 614 | } |
| 615 | |
| 616 | (*orig_arg)++; |
| 617 | return ACT_RET_PRS_OK; |
| 618 | |
| 619 | err: |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 620 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 621 | |
| 622 | } |
| 623 | |
| 624 | int cfg_parse_cache(const char *file, int linenum, char **args, int kwm) |
| 625 | { |
| 626 | int err_code = 0; |
| 627 | |
| 628 | if (strcmp(args[0], "cache") == 0) { /* new cache section */ |
| 629 | |
| 630 | if (!*args[1]) { |
| 631 | Alert("parsing [%s:%d] : '%s' expects an <id> argument\n", |
| 632 | file, linenum, args[0]); |
| 633 | err_code |= ERR_ALERT | ERR_ABORT; |
| 634 | goto out; |
| 635 | } |
| 636 | |
| 637 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 638 | err_code |= ERR_ABORT; |
| 639 | goto out; |
| 640 | } |
| 641 | |
| 642 | if (tmp_cache_config == NULL) { |
| 643 | tmp_cache_config = calloc(1, sizeof(*tmp_cache_config)); |
| 644 | if (!tmp_cache_config) { |
| 645 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 646 | err_code |= ERR_ALERT | ERR_ABORT; |
| 647 | goto out; |
| 648 | } |
| 649 | |
| 650 | strlcpy2(tmp_cache_config->id, args[1], 33); |
| 651 | if (strlen(args[1]) > 32) { |
| 652 | Warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n", |
| 653 | file, linenum, tmp_cache_config->id); |
| 654 | err_code |= ERR_WARN; |
| 655 | } |
| 656 | |
| 657 | tmp_cache_config->maxblocks = 0; |
| 658 | } |
| 659 | } else if (strcmp(args[0], "total-max-size") == 0) { |
| 660 | int maxsize; |
| 661 | |
| 662 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 663 | err_code |= ERR_ABORT; |
| 664 | goto out; |
| 665 | } |
| 666 | |
| 667 | /* size in megabytes */ |
| 668 | maxsize = atoi(args[1]) * 1024 * 1024 / CACHE_BLOCKSIZE; |
| 669 | tmp_cache_config->maxblocks = maxsize; |
| 670 | |
| 671 | } else if (*args[0] != 0) { |
| 672 | Alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]); |
| 673 | err_code |= ERR_ALERT | ERR_FATAL; |
| 674 | goto out; |
| 675 | } |
| 676 | out: |
| 677 | return err_code; |
| 678 | } |
| 679 | |
| 680 | /* once the cache section is parsed */ |
| 681 | |
| 682 | int cfg_post_parse_section_cache() |
| 683 | { |
| 684 | struct shared_context *shctx; |
| 685 | int err_code = 0; |
| 686 | int ret_shctx; |
| 687 | |
| 688 | if (tmp_cache_config) { |
| 689 | struct cache *cache; |
| 690 | |
| 691 | if (tmp_cache_config->maxblocks <= 0) { |
| 692 | Alert("Size not specified for cache '%s'\n", tmp_cache_config->id); |
| 693 | err_code |= ERR_FATAL | ERR_ALERT; |
| 694 | goto out; |
| 695 | } |
| 696 | |
| 697 | ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE, sizeof(struct cache), 1); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 698 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 699 | if (ret_shctx < 0) { |
| 700 | if (ret_shctx == SHCTX_E_INIT_LOCK) |
| 701 | Alert("Unable to initialize the lock for the cache.\n"); |
| 702 | else |
| 703 | Alert("Unable to allocate cache.\n"); |
| 704 | |
| 705 | err_code |= ERR_FATAL | ERR_ALERT; |
| 706 | goto out; |
| 707 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 708 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 709 | memcpy(shctx->data, tmp_cache_config, sizeof(struct cache)); |
| 710 | cache = (struct cache *)shctx->data; |
| 711 | cache->entries = EB_ROOT_UNIQUE; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 712 | LIST_ADDQ(&caches, &cache->list); |
| 713 | } |
| 714 | out: |
| 715 | free(tmp_cache_config); |
| 716 | tmp_cache_config = NULL; |
| 717 | return err_code; |
| 718 | |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | * Resolve the cache name to a pointer once the file is completely read. |
| 723 | */ |
| 724 | int cfg_cache_postparser() |
| 725 | { |
| 726 | struct act_rule *hresrule, *hrqrule; |
| 727 | void *cache_ptr; |
| 728 | struct cache *cache; |
| 729 | struct proxy *curproxy = NULL; |
| 730 | int err = 0; |
| 731 | struct flt_conf *fconf; |
| 732 | |
| 733 | for (curproxy = proxy; curproxy; curproxy = curproxy->next) { |
| 734 | |
| 735 | /* resolve the http response cache name to a ptr in the action rule */ |
| 736 | list_for_each_entry(hresrule, &curproxy->http_res_rules, list) { |
| 737 | if (hresrule->action != ACT_CUSTOM || |
| 738 | hresrule->action_ptr != http_action_store_cache) |
| 739 | continue; |
| 740 | |
| 741 | cache_ptr = hresrule->arg.act.p[0]; |
| 742 | |
| 743 | list_for_each_entry(cache, &caches, list) { |
| 744 | if (!strcmp(cache->id, cache_ptr)) { |
| 745 | /* don't free there, it's still used in the filter conf */ |
| 746 | cache_ptr = cache; |
| 747 | break; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | if (cache_ptr == hresrule->arg.act.p[0]) { |
| 752 | Alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n", |
| 753 | curproxy->id, (char *)hresrule->arg.act.p[0]); |
| 754 | err++; |
| 755 | } |
| 756 | |
| 757 | hresrule->arg.act.p[0] = cache_ptr; |
| 758 | } |
| 759 | |
| 760 | /* resolve the http request cache name to a ptr in the action rule */ |
| 761 | list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) { |
| 762 | if (hrqrule->action != ACT_CUSTOM || |
| 763 | hrqrule->action_ptr != http_action_req_cache_use) |
| 764 | continue; |
| 765 | |
| 766 | cache_ptr = hrqrule->arg.act.p[0]; |
| 767 | |
| 768 | list_for_each_entry(cache, &caches, list) { |
| 769 | if (!strcmp(cache->id, cache_ptr)) { |
| 770 | free(cache_ptr); |
| 771 | cache_ptr = cache; |
| 772 | break; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | if (cache_ptr == hrqrule->arg.act.p[0]) { |
| 777 | Alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n", |
| 778 | curproxy->id, (char *)hrqrule->arg.act.p[0]); |
| 779 | err++; |
| 780 | } |
| 781 | |
| 782 | hrqrule->arg.act.p[0] = cache_ptr; |
| 783 | } |
| 784 | |
| 785 | /* resolve the cache name to a ptr in the filter config */ |
| 786 | list_for_each_entry(fconf, &curproxy->filter_configs, list) { |
| 787 | |
William Lallemand | 9c54c53 | 2017-11-02 16:38:42 +0100 | [diff] [blame] | 788 | if (fconf->id != cache_store_flt_id) |
| 789 | continue; |
| 790 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 791 | cache_ptr = fconf->conf; |
| 792 | |
| 793 | list_for_each_entry(cache, &caches, list) { |
| 794 | if (!strcmp(cache->id, cache_ptr)) { |
| 795 | /* there can be only one filter per cache, so we free it there */ |
| 796 | free(cache_ptr); |
| 797 | cache_ptr = cache; |
| 798 | break; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | if (cache_ptr == fconf->conf) { |
| 803 | Alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n", |
| 804 | curproxy->id, (char *)fconf->conf); |
| 805 | err++; |
| 806 | } |
| 807 | fconf->conf = cache_ptr; |
| 808 | } |
| 809 | } |
| 810 | return err; |
| 811 | } |
| 812 | |
| 813 | |
| 814 | struct flt_ops cache_ops = { |
| 815 | .init = cache_store_init, |
| 816 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 817 | /* Handle channels activity */ |
| 818 | .channel_start_analyze = cache_store_chn_start_analyze, |
| 819 | |
| 820 | /* Filter HTTP requests and responses */ |
| 821 | .http_headers = cache_store_http_headers, |
| 822 | .http_end = cache_store_http_end, |
| 823 | |
| 824 | .http_forward_data = cache_store_http_forward_data, |
| 825 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 826 | }; |
| 827 | |
| 828 | static struct action_kw_list http_res_actions = { |
| 829 | .kw = { |
| 830 | { "cache-store", parse_cache_store }, |
| 831 | { NULL, NULL } |
| 832 | } |
| 833 | }; |
| 834 | |
| 835 | static struct action_kw_list http_req_actions = { |
| 836 | .kw = { |
| 837 | { "cache-use", parse_cache_use }, |
| 838 | { NULL, NULL } |
| 839 | } |
| 840 | }; |
| 841 | |
| 842 | struct applet http_cache_applet = { |
| 843 | .obj_type = OBJ_TYPE_APPLET, |
| 844 | .name = "<CACHE>", /* used for logging */ |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 845 | .fct = http_cache_io_handler, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 846 | .release = NULL, |
| 847 | }; |
| 848 | |
| 849 | __attribute__((constructor)) |
| 850 | static void __cache_init(void) |
| 851 | { |
| 852 | cfg_register_section("cache", cfg_parse_cache, cfg_post_parse_section_cache); |
| 853 | cfg_register_postparser("cache", cfg_cache_postparser); |
| 854 | http_res_keywords_register(&http_res_actions); |
| 855 | http_req_keywords_register(&http_req_actions); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 856 | pool2_cache_st = create_pool("cache_st", sizeof(struct cache_st), MEM_F_SHARED); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 857 | } |
| 858 | |