Skip to content
Merged
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
feat(chore): add request stale-if-error directive
  • Loading branch information
darkweak committed Apr 15, 2023
commit 792e5eae320591934acfe3ec0c3bd3e1ea856bff
15 changes: 11 additions & 4 deletions cacheobject/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ func parse(value string, cd cacheDirective) error {
// time in seconds: http://tools.ietf.org/html/rfc7234#section-1.2.1
//
// When set to -1, this means unset.
//
type DeltaSeconds int32

// Parser for delta-seconds, a uint31, more or less:
Expand Down Expand Up @@ -167,7 +166,6 @@ type cacheDirective interface {
// LOW LEVEL API: Representation of possible request directives in a `Cache-Control` header: http://tools.ietf.org/html/rfc7234#section-5.2.1
//
// Note: Many fields will be `nil` in practice.
//
type RequestCacheDirectives struct {

// max-age(delta seconds): http://tools.ietf.org/html/rfc7234#section-5.2.1.1
Expand All @@ -188,7 +186,7 @@ type RequestCacheDirectives struct {
// by no more than the specified number of seconds. If no value is
// assigned to max-stale, then the client is willing to accept a stale
// response of any age.
MaxStale DeltaSeconds
MaxStale DeltaSeconds
MaxStaleSet bool

// min-fresh(delta seconds): http://tools.ietf.org/html/rfc7234#section-5.2.1.3
Expand Down Expand Up @@ -227,6 +225,9 @@ type RequestCacheDirectives struct {
// wishes to obtain a stored response.
OnlyIfCached bool

// stale-if-error(delta seconds): https://datatracker.ietf.org/doc/html/rfc5861#section-4
StaleIfError DeltaSeconds

// Extensions: http://tools.ietf.org/html/rfc7234#section-5.2.3
//
// The Cache-Control header field can be extended through the use of one
Expand All @@ -253,6 +254,8 @@ func (cd *RequestCacheDirectives) addToken(token string) error {
cd.NoTransform = true
case "only-if-cached":
cd.OnlyIfCached = true
case "stale-if-error":
err = ErrMaxAgeDeltaSeconds
default:
cd.Extensions = append(cd.Extensions, token)
}
Expand Down Expand Up @@ -286,6 +289,11 @@ func (cd *RequestCacheDirectives) addPair(token string, v string) error {
err = ErrNoTransformNoArgs
case "only-if-cached":
err = ErrOnlyIfCachedNoArgs
case "stale-if-error":
cd.StaleIfError, err = parseDeltaSeconds(v)
if err != nil {
err = ErrStaleIfErrorDeltaSeconds
}
default:
// TODO(pquerna): this sucks, making user re-parse
cd.Extensions = append(cd.Extensions, token+"="+v)
Expand All @@ -312,7 +320,6 @@ func ParseRequestCacheControl(value string) (*RequestCacheDirectives, error) {
// LOW LEVEL API: Repersentation of possible response directives in a `Cache-Control` header: http://tools.ietf.org/html/rfc7234#section-5.2.2
//
// Note: Many fields will be `nil` in practice.
//
type ResponseCacheDirectives struct {

// must-revalidate(bool): http://tools.ietf.org/html/rfc7234#section-5.2.2.1
Expand Down
7 changes: 7 additions & 0 deletions cacheobject/directive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,13 @@ func TestNoSpacesIssue3(t *testing.T) {
require.Equal(t, cd.MustRevalidate, true)
}

func TestStaleIfError(t *testing.T) {
cd, err := ParseRequestCacheControl(`stale-if-error=999`)
require.NoError(t, err)
require.NotNil(t, cd)
require.Equal(t, cd.StaleIfError, DeltaSeconds(999))
}

func TestNoSpacesIssue3PrivateFields(t *testing.T) {
cd, err := ParseResponseCacheControl(`no-cache, no-store, private=set-cookie,hello, max-age=0, must-revalidate`)
require.NoError(t, err)
Expand Down