Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI files prevent cache by response headers #2159

Merged
merged 1 commit into from
Jun 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion pkg/api/ui_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path"
"strings"
"time"

"github.com/rakyll/statik/fs"
"github.com/treeverse/lakefs/pkg/auth"
Expand All @@ -15,13 +16,51 @@ import (
"github.com/treeverse/lakefs/pkg/webui"
)

// Taken from https://github.com/mytrile/nocache
var noCacheHeaders = map[string]string{
"Expires": time.Unix(0, 0).Format(time.RFC1123),
"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",
}

func NewUIHandler(authService auth.Service, gatewayDomains []string) http.Handler {
mux := http.NewServeMux()
staticFiles, _ := fs.NewWithNamespace(webui.Webui)
mux.Handle("/", NewHandlerWithDefault(staticFiles, http.FileServer(staticFiles), "/", gatewayDomains))
fileServer := http.FileServer(staticFiles)
noCacheFileServer := NoCacheHandler(fileServer)
mux.Handle("/", NewHandlerWithDefault(staticFiles, noCacheFileServer, "/", gatewayDomains))
return mux
}

// NoCacheHandler based on github.com/zenazn/goji's NoCache
func NoCacheHandler(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 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)
}

handler.ServeHTTP(w, r)
})
}

func NewHandlerWithDefault(root http.FileSystem, handler http.Handler, defaultPath string, gatewayDomains []string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if isGatewayRequest(r) {
Expand Down