From efb23778b8e5744b64a458c596fa6a1a3c4451c3 Mon Sep 17 00:00:00 2001 From: Adhityaa Date: Sun, 3 Jun 2018 20:19:36 +0530 Subject: [PATCH] router_static.go: set Content-Type header --- api/router_static.go | 11 +++++++---- api/utils_sanitise.go | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/api/router_static.go b/api/router_static.go index 38cd68c..ee1b6b7 100644 --- a/api/router_static.go +++ b/api/router_static.go @@ -8,6 +8,8 @@ import ( "io/ioutil" "net/http" "os" + "path" + "mime" ) func redirectLogin(w http.ResponseWriter, r *http.Request) { @@ -34,11 +36,11 @@ func initStaticRouter(router *mux.Router) error { for _, file := range files { sl := string(os.PathSeparator) - path := sl + dir + sl + file.Name() + p := sl + dir + sl + file.Name() - contents, err := ioutil.ReadFile("." + path) + contents, err := ioutil.ReadFile("." + p) if err != nil { - logger.Errorf("cannot read file %s: %v", path, err) + logger.Errorf("cannot read file %s: %v", p, err) return err } @@ -49,7 +51,8 @@ func initStaticRouter(router *mux.Router) error { asset[p] = prefix + string(contents); - router.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { + router.HandleFunc(p, func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", mime.TypeByExtension(path.Ext(r.URL.Path))) fmt.Fprint(w, asset[r.URL.Path]) }) } diff --git a/api/utils_sanitise.go b/api/utils_sanitise.go index f4d7f57..a7c2229 100644 --- a/api/utils_sanitise.go +++ b/api/utils_sanitise.go @@ -26,10 +26,10 @@ func stripDomain(domain string) string { return noProtocol } -var path = regexp.MustCompile(`(https?://[^/]*)`) +var pathMatch = regexp.MustCompile(`(https?://[^/]*)`) func stripPath(url string) string { - strippedPath := path.ReplaceAllString(url, ``) + strippedPath := pathMatch.ReplaceAllString(url, ``) return strippedPath }