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.
(cherry picked from commit 5f2c49f5eedb55fd757c2e4812df840bcf2dd9e9)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
diff --git a/src/cache.c b/src/cache.c
index 3504fba..d46a8bb 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -578,36 +578,66 @@
*/
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;
+
+ if (IS_HTX_STRM(s)) {
+ /* HTX mode */
+ struct htx *htx = htxbuf(&s->res.buf);
+ struct http_hdr_ctx ctx = { .blk = NULL };
- ctx.idx = 0;
+ while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
+ char *value;
- /* 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(ctx.value.ptr, ctx.value.len, "s-maxage", 8);
+ if (value) {
+ struct buffer *chk = get_trash_chunk();
- value = directive_value(directive, ctx.vlen, "s-maxage", 8);
- if (value) {
- struct buffer *chk = get_trash_chunk();
+ chunk_strncat(chk, value, ctx.value.len - 8 + 1);
+ chunk_strncat(chk, "", 1);
+ maxage = atoi(chk->area);
+ }
- chunk_strncat(chk, value, ctx.vlen - 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;
+
+ ctx.idx = 0;
- value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
- if (value) {
- struct buffer *chk = get_trash_chunk();
+ /* 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);
+ chunk_strncat(chk, value, ctx.vlen - 7 + 1);
+ chunk_strncat(chk, "", 1);
+ smaxage = atoi(chk->area);
+ }
}
}