Bladeren bron

Fallback image TTL

Viktor Sokolov 1 maand geleden
bovenliggende
commit
b3a72e630b
4 gewijzigde bestanden met toevoegingen van 18 en 50 verwijderingen
  1. 2 0
      headerwriter/config.go
  2. 7 16
      headerwriter/writer.go
  3. 8 34
      headerwriter/writer_test.go
  4. 1 0
      httpheaders/headers.go

+ 2 - 0
headerwriter/config.go

@@ -8,6 +8,7 @@ import (
 type Config struct {
 	SetCanonicalHeader      bool // Indicates whether to set the canonical header
 	DefaultTTL              int  // Default Cache-Control max-age= value for cached images
+	FallbackImageTTL        int  // TTL for images served as fallbacks
 	CacheControlPassthrough bool // Passthrough the Cache-Control from the original response
 	LastModifiedEnabled     bool // Set the Last-Modified header
 	EnableClientHints       bool // Enable Vary header
@@ -19,6 +20,7 @@ func NewConfigFromEnv() *Config {
 	return &Config{
 		SetCanonicalHeader:      config.SetCanonicalHeader,
 		DefaultTTL:              config.TTL,
+		FallbackImageTTL:        config.FallbackImageTTL,
 		LastModifiedEnabled:     config.LastModifiedEnabled,
 		CacheControlPassthrough: config.CacheControlPassthrough,
 		EnableClientHints:       config.EnableClientHints,

+ 7 - 16
headerwriter/writer.go

@@ -45,20 +45,15 @@ func New(config *Config, originalResponseHeaders http.Header, url string) *Write
 	}
 }
 
-// TODO: Do not remove, will be used shortly in processing_handler.go
-//
 // SetIsFallbackImage sets the Fallback-Image header to
 // indicate that the fallback image was used.
-// func (w *Writer) SetIsFallbackImage() {
-// 	w.result.Set("Fallback-Image", "1")
-// }
-
-// SetMaxAge sets the max-age for the Cache-Control header.
-func (w *Writer) SetMaxAge(ttl int) {
-	// We set maxAge to ttl if it's explicitly passed
-	if ttl >= 0 {
-		w.maxAge = ttl
+func (w *Writer) SetIsFallbackImage() {
+	// We set maxAge to FallbackImageTTL if it's explicitly passed
+	if w.config.FallbackImageTTL >= 0 {
+		w.maxAge = w.config.FallbackImageTTL
 	}
+
+	w.result.Set(httpheaders.FallbackImage, "1")
 }
 
 // SetForceExpires sets the TTL from time
@@ -73,11 +68,7 @@ func (w *Writer) SetForceExpires(force *time.Time) {
 
 	// If maxAge outlives expires or was not set, we'll use expires as maxAge.
 	if w.maxAge < 0 || force.Before(currentMaxAgeTime) {
-		expiresTTL := min(w.config.DefaultTTL, max(0, int(time.Until(*force).Seconds())))
-
-		if expiresTTL > 0 {
-			w.maxAge = expiresTTL
-		}
+		w.maxAge = min(w.config.DefaultTTL, max(0, int(time.Until(*force).Seconds())))
 	}
 }
 

+ 8 - 34
headerwriter/writer_test.go

@@ -162,12 +162,14 @@ func (s *HeaderWriterSuite) TestHeaderCases() {
 			res: http.Header{
 				httpheaders.CacheControl:          []string{"max-age=1, public"},
 				httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
+				httpheaders.FallbackImage:         []string{"1"},
 			},
 			config: Config{
-				DefaultTTL: 3600,
+				DefaultTTL:       3600,
+				FallbackImageTTL: 1,
 			},
 			fn: func(w *Writer) {
-				w.SetMaxAge(1)
+				w.SetIsFallbackImage()
 			},
 		},
 		{
@@ -190,12 +192,14 @@ func (s *HeaderWriterSuite) TestHeaderCases() {
 			res: http.Header{
 				httpheaders.CacheControl:          []string{fmt.Sprintf("max-age=%s, public", shortExpiresSeconds)},
 				httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
+				httpheaders.FallbackImage:         []string{"1"},
 			},
 			config: Config{
-				DefaultTTL: math.MaxInt32,
+				DefaultTTL:       math.MaxInt32,
+				FallbackImageTTL: 600,
 			},
 			fn: func(w *Writer) {
-				w.SetMaxAge(600)
+				w.SetIsFallbackImage()
 				w.SetForceExpires(&shortExpires)
 			},
 		},
@@ -271,36 +275,6 @@ func (s *HeaderWriterSuite) TestHeaderCases() {
 				w.SetContentType("image/png")
 			},
 		},
-		// NOTE: Do not remove, will be used shortly in processing_handler.go
-		//
-		// {
-		// 	name: "WriteIsFallbackImage",
-		// 	req:  http.Header{},
-		// 	res: http.Header{
-		// 		"Fallback-Image":                  []string{"1"},
-		// 		httpheaders.CacheControl:          []string{"no-cache"},
-		// 		httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
-		// 	},
-		// 	config: Config{},
-		// 	fn: func(w *Writer) {
-		// 		w.SetIsFallbackImage()
-		// 	},
-		// },
-		{
-			name: "SetMaxAgeZeroOrNegative",
-			req:  http.Header{},
-			res: http.Header{
-				httpheaders.CacheControl:          []string{"max-age=3600, public"},
-				httpheaders.ContentSecurityPolicy: []string{"script-src 'none'"},
-			},
-			config: Config{
-				DefaultTTL: 3600,
-			},
-			fn: func(w *Writer) {
-				w.SetMaxAge(0)
-				w.SetMaxAge(-10)
-			},
-		},
 		{
 			name: "SetMaxAgeFromExpiresNil",
 			req:  http.Header{},

+ 1 - 0
httpheaders/headers.go

@@ -34,6 +34,7 @@ const (
 	Expect                          = "Expect"
 	ExpectCt                        = "Expect-Ct"
 	Expires                         = "Expires"
+	FallbackImage                   = "Fallback-Image"
 	Forwarded                       = "Forwarded"
 	Host                            = "Host"
 	IfMatch                         = "If-Match"