Alex Brainman | ac17fd4 | 2011-11-18 10:07:36 +1100 | [diff] [blame] | 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 | |
Dave Cheney | 7c8280c | 2014-02-25 09:47:42 -0500 | [diff] [blame] | 5 | // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris |
Alex Brainman | a462816 | 2011-12-21 16:57:44 +1100 | [diff] [blame] | 6 | |
Alex Brainman | ac17fd4 | 2011-11-18 10:07:36 +1100 | [diff] [blame] | 7 | package mime |
| 8 | |
| 9 | import ( |
| 10 | "bufio" |
| 11 | "os" |
| 12 | "strings" |
| 13 | ) |
| 14 | |
Brad Fitzpatrick | b86f393 | 2015-03-29 21:21:15 +0200 | [diff] [blame] | 15 | func init() { |
| 16 | osInitMime = initMimeUnix |
| 17 | } |
| 18 | |
Alex Brainman | ac17fd4 | 2011-11-18 10:07:36 +1100 | [diff] [blame] | 19 | var typeFiles = []string{ |
| 20 | "/etc/mime.types", |
| 21 | "/etc/apache2/mime.types", |
| 22 | "/etc/apache/mime.types", |
| 23 | } |
| 24 | |
| 25 | func loadMimeFile(filename string) { |
| 26 | f, err := os.Open(filename) |
| 27 | if err != nil { |
| 28 | return |
| 29 | } |
Rob Pike | 35367cc | 2013-02-20 14:34:03 -0800 | [diff] [blame] | 30 | defer f.Close() |
Alex Brainman | ac17fd4 | 2011-11-18 10:07:36 +1100 | [diff] [blame] | 31 | |
Rob Pike | 35367cc | 2013-02-20 14:34:03 -0800 | [diff] [blame] | 32 | scanner := bufio.NewScanner(f) |
| 33 | for scanner.Scan() { |
| 34 | fields := strings.Fields(scanner.Text()) |
Alex Brainman | ac17fd4 | 2011-11-18 10:07:36 +1100 | [diff] [blame] | 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 | } |
Rob Pike | 35367cc | 2013-02-20 14:34:03 -0800 | [diff] [blame] | 46 | if err := scanner.Err(); err != nil { |
| 47 | panic(err) |
| 48 | } |
Alex Brainman | ac17fd4 | 2011-11-18 10:07:36 +1100 | [diff] [blame] | 49 | } |
| 50 | |
Brad Fitzpatrick | b86f393 | 2015-03-29 21:21:15 +0200 | [diff] [blame] | 51 | func initMimeUnix() { |
Alex Brainman | ac17fd4 | 2011-11-18 10:07:36 +1100 | [diff] [blame] | 52 | for _, filename := range typeFiles { |
| 53 | loadMimeFile(filename) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func initMimeForTests() map[string]string { |
Robin Eklind | a123183 | 2013-01-22 13:44:35 -0800 | [diff] [blame] | 58 | typeFiles = []string{"testdata/test.types"} |
Alex Brainman | ac17fd4 | 2011-11-18 10:07:36 +1100 | [diff] [blame] | 59 | return map[string]string{ |
Jeff R. Allen | af12dc5 | 2014-08-28 08:22:54 -0700 | [diff] [blame] | 60 | ".T1": "application/test", |
Alex Brainman | ac17fd4 | 2011-11-18 10:07:36 +1100 | [diff] [blame] | 61 | ".t2": "text/test; charset=utf-8", |
| 62 | ".png": "image/png", |
| 63 | } |
| 64 | } |