1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // The mime package implements parts of the MIME spec.
15 var typeFiles
= []string{
17 "/etc/apache2/mime.types",
18 "/etc/apache/mime.types",
21 var mimeTypes
= map[string]string{
24 ".htm": "text/html; charset=utf-8",
25 ".html": "text/html; charset=utf-8",
27 ".js": "application/x-javascript",
28 ".pdf": "application/pdf",
30 ".xml": "text/xml; charset=utf-8",
33 var mimeLock sync
.RWMutex
35 func loadMimeFile(filename
string) {
36 f
, err
:= os
.Open(filename
, os
.O_RDONLY
, 0666)
41 reader
:= bufio
.NewReader(f
)
43 line
, err
:= reader
.ReadString('\n')
48 fields
:= strings
.Fields(line
)
49 if len(fields
) <= 1 || fields
[0][0] == '#' {
53 if strings
.HasPrefix(typename
, "text/") {
54 typename
+= "; charset=utf-8"
56 for _
, ext
:= range fields
[1:] {
60 mimeTypes
["."+ext
] = typename
66 for _
, filename
:= range typeFiles
{
67 loadMimeFile(filename
)
73 // TypeByExtension returns the MIME type associated with the file extension ext.
74 // The extension ext should begin with a leading dot, as in ".html".
75 // When ext has no associated type, TypeByExtension returns "".
77 // The built-in table is small but is is augmented by the local
78 // system's mime.types file(s) if available under one or more of these
82 // /etc/apache2/mime.types
83 // /etc/apache/mime.types
84 func TypeByExtension(ext
string) string {
87 typename
:= mimeTypes
[ext
]
92 // AddExtensionType sets the MIME type associated with
93 // the extension ext to typ. The extension should begin with
94 // a leading dot, as in ".html".
95 func AddExtensionType(ext
, typ
string) os
.Error
{
97 if len(ext
) < 1 || ext
[0] != '.' {