2020-12-04 15:28:02 +00:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-12-06 11:18:42 +00:00
|
|
|
"strings"
|
2020-12-04 15:28:02 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var epoch = time.Unix(0, 0).Format(time.RFC1123)
|
|
|
|
|
|
|
|
var noCacheHeaders = map[string]string{
|
|
|
|
"Expires": epoch,
|
|
|
|
"Cache-Control": "no-cache, private, max-age=0",
|
|
|
|
"Pragma": "no-cache",
|
|
|
|
"X-Accel-Expires": "0",
|
|
|
|
}
|
|
|
|
|
|
|
|
var etagHeaders = []string{
|
|
|
|
"ETag",
|
|
|
|
"If-Modified-Since",
|
|
|
|
"If-Match",
|
|
|
|
"If-None-Match",
|
|
|
|
"If-Range",
|
|
|
|
"If-Unmodified-Since",
|
|
|
|
}
|
|
|
|
|
2020-12-06 11:18:42 +00:00
|
|
|
func NoCache(h http.Handler, pathsToDisableCache []string) http.Handler {
|
|
|
|
pathLookup := make(map[string]bool)
|
|
|
|
|
|
|
|
for idx := range pathsToDisableCache {
|
|
|
|
pathLookup[strings.ToLower(pathsToDisableCache[idx])] = true
|
|
|
|
}
|
|
|
|
|
2020-12-04 15:28:02 +00:00
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
2020-12-06 11:18:42 +00:00
|
|
|
if _, shouldBeHandled := pathLookup[strings.ToLower(r.URL.Path)]; !shouldBeHandled {
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-04 15:28:02 +00:00
|
|
|
// Delete any ETag headers that may have been set
|
|
|
|
for _, v := range etagHeaders {
|
|
|
|
if r.Header.Get(v) != "" {
|
|
|
|
r.Header.Del(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set our NoCache headers
|
|
|
|
for k, v := range noCacheHeaders {
|
|
|
|
w.Header().Set(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|