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 // Package mime implements parts of the MIME spec.
16 mimeTypes sync.Map // map[string]string; ".Z" => "application/x-compress"
17 mimeTypesLower sync.Map // map[string]string; ".z" => "application/x-compress"
19 // extensions maps from MIME type to list of lowercase file
20 // extensions: "image/jpeg" => [".jpg", ".jpeg"]
21 extensionsMu sync.Mutex // Guards stores (but not loads) on extensions.
22 extensions sync.Map // map[string][]string; slice values are append-only.
25 func clearSyncMap(m *sync.Map) {
26 m.Range(func(k, _ any) bool {
32 // setMimeTypes is used by initMime's non-test path, and by tests.
33 func setMimeTypes(lowerExt, mixExt map[string]string) {
34 clearSyncMap(&mimeTypes)
35 clearSyncMap(&mimeTypesLower)
36 clearSyncMap(&extensions)
38 for k, v := range lowerExt {
39 mimeTypesLower.Store(k, v)
41 for k, v := range mixExt {
46 defer extensionsMu.Unlock()
47 for k, v := range lowerExt {
48 justType, _, err := ParseMediaType(v)
53 if ei, ok := extensions.Load(justType); ok {
56 extensions.Store(justType, append(exts, k))
60 var builtinTypesLower = map[string]string{
61 ".avif": "image/avif",
62 ".css": "text/css; charset=utf-8",
64 ".htm": "text/html; charset=utf-8",
65 ".html": "text/html; charset=utf-8",
66 ".jpeg": "image/jpeg",
68 ".js": "text/javascript; charset=utf-8",
69 ".json": "application/json",
70 ".mjs": "text/javascript; charset=utf-8",
71 ".pdf": "application/pdf",
73 ".svg": "image/svg+xml",
74 ".wasm": "application/wasm",
75 ".webp": "image/webp",
76 ".xml": "text/xml; charset=utf-8",
79 var once sync.Once // guards initMime
81 var testInitMime, osInitMime func()
84 if fn := testInitMime; fn != nil {
87 setMimeTypes(builtinTypesLower, builtinTypesLower)
92 // TypeByExtension returns the MIME type associated with the file extension ext.
93 // The extension ext should begin with a leading dot, as in ".html".
94 // When ext has no associated type, TypeByExtension returns "".
96 // Extensions are looked up first case-sensitively, then case-insensitively.
98 // The built-in table is small but on unix it is augmented by the local
99 // system's MIME-info database or mime.types file(s) if available under one or
100 // more of these names:
102 // /usr/local/share/mime/globs2
103 // /usr/share/mime/globs2
105 // /etc/apache2/mime.types
106 // /etc/apache/mime.types
108 // On Windows, MIME types are extracted from the registry.
110 // Text types have the charset parameter set to "utf-8" by default.
111 func TypeByExtension(ext string) string {
114 // Case-sensitive lookup.
115 if v, ok := mimeTypes.Load(ext); ok {
119 // Case-insensitive lookup.
120 // Optimistically assume a short ASCII extension and be
121 // allocation-free in that case.
124 const utf8RuneSelf = 0x80 // from utf8 package, but not importing it.
125 for i := 0; i < len(ext); i++ {
127 if c >= utf8RuneSelf {
129 si, _ := mimeTypesLower.Load(strings.ToLower(ext))
133 if 'A' <= c && c <= 'Z' {
134 lower = append(lower, c+('a'-'A'))
136 lower = append(lower, c)
139 si, _ := mimeTypesLower.Load(string(lower))
144 // ExtensionsByType returns the extensions known to be associated with the MIME
145 // type typ. The returned extensions will each begin with a leading dot, as in
146 // ".html". When typ has no associated extensions, ExtensionsByType returns an
148 func ExtensionsByType(typ string) ([]string, error) {
149 justType, _, err := ParseMediaType(typ)
155 s, ok := extensions.Load(justType)
159 ret := append([]string(nil), s.([]string)...)
164 // AddExtensionType sets the MIME type associated with
165 // the extension ext to typ. The extension should begin with
166 // a leading dot, as in ".html".
167 func AddExtensionType(ext, typ string) error {
168 if !strings.HasPrefix(ext, ".") {
169 return fmt.Errorf("mime: extension %q missing leading dot", ext)
172 return setExtensionType(ext, typ)
175 func setExtensionType(extension, mimeType string) error {
176 justType, param, err := ParseMediaType(mimeType)
180 if strings.HasPrefix(mimeType, "text/") && param["charset"] == "" {
181 param["charset"] = "utf-8"
182 mimeType = FormatMediaType(mimeType, param)
184 extLower := strings.ToLower(extension)
186 mimeTypes.Store(extension, mimeType)
187 mimeTypesLower.Store(extLower, mimeType)
190 defer extensionsMu.Unlock()
192 if ei, ok := extensions.Load(justType); ok {
195 for _, v := range exts {
200 extensions.Store(justType, append(exts, extLower))