From 5915e8e86ada284d9e55b5a8b91de11a36f90b88 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Thu, 4 Sep 2014 08:24:42 +0200 Subject: [PATCH] Don't trust mime.TypeByExtension for the easy stuff (fixes #598) --- cmd/syncthing/gui.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/cmd/syncthing/gui.go b/cmd/syncthing/gui.go index 0a01b920b..0f9a1cd65 100644 --- a/cmd/syncthing/gui.go +++ b/cmd/syncthing/gui.go @@ -680,7 +680,7 @@ func embeddedStatic(assetDir string) http.Handler { return } - mtype := mime.TypeByExtension(filepath.Ext(r.URL.Path)) + mtype := mimeTypeForFile(file) if len(mtype) != 0 { w.Header().Set("Content-Type", mtype) } @@ -690,3 +690,26 @@ func embeddedStatic(assetDir string) http.Handler { w.Write(bs) }) } + +func mimeTypeForFile(file string) string { + // We use a built in table of the common types since the system + // TypeByExtension might be unreliable. But if we don't know, we delegate + // to the system. + ext := filepath.Ext(file) + switch ext { + case ".htm", ".html": + return "text/html" + case ".css": + return "text/css" + case ".js": + return "application/javascript" + case ".json": + return "application/json" + case ".png": + return "image/png" + case ".ttf": + return "application/x-font-ttf" + default: + return mime.TypeByExtension(ext) + } +}