blob: bb06a77c458566669e3de43761f18e3e104e30d7 [file] [log] [blame]
Alex Brainmanac17fd42011-11-18 10:07:36 +11001// 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 Cheney7c8280c2014-02-25 09:47:42 -05005// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
Alex Brainmana4628162011-12-21 16:57:44 +11006
Alex Brainmanac17fd42011-11-18 10:07:36 +11007package mime
8
9import (
10 "bufio"
11 "os"
12 "strings"
13)
14
Brad Fitzpatrickb86f3932015-03-29 21:21:15 +020015func init() {
16 osInitMime = initMimeUnix
17}
18
Alex Brainmanac17fd42011-11-18 10:07:36 +110019var typeFiles = []string{
20 "/etc/mime.types",
21 "/etc/apache2/mime.types",
22 "/etc/apache/mime.types",
23}
24
25func loadMimeFile(filename string) {
26 f, err := os.Open(filename)
27 if err != nil {
28 return
29 }
Rob Pike35367cc2013-02-20 14:34:03 -080030 defer f.Close()
Alex Brainmanac17fd42011-11-18 10:07:36 +110031
Rob Pike35367cc2013-02-20 14:34:03 -080032 scanner := bufio.NewScanner(f)
33 for scanner.Scan() {
34 fields := strings.Fields(scanner.Text())
Alex Brainmanac17fd42011-11-18 10:07:36 +110035 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 Pike35367cc2013-02-20 14:34:03 -080046 if err := scanner.Err(); err != nil {
47 panic(err)
48 }
Alex Brainmanac17fd42011-11-18 10:07:36 +110049}
50
Brad Fitzpatrickb86f3932015-03-29 21:21:15 +020051func initMimeUnix() {
Alex Brainmanac17fd42011-11-18 10:07:36 +110052 for _, filename := range typeFiles {
53 loadMimeFile(filename)
54 }
55}
56
57func initMimeForTests() map[string]string {
Robin Eklinda1231832013-01-22 13:44:35 -080058 typeFiles = []string{"testdata/test.types"}
Alex Brainmanac17fd42011-11-18 10:07:36 +110059 return map[string]string{
Jeff R. Allenaf12dc52014-08-28 08:22:54 -070060 ".T1": "application/test",
Alex Brainmanac17fd42011-11-18 10:07:36 +110061 ".t2": "text/test; charset=utf-8",
62 ".png": "image/png",
63 }
64}