Skip to content

Commit

Permalink
wrap mime.TypeByExtension with preset mimetypes
Browse files Browse the repository at this point in the history
  • Loading branch information
cocoastorm committed Oct 22, 2019
1 parent a0d727b commit d1d48ad
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions internal/webserver/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,26 @@ import (
"syscall"
)

var rxRepeatedStrip = regexp.MustCompile(`(?i)-+`)
var (
rxRepeatedStrip = regexp.MustCompile(`(?i)-+`)

presetMimeTypes = map[string]string{
".css": "text/css; charset=utf-8",
".html": "text/html; charset=utf-8",
".js": "application/javascript",
".png": "image/png",
}
)

func guessTypeByExtension(ext string) string {
ext = strings.ToLower(ext)

if v, ok := presetMimeTypes[ext]; ok {
return v
}

return mime.TypeByExtension(ext)
}

func serveFile(w http.ResponseWriter, filePath string, cache bool) error {
// Open file
Expand All @@ -42,7 +61,7 @@ func serveFile(w http.ResponseWriter, filePath string, cache bool) error {

// Set content type
ext := fp.Ext(filePath)
mimeType := mime.TypeByExtension(ext)
mimeType := guessTypeByExtension(ext)
if mimeType != "" {
w.Header().Set("Content-Type", mimeType)
w.Header().Set("X-Content-Type-Options", "nosniff")
Expand Down

0 comments on commit d1d48ad

Please sign in to comment.