1
0
mirror of https://github.com/golang/go synced 2024-11-25 09:07:58 -07:00

mime: text charset defaults

Enforce + document the UTF-8 default.

R=rsc, bradfitz, adg
CC=golang-dev
https://golang.org/cl/4627049
This commit is contained in:
Pascal S. de Kloe 2011-08-26 16:55:25 -04:00 committed by Russ Cox
parent fbdbb595b6
commit bd3627cd7d
2 changed files with 44 additions and 17 deletions

View File

@ -7,6 +7,7 @@ package mime
import (
"bufio"
"fmt"
"os"
"strings"
"sync"
@ -19,15 +20,15 @@ var typeFiles = []string{
}
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
@ -49,15 +50,12 @@ func loadMimeFile(filename string) {
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)
}
}
}
@ -81,6 +79,8 @@ var once sync.Once
// /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()
@ -93,12 +93,31 @@ func TypeByExtension(ext string) string {
// the extension ext to typ. The extension should begin with
// a leading dot, as in ".html".
func AddExtensionType(ext, typ string) os.Error {
once.Do(initMime)
if len(ext) < 1 || ext[0] != '.' {
return os.EINVAL
if ext == "" || ext[0] != '.' {
return fmt.Errorf(`mime: extension "%s" misses dot`, ext)
}
once.Do(initMime)
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
}

View File

@ -2,19 +2,17 @@
// 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 {
@ -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)
}
}