From 5f2c49f5eedb55fd757c2e4812df840bcf2dd9e9 Mon Sep 17 00:00:00 2001 From: Christopher Faulet Date: Mon, 15 Jul 2019 20:49:46 +0200 Subject: [PATCH] BUG/MINOR: cache/htx: Make maxage calculation HTX aware The function http_calc_maxage() was not updated to be HTX aware. So the header "Cache-Control" on the response was never parsed to find "max-age" or "s-maxage" values. This patch must be backported to 2.0 and 1.9. --- src/cache.c | 70 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/src/cache.c b/src/cache.c index 3504fbaa4..d46a8bb34 100644 --- a/src/cache.c +++ b/src/cache.c @@ -578,36 +578,66 @@ char *directive_value(const char *sample, int slen, const char *word, int wlen) */ int http_calc_maxage(struct stream *s, struct cache *cache) { - struct http_txn *txn = s->txn; - struct hdr_ctx ctx; - int smaxage = -1; int maxage = -1; - ctx.idx = 0; + if (IS_HTX_STRM(s)) { + /* HTX mode */ + struct htx *htx = htxbuf(&s->res.buf); + struct http_hdr_ctx ctx = { .blk = NULL }; - /* loop on the Cache-Control values */ - while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) { - char *directive = ctx.line + ctx.val; - char *value; + while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { + char *value; - value = directive_value(directive, ctx.vlen, "s-maxage", 8); - if (value) { - struct buffer *chk = get_trash_chunk(); + value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8); + if (value) { + struct buffer *chk = get_trash_chunk(); - chunk_strncat(chk, value, ctx.vlen - 8 + 1); - chunk_strncat(chk, "", 1); - maxage = atoi(chk->area); + chunk_strncat(chk, value, ctx.value.len - 8 + 1); + chunk_strncat(chk, "", 1); + maxage = atoi(chk->area); + } + + value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7); + if (value) { + struct buffer *chk = get_trash_chunk(); + + chunk_strncat(chk, value, ctx.value.len - 7 + 1); + chunk_strncat(chk, "", 1); + smaxage = atoi(chk->area); + } } + } + else { + /* Legacy mode */ + struct http_txn *txn = s->txn; + struct hdr_ctx ctx; - value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7); - if (value) { - struct buffer *chk = get_trash_chunk(); + ctx.idx = 0; - chunk_strncat(chk, value, ctx.vlen - 7 + 1); - chunk_strncat(chk, "", 1); - smaxage = atoi(chk->area); + /* loop on the Cache-Control values */ + while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) { + char *directive = ctx.line + ctx.val; + char *value; + + value = directive_value(directive, ctx.vlen, "s-maxage", 8); + if (value) { + struct buffer *chk = get_trash_chunk(); + + chunk_strncat(chk, value, ctx.vlen - 8 + 1); + chunk_strncat(chk, "", 1); + maxage = atoi(chk->area); + } + + value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7); + if (value) { + struct buffer *chk = get_trash_chunk(); + + chunk_strncat(chk, value, ctx.vlen - 7 + 1); + chunk_strncat(chk, "", 1); + smaxage = atoi(chk->area); + } } }