Commit bd3627cd authored by Pascal S. de Kloe's avatar Pascal S. de Kloe Committed by Russ Cox

mime: text charset defaults

Enforce + document the UTF-8 default.

R=rsc, bradfitz, adg
CC=golang-dev
https://golang.org/cl/4627049
parent fbdbb595
...@@ -7,6 +7,7 @@ package mime ...@@ -7,6 +7,7 @@ package mime
import ( import (
"bufio" "bufio"
"fmt"
"os" "os"
"strings" "strings"
"sync" "sync"
...@@ -19,15 +20,15 @@ var typeFiles = []string{ ...@@ -19,15 +20,15 @@ var typeFiles = []string{
} }
var mimeTypes = map[string]string{ var mimeTypes = map[string]string{
".css": "text/css; charset=utf-8", ".css": "text/css;charset=utf-8",
".gif": "image/gif", ".gif": "image/gif",
".htm": "text/html; charset=utf-8", ".htm": "text/html;charset=utf-8",
".html": "text/html; charset=utf-8", ".html": "text/html;charset=utf-8",
".jpg": "image/jpeg", ".jpg": "image/jpeg",
".js": "application/x-javascript", ".js": "application/x-javascript",
".pdf": "application/pdf", ".pdf": "application/pdf",
".png": "image/png", ".png": "image/png",
".xml": "text/xml; charset=utf-8", ".xml": "text/xml;charset=utf-8",
} }
var mimeLock sync.RWMutex var mimeLock sync.RWMutex
...@@ -49,15 +50,12 @@ func loadMimeFile(filename string) { ...@@ -49,15 +50,12 @@ func loadMimeFile(filename string) {
if len(fields) <= 1 || fields[0][0] == '#' { if len(fields) <= 1 || fields[0][0] == '#' {
continue continue
} }
typename := fields[0] mimeType := fields[0]
if strings.HasPrefix(typename, "text/") {
typename += "; charset=utf-8"
}
for _, ext := range fields[1:] { for _, ext := range fields[1:] {
if ext[0] == '#' { if ext[0] == '#' {
break break
} }
mimeTypes["."+ext] = typename setExtensionType("."+ext, mimeType)
} }
} }
} }
...@@ -81,6 +79,8 @@ var once sync.Once ...@@ -81,6 +79,8 @@ var once sync.Once
// /etc/mime.types // /etc/mime.types
// /etc/apache2/mime.types // /etc/apache2/mime.types
// /etc/apache/mime.types // /etc/apache/mime.types
//
// Text types have the charset parameter set to "utf-8" by default.
func TypeByExtension(ext string) string { func TypeByExtension(ext string) string {
once.Do(initMime) once.Do(initMime)
mimeLock.RLock() mimeLock.RLock()
...@@ -93,12 +93,31 @@ func TypeByExtension(ext string) string { ...@@ -93,12 +93,31 @@ func TypeByExtension(ext string) string {
// 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) os.Error { func AddExtensionType(ext, typ string) os.Error {
if ext == "" || ext[0] != '.' {
return fmt.Errorf(`mime: extension "%s" misses dot`, ext)
}
once.Do(initMime) once.Do(initMime)
if len(ext) < 1 || ext[0] != '.' { return setExtensionType(ext, typ)
return os.EINVAL }
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() mimeLock.Lock()
mimeTypes[ext] = typ mimeTypes[extension] = mimeType
mimeLock.Unlock() mimeLock.Unlock()
return nil return nil
} }
...@@ -2,19 +2,17 @@ ...@@ -2,19 +2,17 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Tests for type.go
package mime package mime
import "testing" import "testing"
var typeTests = map[string]string{ var typeTests = 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",
} }
func TestType(t *testing.T) { func TestTypeByExtension(t *testing.T) {
typeFiles = []string{"test.types"} typeFiles = []string{"test.types"}
for ext, want := range typeTests { for ext, want := range typeTests {
...@@ -25,3 +23,13 @@ func TestType(t *testing.T) { ...@@ -25,3 +23,13 @@ func TestType(t *testing.T) {
} }
} }
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)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment