diff --git a/CHANGELOG.md b/CHANGELOG.md index 543ecdbd..b60c4db4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ ### Change - Don't report `The image request is cancelled` errors. +### Fix +- Fix the `Cache-Control` and `Expires` headers behavior when both `IMGPROXY_CACHE_CONTROL_PASSTHROUGH` and `IMGPROXY_FALLBACK_IMAGE_TTL` configs are set. +- (pro) Fix the `IMGPROXY_FALLBACK_IMAGE_TTL` config behavior when the `fallback_image_url` processing option is used. + ## [3.18.2] - 2023-07-13 ### Fix - Fix saving to JPEG when using linear colorspace. diff --git a/processing_handler.go b/processing_handler.go index 3b479d7b..22b2a6bb 100644 --- a/processing_handler.go +++ b/processing_handler.go @@ -57,15 +57,20 @@ func initProcessingHandler() { func setCacheControl(rw http.ResponseWriter, force *time.Time, originHeaders map[string]string) { var cacheControl, expires string - var ttl int - if force != nil { + ttl := -1 + + if _, ok := originHeaders["Fallback-Image"]; ok && config.FallbackImageTTL > 0 { + ttl = config.FallbackImageTTL + } + + if force != nil && (ttl < 0 || force.Before(time.Now().Add(time.Duration(ttl)*time.Second))) { rw.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, public", int(time.Until(*force).Seconds()))) rw.Header().Set("Expires", force.Format(http.TimeFormat)) return } - if config.CacheControlPassthrough && originHeaders != nil { + if config.CacheControlPassthrough && ttl < 0 && originHeaders != nil { if val, ok := originHeaders["Cache-Control"]; ok && len(val) > 0 { cacheControl = val } @@ -75,9 +80,8 @@ func setCacheControl(rw http.ResponseWriter, force *time.Time, originHeaders map } if len(cacheControl) == 0 && len(expires) == 0 { - ttl = config.TTL - if _, ok := originHeaders["Fallback-Image"]; ok && config.FallbackImageTTL > 0 { - ttl = config.FallbackImageTTL + if ttl < 0 { + ttl = config.TTL } cacheControl = fmt.Sprintf("max-age=%d, public", ttl) expires = time.Now().Add(time.Second * time.Duration(ttl)).Format(http.TimeFormat)