Skip to content

Commit dcda643

Browse files
committed
src/mime: Use os-generated mimetypes on unix systems
Related to bug on Solus bugtracker: https://dev.getsol.us/T9394 Mimetypes was incorrectly being looked up from custom mime.types files that were not generated by the systems. This changes the mimetype loading and parsing on unix systems (except for darwin, see note below) to use the os-generated mimetypes instead. It makes mimetype lookup work on systems that do not have those custom files and allows us to drop the workarounds for FreeBSD, DragonflyBSD and OpenBSD. Mimetype lookup will now also work on Solus, a Linux distrubution. The old lookup is used only for darwin, where the globs file is not present in later releases.
1 parent 49dccf1 commit dcda643

File tree

7 files changed

+87
-44
lines changed

7 files changed

+87
-44
lines changed

src/mime/testdata/test.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
# license that can be found in the LICENSE file.
44

55

6-
# mime package test
7-
application/test t1 # Simple test
8-
text/test t2 # Text test
6+
# mime package test
7+
application/test:*.t1
8+
text/test:*.t2

src/mime/testdata/test.types.darwin

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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.
4+
5+
6+
# mime package test
7+
application/test t1 # Simple test
8+
text/test t2 # Text test

src/mime/type_darwin.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2021 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.
4+
5+
//go:build darwin
6+
// +build darwin
7+
8+
package mime
9+
10+
import (
11+
"bufio"
12+
"os"
13+
"strings"
14+
)
15+
16+
func init() {
17+
osInitMime = initMimeDarwin
18+
}
19+
20+
var typeFiles = []string{
21+
"/etc/apache2/mime.types",
22+
}
23+
24+
func loadMimeFile(filename string) {
25+
f, err := os.Open(filename)
26+
if err != nil {
27+
return
28+
}
29+
defer f.Close()
30+
31+
scanner := bufio.NewScanner(f)
32+
for scanner.Scan() {
33+
fields := strings.Fields(scanner.Text())
34+
if len(fields) <= 1 || fields[0][0] == '#' {
35+
continue
36+
}
37+
mimeType := fields[0]
38+
for _, ext := range fields[1:] {
39+
if ext[0] == '#' {
40+
break
41+
}
42+
setExtensionType("."+ext, mimeType)
43+
}
44+
}
45+
if err := scanner.Err(); err != nil {
46+
panic(err)
47+
}
48+
}
49+
50+
func initMimeDarwin() {
51+
for _, filename := range typeFiles {
52+
loadMimeFile(filename)
53+
}
54+
}
55+
56+
func initMimeForTests() map[string]string {
57+
typeFiles = []string{"testdata/test.types.darwin"}
58+
return map[string]string{
59+
".T1": "application/test",
60+
".t2": "text/test; charset=utf-8",
61+
".png": "image/png",
62+
}
63+
}

src/mime/type_dragonfly.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/mime/type_freebsd.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/mime/type_openbsd.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/mime/type_unix.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris
6-
// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris
5+
//go:build aix || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris
6+
// +build aix dragonfly freebsd js,wasm linux netbsd openbsd solaris
77

88
package mime
99

@@ -18,10 +18,8 @@ func init() {
1818
}
1919

2020
var typeFiles = []string{
21-
"/etc/mime.types",
22-
"/etc/apache2/mime.types",
23-
"/etc/apache/mime.types",
24-
"/etc/httpd/conf/mime.types",
21+
"/usr/local/share/mime/globs",
22+
"/usr/share/mime/globs", // Fallback for unix systems that don't use /usr/local.
2523
}
2624

2725
func loadMimeFile(filename string) {
@@ -32,18 +30,19 @@ func loadMimeFile(filename string) {
3230
defer f.Close()
3331

3432
scanner := bufio.NewScanner(f)
33+
scanner.Split(bufio.ScanLines)
3534
for scanner.Scan() {
36-
fields := strings.Fields(scanner.Text())
37-
if len(fields) <= 1 || fields[0][0] == '#' {
35+
if scanner.Text() == "" || scanner.Text()[0] == '#' {
3836
continue
3937
}
40-
mimeType := fields[0]
41-
for _, ext := range fields[1:] {
42-
if ext[0] == '#' {
43-
break
44-
}
45-
setExtensionType("."+ext, mimeType)
38+
39+
// Each line should be of format: mimetype:*.ext
40+
fields := strings.Split(scanner.Text(), ":")
41+
if fields[1][0] != '*' {
42+
continue // We only support getting mimetypes for extensions, not for filenames.
4643
}
44+
45+
setExtensionType(fields[1][1:], fields[0])
4746
}
4847
if err := scanner.Err(); err != nil {
4948
panic(err)

0 commit comments

Comments
 (0)