storagenode/console/consoleserver: set content-type manually
http.FileServer relies on mime types defined in the operating system. These values may be misconfigured, so a javascript file might end up being served as "plain/text". Change-Id: I3c13c8a9ac484bd765a4de0f8253bfe40dde7513
This commit is contained in:
parent
bd78945116
commit
9e5679fdaa
35
storagenode/console/consoleserver/mime.go
Normal file
35
storagenode/console/consoleserver/mime.go
Normal file
@ -0,0 +1,35 @@
|
||||
// Copyright (C) 2020 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package consoleserver
|
||||
|
||||
import "strings"
|
||||
|
||||
// CommonContentType returns content-type for common extensions.
|
||||
func CommonContentType(ext string) (string, bool) {
|
||||
ext = strings.ToLower(ext)
|
||||
mime, ok := commonContentType[ext]
|
||||
return mime, ok
|
||||
}
|
||||
|
||||
var commonContentType = map[string]string{
|
||||
".css": "text/css; charset=utf-8",
|
||||
".gif": "image/gif",
|
||||
".htm": "text/html; charset=utf-8",
|
||||
".html": "text/html; charset=utf-8",
|
||||
".jpeg": "image/jpeg",
|
||||
".jpg": "image/jpeg",
|
||||
".js": "application/javascript",
|
||||
".mjs": "application/javascript",
|
||||
".otf": "font/otf",
|
||||
".pdf": "application/pdf",
|
||||
".png": "image/png",
|
||||
".svg": "image/svg+xml",
|
||||
".ttf": "font/ttf",
|
||||
".wasm": "application/wasm",
|
||||
".webp": "image/webp",
|
||||
".xml": "text/xml; charset=utf-8",
|
||||
".sfnt": "font/sfnt",
|
||||
".woff": "font/woff",
|
||||
".woff2": "font/woff2",
|
||||
}
|
@ -8,6 +8,7 @@ import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/zeebo/errs"
|
||||
@ -188,6 +189,16 @@ func (server *Server) satelliteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// cacheMiddleware is a middleware for caching static files.
|
||||
func (server *Server) cacheMiddleware(fn http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// "mime" package, which http.FileServer uses, depends on Operating System
|
||||
// configuration for mime-types. When a system has hardcoded mime-types to
|
||||
// something else, they might serve ".js" as a "plain/text".
|
||||
//
|
||||
// Override any of that default behavior to ensure we get the correct types for
|
||||
// common files.
|
||||
if contentType, ok := CommonContentType(filepath.Ext(r.URL.Path)); ok {
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
}
|
||||
|
||||
w.Header().Set("Cache-Control", "public, max-age=31536000")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user