diff --git a/changes-entries/69512.txt b/changes-entries/69512.txt new file mode 100644 index 00000000000..4ed416fa71a --- /dev/null +++ b/changes-entries/69512.txt @@ -0,0 +1,3 @@ + *) mod_cache_socache: Keep the cached response body when a cached entry + is revalidated with a 304 Not Modified, instead of returning an empty + body on later cache hits. PR 69512. [Arturo Bernal] diff --git a/modules/cache/mod_cache_socache.c b/modules/cache/mod_cache_socache.c index 78bd3e7bb4c..7ee471d8c34 100644 --- a/modules/cache/mod_cache_socache.c +++ b/modules/cache/mod_cache_socache.c @@ -936,6 +936,33 @@ static apr_status_t store_headers(cache_handle_t *h, request_rec *r, sobj->body_offset = slider; + /* On a conditional revalidation (a 304 Not Modified from the origin) the + * generic cache updates the stored headers through store_headers() and + * commits, without calling store_body() again. Unlike mod_cache_disk, + * mod_cache_socache keeps the headers and the body together in a single + * socache blob, and the code above has just rebuilt that blob in a fresh + * buffer holding the new headers only. Carry the previously cached body + * (loaded by open_entity() into sobj->body) into the new buffer so + * commit_entity() stores it again instead of dropping it. */ + if (sobj->body && !APR_BRIGADE_EMPTY(sobj->body)) { + apr_off_t blen = 0; + apr_size_t flat; + + apr_brigade_length(sobj->body, 1, &blen); + if (blen < 0 || (apr_uint64_t)slider + (apr_uint64_t)blen + > (apr_uint64_t)sobj->buffer_len) { + ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO() + "revalidated body does not fit in buffer, ignoring: %s", + sobj->name); + apr_pool_destroy(sobj->pool); + sobj->pool = NULL; + return APR_EGENERAL; + } + flat = (apr_size_t)blen; + apr_brigade_flatten(sobj->body, (char *) sobj->buffer + slider, &flat); + sobj->body_length = flat; + } + return APR_SUCCESS; } diff --git a/test/modules/cache/test_004_socache_revalidate.py b/test/modules/cache/test_004_socache_revalidate.py new file mode 100644 index 00000000000..bbb2341d1d1 --- /dev/null +++ b/test/modules/cache/test_004_socache_revalidate.py @@ -0,0 +1,50 @@ +import os + +import pytest + +from pyhttpd.conf import HttpdConf + + +class TestCacheSocacheRevalidate: + + @pytest.fixture(autouse=True, scope='class') + def _class_scope(self, env): + docs = os.path.join(env.gen_dir, "apache/htdocs/revtest") + os.makedirs(docs, exist_ok=True) + self.body = b"X" * 512 + b"\n" + with open(os.path.join(docs, "revalidate.bin"), "wb") as f: + f.write(self.body) + conf = HttpdConf(env, extras={ + f"cgi.{env.http_tld}": [ + f'Alias /revtest {docs}', + f'', + ' Require all granted', + ' Header set Cache-Control "max-age=0, must-revalidate"', + '', + 'CacheSocache shmcb', + 'CacheSocacheMaxSize 102400', + 'CacheEnable socache /revtest/revalidate.bin', + ], + }) + conf.add_vhost_cgi() + conf.install() + assert env.apache_restart() == 0 + + def test_cache_004_01_revalidate_keeps_body(self, env): + """A socache cache entry that is revalidated with a 304 must keep its + body. mod_cache_socache rewrites the single headers+body blob on + store_headers(); when the revalidation stores no new body it must not + drop the cached one (PR 69512). The resource is always stale + (max-age=0, must-revalidate) and the static handler answers the + conditional request with 304, so every request after the first + revalidates.""" + url = env.mkurl("http", "cgi", "/revtest/revalidate.bin") + sizes = [] + for _ in range(5): + r = env.curl_get(url) + assert r.response["status"] == 200, f"{r.response}" + sizes.append(len(r.response["body"])) + assert sizes == [513] * 5, ( + f"revalidation dropped the cached body: sizes per request {sizes}" + ) + env.httpd_error_log.ignore_recent()