]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: http-ana: let s-maxage override a null max-age whatever their order
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 21 Oct 2026 08:47:22 +0000 (08:47 +0000)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 27 Nov 2026 15:50:22 +0000 (16:50 +0100)
A response carrying "Cache-Control: s-maxage=60, max-age=0" is not stored
in the cache, while "Cache-Control: max-age=0, s-maxage=60" is. Both mean
that private caches must not reuse the response while shared caches like
haproxy's may keep it for 60 seconds (RFC 9111#5.2.2.10), and the order
of the directives does not matter. Servers emitting the directives in
the first order, or as two separate Cache-Control headers in this order,
do not get their responses cached, which only affects performance.

http_check_response_for_cacheability() iterates over the Cache-Control
values. A "max-age=0" sets has_null_maxage and an "s-maxage" resets it,
so a "max-age=0" found after the "s-maxage" sets it again and the
response is finally considered as not cacheable.

The s-maxage directive is now recorded in its own variable, and the null
max-age only makes the response non-cacheable in the absence of s-maxage.
Note that "s-maxage=0" is handled earlier and does not count as an
s-maxage here, so it still makes the response non-cacheable.

The reg-test reg-tests/cache/smaxage_override.vtc checks the two orders
and the case of two headers. It fails without the fix.

This was introduced by commit ca4fd7393 ("BUG/MINOR: cache: A 'max-age=0'
cache-control directive can be overriden by a s-maxage") in 2.9-dev2
which only handled the first order, so the fix may be backported to 3.0.

reg-tests/cache/smaxage_override.vtc [new file with mode: 0644]
src/http_ana.c

diff --git a/reg-tests/cache/smaxage_override.vtc b/reg-tests/cache/smaxage_override.vtc
new file mode 100644 (file)
index 0000000..9172a40
--- /dev/null
@@ -0,0 +1,83 @@
+varnishtest "s-maxage overrides a null max-age whatever their order"
+
+# RFC 9111#5.2.2.10: for a shared cache, s-maxage overrides max-age. A response
+# with "max-age=0" and a non-null "s-maxage" is thus cacheable, whatever the
+# order of the two directives.
+
+feature ignore_unknown_macro
+
+server s1 {
+    rxreq
+    expect req.url == "/a"
+    txresp -hdr "Cache-Control: max-age=0, s-maxage=60" -bodylen 10
+
+    rxreq
+    expect req.url == "/b"
+    txresp -hdr "Cache-Control: s-maxage=60, max-age=0" -bodylen 20
+
+    rxreq
+    expect req.url == "/c"
+    txresp -hdr "Cache-Control: s-maxage=60" -hdr "Cache-Control: max-age=0" -bodylen 30
+} -start
+
+haproxy h1 -conf {
+    global
+    .if feature(THREAD)
+        thread-groups 1
+    .endif
+
+    defaults
+        mode http
+        timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
+        timeout client  "${HAPROXY_TEST_TIMEOUT-5s}"
+        timeout server  "${HAPROXY_TEST_TIMEOUT-5s}"
+
+    frontend fe
+        bind "fd@${fe}"
+        http-request cache-use c1
+        http-response cache-store c1
+        http-response set-header X-Cache-Hit %[res.cache_hit]
+        default_backend be
+
+    backend be
+        server s1 ${s1_addr}:${s1_port}
+
+    cache c1
+        total-max-size 4
+} -start
+
+client c1 -connect ${h1_fe_sock} {
+    txreq -url "/a"
+    rxresp
+    expect resp.status == 200
+    expect resp.http.X-Cache-Hit == 0
+
+    txreq -url "/b"
+    rxresp
+    expect resp.status == 200
+    expect resp.http.X-Cache-Hit == 0
+
+    txreq -url "/c"
+    rxresp
+    expect resp.status == 200
+    expect resp.http.X-Cache-Hit == 0
+
+    # all of them must have been cached
+    txreq -url "/a"
+    rxresp
+    expect resp.status == 200
+    expect resp.bodylen == 10
+    expect resp.http.X-Cache-Hit == 1
+
+    txreq -url "/b"
+    rxresp
+    expect resp.status == 200
+    expect resp.bodylen == 20
+    expect resp.http.X-Cache-Hit == 1
+
+    txreq -url "/c"
+    rxresp
+    expect resp.status == 200
+    expect resp.bodylen == 30
+    expect resp.http.X-Cache-Hit == 1
+} -run
index 8282949bf70bc6752ff1691e9bf2e52a7d58e4da..acbb1ecc938e3bf8830328a4dddaf85736498583 100644 (file)
@@ -4061,6 +4061,7 @@ void http_check_response_for_cacheability(struct stream *s, struct channel *res)
        int has_freshness_info = 0;
        int has_validator = 0;
        int has_null_maxage = 0;
+       int has_smaxage = 0;
 
        if (txn->status < 200) {
                /* do not try to cache interim responses! */
@@ -4107,7 +4108,7 @@ void http_check_response_for_cacheability(struct stream *s, struct channel *res)
                }
                if (istmatchi(ctx.value, ist("s-maxage"))) {
                        has_freshness_info = 1;
-                       has_null_maxage = 0;    /* The null max-age is overridden, ignore it */
+                       has_smaxage = 1;        /* overrides a null max-age, wherever it is */
                        continue;
                }
                if (istmatchi(ctx.value, ist("max-age"))) {
@@ -4118,7 +4119,7 @@ void http_check_response_for_cacheability(struct stream *s, struct channel *res)
 
        /* We had a 'max-age=0' directive but no extra s-maxage, do not cache
         * the response. */
-       if (has_null_maxage) {
+       if (has_null_maxage && !has_smaxage) {
                txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
        }