Skip to content

Commit

Permalink
fix(rfc): Age header update (#432)
Browse files Browse the repository at this point in the history
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 <vincent.jordan@platform.sh>
  • Loading branch information
vejipe and Vincent Jordan committed Jan 2, 2024
1 parent 40a16bf commit c7183b1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pkg/rfc/age.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 11 additions & 1 deletion pkg/rfc/cache_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c7183b1

Please sign in to comment.