From 6abdc8d2a9b7ee729cccebeb2115ca7cda465713 Mon Sep 17 00:00:00 2001 From: mappu Date: Sat, 28 Oct 2017 13:27:44 +1300 Subject: [PATCH] server: use real content length, use system MIME info instead of handrolling it --- main.go | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index 0e96daf..4b7de26 100644 --- a/main.go +++ b/main.go @@ -5,8 +5,9 @@ import ( "fmt" "io/ioutil" "log" + "mime" "net/http" - "strings" + "path/filepath" "code.ivysaur.me/libnmdc" "github.com/googollee/go-socket.io" @@ -204,27 +205,8 @@ func (this *App) StaticRequestHandler(w http.ResponseWriter, r *http.Request) { return } - knownContentTypes := map[string]string{ - ".htm": "text/html", - ".png": "image/png", - ".ico": "image/x-icon", - // No CSS/JS since they're embedded in the HTML - } - - foundMime := false - for ext, mimeType := range knownContentTypes { - if strings.HasSuffix(fileName, ext) { - w.Header().Set("Content-Type", mimeType) - foundMime = true - break - } - } - if !foundMime { - w.Header().Set("Content-Type", "application/x-octet-stream") - } - dataInfo, _ := AssetInfo(fileName) - w.Header().Set("Content-Length", fmt.Sprintf("%d", dataInfo.Size())) - + w.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(fileName))) + w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data))) w.Write(data) }