2010-02-09 21:47:45 -07:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2011-04-19 17:57:05 -06:00
|
|
|
// Package mime implements parts of the MIME spec.
|
2010-02-09 21:47:45 -07:00
|
|
|
package mime
|
|
|
|
|
|
|
|
import (
|
2011-08-26 14:55:25 -06:00
|
|
|
"fmt"
|
2010-02-09 21:47:45 -07:00
|
|
|
"strings"
|
2010-07-29 15:12:04 -06:00
|
|
|
"sync"
|
2010-02-09 21:47:45 -07:00
|
|
|
)
|
|
|
|
|
2014-08-28 09:22:54 -06:00
|
|
|
var (
|
|
|
|
mimeLock sync.RWMutex
|
2014-08-28 12:07:46 -06:00
|
|
|
mimeTypesLower = map[string]string{
|
2014-08-28 09:22:54 -06:00
|
|
|
".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",
|
|
|
|
}
|
2014-08-28 12:07:46 -06:00
|
|
|
mimeTypes = clone(mimeTypesLower)
|
|
|
|
)
|
|
|
|
|
|
|
|
func clone(m map[string]string) map[string]string {
|
|
|
|
m2 := make(map[string]string, len(m))
|
|
|
|
for k, v := range m {
|
|
|
|
m2[k] = v
|
|
|
|
if strings.ToLower(k) != k {
|
|
|
|
panic("keys in mimeTypesLower must be lowercase")
|
|
|
|
}
|
2014-08-28 09:22:54 -06:00
|
|
|
}
|
2014-08-28 12:07:46 -06:00
|
|
|
return m2
|
2014-08-28 09:22:54 -06:00
|
|
|
}
|
2010-07-29 15:12:04 -06:00
|
|
|
|
2014-08-28 12:07:46 -06:00
|
|
|
var once sync.Once // guards initMime
|
2010-08-05 14:14:41 -06:00
|
|
|
|
2010-02-09 21:47:45 -07:00
|
|
|
// TypeByExtension returns the MIME type associated with the file extension ext.
|
|
|
|
// The extension ext should begin with a leading dot, as in ".html".
|
|
|
|
// When ext has no associated type, TypeByExtension returns "".
|
2010-07-14 18:26:14 -06:00
|
|
|
//
|
2014-08-28 09:22:54 -06:00
|
|
|
// Extensions are looked up first case-sensitively, then case-insensitively.
|
|
|
|
//
|
2011-11-17 16:07:36 -07:00
|
|
|
// The built-in table is small but on unix it is augmented by the local
|
2010-07-14 18:26:14 -06:00
|
|
|
// system's mime.types file(s) if available under one or more of these
|
|
|
|
// names:
|
|
|
|
//
|
|
|
|
// /etc/mime.types
|
|
|
|
// /etc/apache2/mime.types
|
|
|
|
// /etc/apache/mime.types
|
2011-08-26 14:55:25 -06:00
|
|
|
//
|
2014-08-28 12:07:46 -06:00
|
|
|
// On Windows, MIME types are extracted from the registry.
|
2011-11-17 16:07:36 -07:00
|
|
|
//
|
2011-08-26 14:55:25 -06:00
|
|
|
// Text types have the charset parameter set to "utf-8" by default.
|
2010-02-09 21:47:45 -07:00
|
|
|
func TypeByExtension(ext string) string {
|
|
|
|
once.Do(initMime)
|
2010-07-29 15:12:04 -06:00
|
|
|
mimeLock.RLock()
|
2014-08-28 12:07:46 -06:00
|
|
|
defer mimeLock.RUnlock()
|
|
|
|
|
|
|
|
// Case-sensitive lookup.
|
|
|
|
v := mimeTypes[ext]
|
|
|
|
if v != "" {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case-insensitive lookup.
|
|
|
|
// Optimistically assume a short ASCII extension and be
|
|
|
|
// allocation-free in that case.
|
|
|
|
var buf [10]byte
|
|
|
|
lower := buf[:0]
|
|
|
|
const utf8RuneSelf = 0x80 // from utf8 package, but not importing it.
|
|
|
|
for i := 0; i < len(ext); i++ {
|
|
|
|
c := ext[i]
|
|
|
|
if c >= utf8RuneSelf {
|
|
|
|
// Slow path.
|
|
|
|
return mimeTypesLower[strings.ToLower(ext)]
|
|
|
|
}
|
|
|
|
if 'A' <= c && c <= 'Z' {
|
|
|
|
lower = append(lower, c+('a'-'A'))
|
|
|
|
} else {
|
|
|
|
lower = append(lower, c)
|
|
|
|
}
|
2014-08-28 09:22:54 -06:00
|
|
|
}
|
2014-08-28 12:07:46 -06:00
|
|
|
// The conversion from []byte to string doesn't allocate in
|
|
|
|
// a map lookup.
|
|
|
|
return mimeTypesLower[string(lower)]
|
2010-07-29 15:12:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddExtensionType sets the MIME type associated with
|
2014-08-28 09:22:54 -06:00
|
|
|
// the extension ext to typ. The extension should begin with
|
2010-07-29 15:12:04 -06:00
|
|
|
// a leading dot, as in ".html".
|
2011-11-01 20:04:37 -06:00
|
|
|
func AddExtensionType(ext, typ string) error {
|
2014-08-28 09:22:54 -06:00
|
|
|
if !strings.HasPrefix(ext, ".") {
|
|
|
|
return fmt.Errorf(`mime: extension %q misses dot`, ext)
|
2011-08-26 14:55:25 -06:00
|
|
|
}
|
2010-07-29 15:12:04 -06:00
|
|
|
once.Do(initMime)
|
2011-08-26 14:55:25 -06:00
|
|
|
return setExtensionType(ext, typ)
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:04:37 -06:00
|
|
|
func setExtensionType(extension, mimeType string) error {
|
2012-01-17 12:57:42 -07:00
|
|
|
_, param, err := ParseMediaType(mimeType)
|
2011-08-26 14:55:25 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2012-01-17 12:57:42 -07:00
|
|
|
if strings.HasPrefix(mimeType, "text/") && param["charset"] == "" {
|
|
|
|
param["charset"] = "utf-8"
|
|
|
|
mimeType = FormatMediaType(mimeType, param)
|
2010-07-29 15:12:04 -06:00
|
|
|
}
|
2014-08-28 09:22:54 -06:00
|
|
|
extLower := strings.ToLower(extension)
|
|
|
|
|
2010-07-29 15:12:04 -06:00
|
|
|
mimeLock.Lock()
|
2011-08-26 14:55:25 -06:00
|
|
|
mimeTypes[extension] = mimeType
|
2014-08-28 09:22:54 -06:00
|
|
|
mimeTypesLower[extLower] = mimeType
|
2010-07-29 15:12:04 -06:00
|
|
|
mimeLock.Unlock()
|
|
|
|
return nil
|
2010-02-09 21:47:45 -07:00
|
|
|
}
|