OSDN Git Service

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