Skip to content

Commit

Permalink
✨ Cache middleware: Add flag for e2e headers.
Browse files Browse the repository at this point in the history
Set flag to prevent e2e headers caching to
be the default behavior of the cache middleware.
This would otherwise change quite a lot the
experience for cache middleware current users.
  • Loading branch information
thylong committed Mar 5, 2022
1 parent aaa2238 commit 751553a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
18 changes: 10 additions & 8 deletions middleware/cache/cache.go
Expand Up @@ -153,14 +153,16 @@ func New(config ...Config) fiber.Handler {

// Store all end-to-end headers
// (more: https://datatracker.ietf.org/doc/html/rfc2616#section-13.5.1)
c.Response().Header.VisitAll(
func(key []byte, value []byte) {
keyS := string(key)
if _, isHopbyHop := ignoreHeaders[keyS]; !isHopbyHop {
e.e2eHeaders[keyS] = value
}
},
)
if cfg.E2EHeaders {
c.Response().Header.VisitAll(
func(key []byte, value []byte) {
keyS := string(key)
if _, isHopbyHop := ignoreHeaders[keyS]; !isHopbyHop {
e.e2eHeaders[keyS] = value
}
},
)
}

// default cache expiration
expiration := uint64(cfg.Expiration.Seconds())
Expand Down
4 changes: 3 additions & 1 deletion middleware/cache/cache_test.go
Expand Up @@ -304,7 +304,9 @@ func Test_CustomExpiration(t *testing.T) {

func Test_AdditionalE2EResponseHeaders(t *testing.T) {
app := fiber.New()
app.Use(New())
app.Use(New(Config{
E2EHeaders: true,
}))

app.Get("/", func(c *fiber.Ctx) error {
c.Response().Header.Add("X-Foobar", "foobar")
Expand Down
6 changes: 6 additions & 0 deletions middleware/cache/config.go
Expand Up @@ -54,6 +54,11 @@ type Config struct {

// Deprecated, use KeyGenerator instead
Key func(*fiber.Ctx) string

// allows you to store additional headers generated by next middlewares & handler
//
// Default: false
E2EHeaders bool
}

// ConfigDefault is the default config
Expand All @@ -66,6 +71,7 @@ var ConfigDefault = Config{
return utils.CopyString(c.Path())
},
ExpirationGenerator: nil,
E2EHeaders: false,
Storage: nil,
}

Expand Down

0 comments on commit 751553a

Please sign in to comment.