From c7183b18f872a9be0a2e128d2a28a6e7d35864af Mon Sep 17 00:00:00 2001 From: vejipe Date: Tue, 2 Jan 2024 19:02:36 +0100 Subject: [PATCH] fix(rfc): Age header update (#432) Fixes following test from cache-tests.fyi: freshness-max-age-age, freshness-max-age-s-maxage-shared-longer-multiple. Unlocks the whole "Age Parsing" section of the test suite. Co-authored-by: Vincent Jordan --- pkg/rfc/age.go | 4 ++++ pkg/rfc/cache_status.go | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/rfc/age.go b/pkg/rfc/age.go index 6f95caa36..ea3bfe0e2 100644 --- a/pkg/rfc/age.go +++ b/pkg/rfc/age.go @@ -20,6 +20,10 @@ func validateMaxAgeCachedResponse(res *http.Response, maxAge int, addTime int) * func ValidateMaxAgeCachedResponse(co *cacheobject.RequestCacheDirectives, res *http.Response) *http.Response { responseCc, _ := cacheobject.ParseResponseCacheControl(res.Header.Get("Cache-Control")) ma := co.MaxAge + if responseCc.MaxAge > -1 { + ma = responseCc.MaxAge + } + // s-maxage overwrites max-age in the response if available together if responseCc.SMaxAge > -1 { ma = responseCc.SMaxAge } diff --git a/pkg/rfc/cache_status.go b/pkg/rfc/cache_status.go index 9ab2d0dc2..98e89db19 100644 --- a/pkg/rfc/cache_status.go +++ b/pkg/rfc/cache_status.go @@ -105,8 +105,18 @@ func manageAge(h *http.Header, ttl time.Duration, cacheName, key string) { apparentAge = 0 } + var oldAge int + { + var err error + oldAgeString := h.Get("Age") + oldAge, err = strconv.Atoi(oldAgeString) + if err != nil { + oldAge = 0 + } + } + cage := int(math.Ceil(apparentAge.Seconds())) - age := strconv.Itoa(cage) + age := strconv.Itoa(oldAge + cage) h.Set("Age", age) ttlValue := strconv.Itoa(int(ttl.Seconds()) - cage) h.Set("Cache-Status", cacheName+"; hit; ttl="+ttlValue+"; key="+key)