-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IAM: Set Cache-Control headers (#3147)
- Loading branch information
Showing
4 changed files
with
218 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright (C) 2024 Nuts community | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package cache | ||
|
||
import ( | ||
"fmt" | ||
"github.com/labstack/echo/v4" | ||
"github.com/labstack/echo/v4/middleware" | ||
"time" | ||
) | ||
|
||
// Middleware is a middleware that sets the Cache-Control header (no-cache or max-age) for the given request URLs. | ||
// Use MaxAge or NoCache to create a new instance. | ||
type Middleware struct { | ||
Skipper middleware.Skipper | ||
maxAge time.Duration | ||
} | ||
|
||
func (m Middleware) Handle(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
if !m.Skipper(c) { | ||
if m.maxAge == -1 { | ||
c.Response().Header().Set("Cache-Control", "no-cache") | ||
// Pragma is deprecated (HTTP/1.0) but it's specified by OAuth2 RFC6749, | ||
// so specify it for compliance. | ||
c.Response().Header().Set("Pragma", "no-store") | ||
} else if m.maxAge > 0 { | ||
c.Response().Header().Set("Cache-Control", fmt.Sprintf("max-age=%d", int(m.maxAge.Seconds()))) | ||
} | ||
} | ||
return next(c) | ||
} | ||
} | ||
|
||
// MaxAge creates a new middleware that sets the Cache-Control header to the given max-age for the given request URLs. | ||
func MaxAge(maxAge time.Duration, requestURLs ...string) Middleware { | ||
return Middleware{ | ||
Skipper: matchRequestPathSkipper(requestURLs), | ||
maxAge: maxAge, | ||
} | ||
} | ||
|
||
// NoCache creates a new middleware that sets the Cache-Control header to no-cache for the given request URLs. | ||
func NoCache(requestURLs ...string) Middleware { | ||
return Middleware{ | ||
Skipper: matchRequestPathSkipper(requestURLs), | ||
maxAge: -1, | ||
} | ||
} | ||
|
||
func matchRequestPathSkipper(requestURLs []string) func(c echo.Context) bool { | ||
return func(c echo.Context) bool { | ||
for _, curr := range requestURLs { | ||
if c.Request().URL.Path == curr { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright (C) 2024 Nuts community | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package cache | ||
|
||
import ( | ||
"github.com/labstack/echo/v4" | ||
"github.com/stretchr/testify/require" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestMaxAge(t *testing.T) { | ||
t.Run("match", func(t *testing.T) { | ||
e := echo.New() | ||
httpResponse := httptest.NewRecorder() | ||
echoContext := e.NewContext(httptest.NewRequest("GET", "/a", nil), httpResponse) | ||
|
||
err := MaxAge(time.Minute, "/a", "/b").Handle(func(c echo.Context) error { | ||
return c.String(200, "OK") | ||
})(echoContext) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, "max-age=60", httpResponse.Header().Get("Cache-Control")) | ||
}) | ||
t.Run("no match", func(t *testing.T) { | ||
e := echo.New() | ||
httpResponse := httptest.NewRecorder() | ||
echoContext := e.NewContext(httptest.NewRequest("GET", "/c", nil), httpResponse) | ||
|
||
err := MaxAge(time.Minute, "/a", "/b").Handle(func(c echo.Context) error { | ||
return c.String(200, "OK") | ||
})(echoContext) | ||
|
||
require.NoError(t, err) | ||
require.Empty(t, httpResponse.Header().Get("Cache-Control")) | ||
}) | ||
|
||
} | ||
|
||
func TestNoCache(t *testing.T) { | ||
t.Run("match", func(t *testing.T) { | ||
e := echo.New() | ||
httpResponse := httptest.NewRecorder() | ||
echoContext := e.NewContext(httptest.NewRequest("GET", "/a", nil), httpResponse) | ||
|
||
err := NoCache("/a", "/b").Handle(func(c echo.Context) error { | ||
return c.String(200, "OK") | ||
})(echoContext) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, "no-cache", httpResponse.Header().Get("Cache-Control")) | ||
require.Equal(t, "no-store", httpResponse.Header().Get("Pragma")) | ||
}) | ||
t.Run("no match", func(t *testing.T) { | ||
e := echo.New() | ||
httpResponse := httptest.NewRecorder() | ||
echoContext := e.NewContext(httptest.NewRequest("GET", "/c", nil), httpResponse) | ||
|
||
err := NoCache("/a", "/b").Handle(func(c echo.Context) error { | ||
return c.String(200, "OK") | ||
})(echoContext) | ||
|
||
require.NoError(t, err) | ||
require.Empty(t, httpResponse.Header().Get("Cache-Control")) | ||
require.Empty(t, httpResponse.Header().Get("Pragma")) | ||
}) | ||
} |