Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format Cache-Control header #22

Open
missinglink opened this issue Jun 10, 2021 · 2 comments
Open

Format Cache-Control header #22

missinglink opened this issue Jun 10, 2021 · 2 comments

Comments

@missinglink
Copy link

missinglink commented Jun 10, 2021

Heya,

I was wondering if there was some functionality which allows reversing the parse process to generate a Cache-Control string value from a ResponseCacheDirectives struct?

My usecase is that I would like to act as a proxy, parse the response header coming from my downstream service, check if the max-age is set, and if the value is unset or too low I will modify it before proxying it to the user.

I ended up just doing this:

// enforce a default CacheControl max-age value if:
// 	1. A default value is configured for this endpoint.
// 	2. The existing 'Cache-Control' header is unset or 'public'.
// 	3. The existing 'max-age' value is unset or lower than the default.
if e.DefaultAge > 0 {
	policy, _ := cacheobject.ParseResponseCacheControl(resp.Header.Get("Cache-Control"))
	if policy.Public && int(policy.MaxAge) < e.DefaultAge {
		// policy.MaxAge = cacheobject.DeltaSeconds(e.DefaultAge)
		resp.Header.Set("Cache-Control", fmt.Sprintf("public, max-age:%d", e.DefaultAge))
	}
}

I was hoping to find something more elegant than the fmt.Sprintf("public, max-age:%d", e.DefaultAge) line because presumably it's more complex than just ignoring the other properties?

@pquerna
Copy link
Owner

pquerna commented Jun 10, 2021

I think this would generally be a good feature - two quick thoughts: we might need a new structure to properly serialize it, and we might want to emit other headers beyond cache-control.

@tonyhb
Copy link

tonyhb commented Oct 31, 2023

+1 on this. Would love to take a ResponseCacheDirectives and convert that to a string :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants