"sync"
)
-var 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",
-}
+var (
+ mimeLock sync.RWMutex
+ mimeTypes = map[string]string{}
+ mimeTypesLower = map[string]string{}
+)
-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
// The extension ext should begin with a leading dot, as in ".html".
// 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
// system's mime.types file(s) if available under one or more of these
// names:
// /etc/apache2/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.
func TypeByExtension(ext string) string {
mimeLock.RLock()
typename := mimeTypes[ext]
mimeLock.RUnlock()
+ if typename == "" {
+ lower := strings.ToLower(ext)
+ mimeLock.RLock()
+ typename = mimeTypesLower[lower]
+ mimeLock.RUnlock()
+ }
return typename
}
// 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".
func AddExtensionType(ext, typ string) error {
- if ext == "" || ext[0] != '.' {
- return fmt.Errorf(`mime: extension "%s" misses dot`, ext)
+ if !strings.HasPrefix(ext, ".") {
+ return fmt.Errorf(`mime: extension %q misses dot`, ext)
}
once.Do(initMime)
return setExtensionType(ext, typ)
param["charset"] = "utf-8"
mimeType = FormatMediaType(mimeType, param)
}
+ extLower := strings.ToLower(extension)
+
mimeLock.Lock()
mimeTypes[extension] = mimeType
+ mimeTypesLower[extLower] = mimeType
mimeLock.Unlock()
return nil
}
package mime
-import "testing"
+import (
+ "testing"
+)
var typeTests = initMimeForTests()
if val != want {
t.Errorf("TypeByExtension(%q) = %q, want %q", ext, val, want)
}
-
}
}
func TestCustomExtension(t *testing.T) {
- custom := "text/xml; charset=iso-8859-1"
- if error := AddExtensionType(".xml", custom); error != nil {
+ custom := "test/test; charset=iso-8859-1"
+ if error := AddExtensionType(".tesT", custom); error != nil {
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)
}
}