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