Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changes-entries/69512.txt
Original file line number Diff line number Diff line change
@@ -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]
27 changes: 27 additions & 0 deletions modules/cache/mod_cache_socache.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
50 changes: 50 additions & 0 deletions test/modules/cache/test_004_socache_revalidate.py
Original file line number Diff line number Diff line change
@@ -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'<Directory {docs}>',
' Require all granted',
' Header set Cache-Control "max-age=0, must-revalidate"',
'</Directory>',
'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()
Loading