Source file src/pkg/mime/type_unix.go
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 // +build darwin freebsd linux netbsd openbsd plan9
6
7 package mime
8
9 import (
10 "bufio"
11 "os"
12 "strings"
13 )
14
15 var typeFiles = []string{
16 "/etc/mime.types",
17 "/etc/apache2/mime.types",
18 "/etc/apache/mime.types",
19 }
20
21 func loadMimeFile(filename string) {
22 f, err := os.Open(filename)
23 if err != nil {
24 return
25 }
26
27 reader := bufio.NewReader(f)
28 for {
29 line, err := reader.ReadString('\n')
30 if err != nil {
31 f.Close()
32 return
33 }
34 fields := strings.Fields(line)
35 if len(fields) <= 1 || fields[0][0] == '#' {
36 continue
37 }
38 mimeType := fields[0]
39 for _, ext := range fields[1:] {
40 if ext[0] == '#' {
41 break
42 }
43 setExtensionType("."+ext, mimeType)
44 }
45 }
46 }
47
48 func initMime() {
49 for _, filename := range typeFiles {
50 loadMimeFile(filename)
51 }
52 }
53
54 func initMimeForTests() map[string]string {
55 typeFiles = []string{"test.types"}
56 return map[string]string{
57 ".t1": "application/test",
58 ".t2": "text/test; charset=utf-8",
59 ".png": "image/png",
60 }
61 }