Keep max-age within an expiring image URL's lifetime (closes #63)
check / check (push) Successful in 12s
check / check (push) Successful in 12s
Both image routes sent Cache-Control: public, max-age=31536000, immutable unconditionally, so a browser or proxy could keep serving an image for a year after its signed or encrypted URL had expired. max-age is now the whole seconds left until the URL expires, never negative and at most one year; a URL with no expiry keeps one year. The 304 answer uses the same value. An encrypted URL's expiry now reaches ImageRequest.Expires through ToImageRequest. immutable stays: freshness now ends no later than the URL's expiry. README.md documents the header. Model: opus-5-5
This commit was merged in pull request #146.
This commit is contained in:
@@ -220,6 +220,24 @@ func (s *Handlers) respondImageError(
|
||||
s.respondError(w, "internal error", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// cacheControl returns the Cache-Control header for an image served through a
|
||||
// URL that expires at expires, or never when expires is the zero time. A cache
|
||||
// may keep the image for a year, but not past the URL's expiry, after which
|
||||
// pixa refuses the URL. The seconds left are rounded down and never negative.
|
||||
// immutable only stops revalidation while the image is fresh, so it also ends
|
||||
// at the expiry.
|
||||
func cacheControl(expires time.Time) string {
|
||||
const oneYear = 365 * 24 * time.Hour
|
||||
|
||||
maxAge := oneYear
|
||||
|
||||
if !expires.IsZero() {
|
||||
maxAge = min(maxAge, max(time.Until(expires), 0))
|
||||
}
|
||||
|
||||
return fmt.Sprintf("public, max-age=%d, immutable", int64(maxAge/time.Second))
|
||||
}
|
||||
|
||||
// writeImageResponse writes headers and streams the image content,
|
||||
// handling conditional and HEAD requests.
|
||||
func (s *Handlers) writeImageResponse(
|
||||
@@ -235,7 +253,7 @@ func (s *Handlers) writeImageResponse(
|
||||
}
|
||||
|
||||
// Cache control headers
|
||||
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
|
||||
w.Header().Set("Cache-Control", cacheControl(req.Expires))
|
||||
w.Header().Set("X-Pixa-Cache", string(resp.CacheStatus))
|
||||
|
||||
if resp.ETag != "" {
|
||||
|
||||
Reference in New Issue
Block a user