Commit c21e2f39 authored by Yuusei Kuwana's avatar Yuusei Kuwana Committed by Russ Cox

mime: add AddExtensionType

For example:
mime.AddExtensionType(".m3u8", "application/x-mpegURL")
mime.AddExtensionType(".ts", "video/MP2T")

R=rsc, rsc1
CC=golang-dev
https://golang.org/cl/1698046
parent 2ef1c6e2
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"once" "once"
"os" "os"
"strings" "strings"
"sync"
) )
var typeFiles = []string{ var typeFiles = []string{
...@@ -30,6 +31,8 @@ var mimeTypes = map[string]string{ ...@@ -30,6 +31,8 @@ var mimeTypes = map[string]string{
".xml": "text/xml; charset=utf-8", ".xml": "text/xml; charset=utf-8",
} }
var mimeLock sync.RWMutex
func loadMimeFile(filename string) { func loadMimeFile(filename string) {
f, err := os.Open(filename, os.O_RDONLY, 0666) f, err := os.Open(filename, os.O_RDONLY, 0666)
if err != nil { if err != nil {
...@@ -79,5 +82,22 @@ func initMime() { ...@@ -79,5 +82,22 @@ func initMime() {
// /etc/apache/mime.types // /etc/apache/mime.types
func TypeByExtension(ext string) string { func TypeByExtension(ext string) string {
once.Do(initMime) once.Do(initMime)
return mimeTypes[ext] mimeLock.RLock()
typename := mimeTypes[ext]
mimeLock.RUnlock()
return typename
}
// AddExtensionType sets the MIME type associated with
// 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
}
mimeLock.Lock()
mimeTypes[ext] = typ
mimeLock.Unlock()
return nil
} }
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