import (
"bufio"
+ "fmt"
"os"
"strings"
"sync"
}
var mimeTypes = map[string]string{
- ".css": "text/css; charset=utf-8",
+ ".css": "text/css;charset=utf-8",
".gif": "image/gif",
- ".htm": "text/html; charset=utf-8",
- ".html": "text/html; charset=utf-8",
+ ".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",
+ ".xml": "text/xml;charset=utf-8",
}
var mimeLock sync.RWMutex
if len(fields) <= 1 || fields[0][0] == '#' {
continue
}
- typename := fields[0]
- if strings.HasPrefix(typename, "text/") {
- typename += "; charset=utf-8"
- }
+ mimeType := fields[0]
for _, ext := range fields[1:] {
if ext[0] == '#' {
break
}
- mimeTypes["."+ext] = typename
+ setExtensionType("."+ext, mimeType)
}
}
}
// /etc/mime.types
// /etc/apache2/mime.types
// /etc/apache/mime.types
+//
+// Text types have the charset parameter set to "utf-8" by default.
func TypeByExtension(ext string) string {
once.Do(initMime)
mimeLock.RLock()
// the extension ext to typ. The extension should begin with
// a leading dot, as in ".html".
func AddExtensionType(ext, typ string) os.Error {
+ if ext == "" || ext[0] != '.' {
+ return fmt.Errorf(`mime: extension "%s" misses dot`, ext)
+ }
once.Do(initMime)
- if len(ext) < 1 || ext[0] != '.' {
- return os.EINVAL
+ return setExtensionType(ext, typ)
+}
+
+func setExtensionType(extension, mimeType string) os.Error {
+ full, param, err := ParseMediaType(mimeType)
+ if err != nil {
+ return err
+ }
+ if split := strings.Index(full, "/"); split < 0 {
+ return fmt.Errorf(`mime: malformed MIME type "%s"`, mimeType)
+ } else {
+ main := full[:split]
+ sub := full[split+1:]
+ if main == "text" && param["charset"] == "" {
+ param["charset"] = "utf-8"
+ }
+ mimeType = FormatMediaType(main, sub, param)
}
+
mimeLock.Lock()
- mimeTypes[ext] = typ
+ mimeTypes[extension] = mimeType
mimeLock.Unlock()
return nil
}
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-// Tests for type.go
-
package mime
import "testing"
var typeTests = map[string]string{
".t1": "application/test",
- ".t2": "text/test; charset=utf-8",
+ ".t2": "text/test;charset=utf-8",
".png": "image/png",
}
-func TestType(t *testing.T) {
+func TestTypeByExtension(t *testing.T) {
typeFiles = []string{"test.types"}
for ext, want := range typeTests {
}
}
+
+func TestCustomExtension(t *testing.T) {
+ custom := "text/xml;charset=iso-8859-1"
+ if error := AddExtensionType(".xml", custom); error != nil {
+ t.Fatalf("error %s for AddExtension(%s)", error, custom)
+ }
+ if registered := TypeByExtension(".xml"); registered != custom {
+ t.Fatalf("registered %s instead of %s", registered, custom)
+ }
+}