-
Notifications
You must be signed in to change notification settings - Fork 0
/
headers.go
38 lines (32 loc) · 1.01 KB
/
headers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package godest
import (
"fmt"
"net/http"
)
type HeaderOption func(http.Header)
func WithContentType(contentType string) HeaderOption {
return func(h http.Header) {
h.Set("Content-Type", contentType)
}
}
func WithUncacheable() HeaderOption {
return func(h http.Header) {
// Don't cache cookies - see the "Web Content Caching" subsection of
// https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html
h.Set("Cache-Control", "no-cache=\"Set-Cookie, Set-Cookie2\", no-store, max-age=0")
}
}
func WithAlwaysRevalidate() HeaderOption {
return func(h http.Header) {
h.Set("Cache-Control", "private, no-cache")
}
}
func WithRevalidateWhenStale(maxAge int) HeaderOption {
return func(h http.Header) {
// Don't cache cookies - see the "Web Content Caching" subsection of
// https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html
h.Set("Cache-Control", fmt.Sprintf(
"private, no-cache=\"Set-Cookie, Set-Cookie2\", max-age=%d, must-revalidate", maxAge,
))
}
}