1
0
mirror of https://github.com/golang/go synced 2024-11-18 23:05:06 -07:00

mime: Make filename extensions case-insensitive

Fixes #8350.

LGTM=bradfitz
R=golang-codereviews, bradfitz, dave
CC=golang-codereviews
https://golang.org/cl/127380043
This commit is contained in:
Jeff R. Allen 2014-08-28 08:22:54 -07:00 committed by Brad Fitzpatrick
parent 6179aca548
commit af12dc5cd5
5 changed files with 50 additions and 24 deletions

View File

@ -11,19 +11,28 @@ import (
"sync" "sync"
) )
var mimeTypes = map[string]string{ var (
".css": "text/css; charset=utf-8", mimeLock sync.RWMutex
".gif": "image/gif", mimeTypes = map[string]string{}
".htm": "text/html; charset=utf-8", mimeTypesLower = map[string]string{}
".html": "text/html; charset=utf-8", )
".jpg": "image/jpeg",
".js": "application/x-javascript",
".pdf": "application/pdf",
".png": "image/png",
".xml": "text/xml; charset=utf-8",
}
var mimeLock sync.RWMutex func init() {
mimeTypes := map[string]string{
".css": "text/css; charset=utf-8",
".gif": "image/gif",
".htm": "text/html; charset=utf-8",
".html": "text/html; charset=utf-8",
".jpg": "image/jpeg",
".js": "application/x-javascript",
".pdf": "application/pdf",
".png": "image/png",
".xml": "text/xml; charset=utf-8",
}
for ext, typ := range mimeTypes {
AddExtensionType(ext, typ)
}
}
var once sync.Once var once sync.Once
@ -31,6 +40,8 @@ var once sync.Once
// The extension ext should begin with a leading dot, as in ".html". // The extension ext should begin with a leading dot, as in ".html".
// When ext has no associated type, TypeByExtension returns "". // When ext has no associated type, TypeByExtension returns "".
// //
// Extensions are looked up first case-sensitively, then case-insensitively.
//
// The built-in table is small but on unix it is augmented by the local // The built-in table is small but on unix it is augmented by the local
// system's mime.types file(s) if available under one or more of these // system's mime.types file(s) if available under one or more of these
// names: // names:
@ -39,7 +50,7 @@ var once sync.Once
// /etc/apache2/mime.types // /etc/apache2/mime.types
// /etc/apache/mime.types // /etc/apache/mime.types
// //
// Windows system mime types are extracted from registry. // Windows system MIME types are extracted from registry.
// //
// Text types have the charset parameter set to "utf-8" by default. // Text types have the charset parameter set to "utf-8" by default.
func TypeByExtension(ext string) string { func TypeByExtension(ext string) string {
@ -47,15 +58,21 @@ func TypeByExtension(ext string) string {
mimeLock.RLock() mimeLock.RLock()
typename := mimeTypes[ext] typename := mimeTypes[ext]
mimeLock.RUnlock() mimeLock.RUnlock()
if typename == "" {
lower := strings.ToLower(ext)
mimeLock.RLock()
typename = mimeTypesLower[lower]
mimeLock.RUnlock()
}
return typename return typename
} }
// AddExtensionType sets the MIME type associated with // AddExtensionType sets the MIME type associated with
// the extension ext to typ. The extension should begin with // the extension ext to typ. The extension should begin with
// a leading dot, as in ".html". // a leading dot, as in ".html".
func AddExtensionType(ext, typ string) error { func AddExtensionType(ext, typ string) error {
if ext == "" || ext[0] != '.' { if !strings.HasPrefix(ext, ".") {
return fmt.Errorf(`mime: extension "%s" misses dot`, ext) return fmt.Errorf(`mime: extension %q misses dot`, ext)
} }
once.Do(initMime) once.Do(initMime)
return setExtensionType(ext, typ) return setExtensionType(ext, typ)
@ -70,8 +87,11 @@ func setExtensionType(extension, mimeType string) error {
param["charset"] = "utf-8" param["charset"] = "utf-8"
mimeType = FormatMediaType(mimeType, param) mimeType = FormatMediaType(mimeType, param)
} }
extLower := strings.ToLower(extension)
mimeLock.Lock() mimeLock.Lock()
mimeTypes[extension] = mimeType mimeTypes[extension] = mimeType
mimeTypesLower[extLower] = mimeType
mimeLock.Unlock() mimeLock.Unlock()
return nil return nil
} }

View File

@ -48,6 +48,6 @@ func initMimeForTests() map[string]string {
return map[string]string{ return map[string]string{
".t1": "application/test", ".t1": "application/test",
".t2": "text/test; charset=utf-8", ".t2": "text/test; charset=utf-8",
".png": "image/png", ".pNg": "image/png",
} }
} }

View File

@ -4,7 +4,9 @@
package mime package mime
import "testing" import (
"testing"
)
var typeTests = initMimeForTests() var typeTests = initMimeForTests()
@ -14,16 +16,20 @@ func TestTypeByExtension(t *testing.T) {
if val != want { if val != want {
t.Errorf("TypeByExtension(%q) = %q, want %q", ext, val, want) t.Errorf("TypeByExtension(%q) = %q, want %q", ext, val, want)
} }
} }
} }
func TestCustomExtension(t *testing.T) { func TestCustomExtension(t *testing.T) {
custom := "text/xml; charset=iso-8859-1" custom := "test/test; charset=iso-8859-1"
if error := AddExtensionType(".xml", custom); error != nil { if error := AddExtensionType(".tesT", custom); error != nil {
t.Fatalf("error %s for AddExtension(%s)", error, custom) t.Fatalf("error %s for AddExtension(%s)", error, custom)
} }
if registered := TypeByExtension(".xml"); registered != custom { // test with same capitalization
if registered := TypeByExtension(".tesT"); registered != custom {
t.Fatalf("registered %s instead of %s", registered, custom)
}
// test with different capitalization
if registered := TypeByExtension(".Test"); registered != custom {
t.Fatalf("registered %s instead of %s", registered, custom) t.Fatalf("registered %s instead of %s", registered, custom)
} }
} }

View File

@ -53,7 +53,7 @@ func initMime() {
func initMimeForTests() map[string]string { func initMimeForTests() map[string]string {
typeFiles = []string{"testdata/test.types"} typeFiles = []string{"testdata/test.types"}
return map[string]string{ return map[string]string{
".t1": "application/test", ".T1": "application/test",
".t2": "text/test; charset=utf-8", ".t2": "text/test; charset=utf-8",
".png": "image/png", ".png": "image/png",
} }

View File

@ -58,6 +58,6 @@ func initMime() {
func initMimeForTests() map[string]string { func initMimeForTests() map[string]string {
return map[string]string{ return map[string]string{
".png": "image/png", ".PnG": "image/png",
} }
} }