Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [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 | |
Nigel Tao | 6a186d3 | 2011-04-20 09:57:05 +1000 | [diff] [blame] | 5 | // Package mime implements parts of the MIME spec. |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 6 | package mime |
| 7 | |
| 8 | import ( |
| 9 | "bufio" |
Pascal S. de Kloe | bd3627c | 2011-08-26 16:55:25 -0400 | [diff] [blame] | 10 | "fmt" |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 11 | "os" |
| 12 | "strings" |
Yuusei Kuwana | c21e2f3 | 2010-07-29 14:12:04 -0700 | [diff] [blame] | 13 | "sync" |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | var typeFiles = []string{ |
| 17 | "/etc/mime.types", |
| 18 | "/etc/apache2/mime.types", |
| 19 | "/etc/apache/mime.types", |
| 20 | } |
| 21 | |
| 22 | var mimeTypes = map[string]string{ |
Russ Cox | 91643ac | 2011-08-26 17:19:52 -0400 | [diff] [blame] | 23 | ".css": "text/css; charset=utf-8", |
Robert Griesemer | f44fa9b | 2010-03-02 13:46:51 -0800 | [diff] [blame] | 24 | ".gif": "image/gif", |
Russ Cox | 91643ac | 2011-08-26 17:19:52 -0400 | [diff] [blame] | 25 | ".htm": "text/html; charset=utf-8", |
| 26 | ".html": "text/html; charset=utf-8", |
Robert Griesemer | f44fa9b | 2010-03-02 13:46:51 -0800 | [diff] [blame] | 27 | ".jpg": "image/jpeg", |
| 28 | ".js": "application/x-javascript", |
| 29 | ".pdf": "application/pdf", |
| 30 | ".png": "image/png", |
Russ Cox | 91643ac | 2011-08-26 17:19:52 -0400 | [diff] [blame] | 31 | ".xml": "text/xml; charset=utf-8", |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Yuusei Kuwana | c21e2f3 | 2010-07-29 14:12:04 -0700 | [diff] [blame] | 34 | var mimeLock sync.RWMutex |
| 35 | |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 36 | func loadMimeFile(filename string) { |
Rob Pike | 8a90fd3 | 2011-04-04 23:42:14 -0700 | [diff] [blame] | 37 | f, err := os.Open(filename) |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 38 | if err != nil { |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | reader := bufio.NewReader(f) |
| 43 | for { |
| 44 | line, err := reader.ReadString('\n') |
| 45 | if err != nil { |
| 46 | f.Close() |
| 47 | return |
| 48 | } |
| 49 | fields := strings.Fields(line) |
| 50 | if len(fields) <= 1 || fields[0][0] == '#' { |
| 51 | continue |
| 52 | } |
Pascal S. de Kloe | bd3627c | 2011-08-26 16:55:25 -0400 | [diff] [blame] | 53 | mimeType := fields[0] |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 54 | for _, ext := range fields[1:] { |
| 55 | if ext[0] == '#' { |
| 56 | break |
| 57 | } |
Pascal S. de Kloe | bd3627c | 2011-08-26 16:55:25 -0400 | [diff] [blame] | 58 | setExtensionType("."+ext, mimeType) |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func initMime() { |
| 64 | for _, filename := range typeFiles { |
| 65 | loadMimeFile(filename) |
| 66 | } |
| 67 | } |
| 68 | |
Rob Pike | c78be46 | 2010-08-06 06:14:41 +1000 | [diff] [blame] | 69 | var once sync.Once |
| 70 | |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 71 | // TypeByExtension returns the MIME type associated with the file extension ext. |
| 72 | // The extension ext should begin with a leading dot, as in ".html". |
| 73 | // When ext has no associated type, TypeByExtension returns "". |
Brad Fitzpatrick | 9b64fef | 2010-07-14 17:26:14 -0700 | [diff] [blame] | 74 | // |
| 75 | // The built-in table is small but is is augmented by the local |
| 76 | // system's mime.types file(s) if available under one or more of these |
| 77 | // names: |
| 78 | // |
| 79 | // /etc/mime.types |
| 80 | // /etc/apache2/mime.types |
| 81 | // /etc/apache/mime.types |
Pascal S. de Kloe | bd3627c | 2011-08-26 16:55:25 -0400 | [diff] [blame] | 82 | // |
| 83 | // Text types have the charset parameter set to "utf-8" by default. |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 84 | func TypeByExtension(ext string) string { |
| 85 | once.Do(initMime) |
Yuusei Kuwana | c21e2f3 | 2010-07-29 14:12:04 -0700 | [diff] [blame] | 86 | mimeLock.RLock() |
| 87 | typename := mimeTypes[ext] |
| 88 | mimeLock.RUnlock() |
| 89 | return typename |
| 90 | } |
| 91 | |
| 92 | // AddExtensionType sets the MIME type associated with |
| 93 | // the extension ext to typ. The extension should begin with |
| 94 | // a leading dot, as in ".html". |
| 95 | func AddExtensionType(ext, typ string) os.Error { |
Pascal S. de Kloe | bd3627c | 2011-08-26 16:55:25 -0400 | [diff] [blame] | 96 | if ext == "" || ext[0] != '.' { |
| 97 | return fmt.Errorf(`mime: extension "%s" misses dot`, ext) |
Yuusei Kuwana | c21e2f3 | 2010-07-29 14:12:04 -0700 | [diff] [blame] | 98 | } |
Pascal S. de Kloe | bd3627c | 2011-08-26 16:55:25 -0400 | [diff] [blame] | 99 | once.Do(initMime) |
| 100 | return setExtensionType(ext, typ) |
| 101 | } |
| 102 | |
| 103 | func setExtensionType(extension, mimeType string) os.Error { |
| 104 | full, param, err := ParseMediaType(mimeType) |
| 105 | if err != nil { |
| 106 | return err |
| 107 | } |
| 108 | if split := strings.Index(full, "/"); split < 0 { |
| 109 | return fmt.Errorf(`mime: malformed MIME type "%s"`, mimeType) |
| 110 | } else { |
| 111 | main := full[:split] |
| 112 | sub := full[split+1:] |
| 113 | if main == "text" && param["charset"] == "" { |
| 114 | param["charset"] = "utf-8" |
| 115 | } |
| 116 | mimeType = FormatMediaType(main, sub, param) |
| 117 | } |
| 118 | |
Yuusei Kuwana | c21e2f3 | 2010-07-29 14:12:04 -0700 | [diff] [blame] | 119 | mimeLock.Lock() |
Pascal S. de Kloe | bd3627c | 2011-08-26 16:55:25 -0400 | [diff] [blame] | 120 | mimeTypes[extension] = mimeType |
Yuusei Kuwana | c21e2f3 | 2010-07-29 14:12:04 -0700 | [diff] [blame] | 121 | mimeLock.Unlock() |
| 122 | return nil |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 123 | } |