server: use real content length, use system MIME info instead of handrolling it

This commit is contained in:
mappu 2017-10-28 13:27:44 +13:00
parent 1a281095f0
commit 6abdc8d2a9

26
main.go
View File

@ -5,8 +5,9 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"mime"
"net/http" "net/http"
"strings" "path/filepath"
"code.ivysaur.me/libnmdc" "code.ivysaur.me/libnmdc"
"github.com/googollee/go-socket.io" "github.com/googollee/go-socket.io"
@ -204,27 +205,8 @@ func (this *App) StaticRequestHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
knownContentTypes := map[string]string{ w.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(fileName)))
".htm": "text/html", w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))
".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.Write(data) w.Write(data)
} }