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 ( |
Pascal S. de Kloe | bd3627c | 2011-08-26 16:55:25 -0400 | [diff] [blame] | 9 | "fmt" |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 10 | "strings" |
Yuusei Kuwana | c21e2f3 | 2010-07-29 14:12:04 -0700 | [diff] [blame] | 11 | "sync" |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 12 | ) |
| 13 | |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 14 | var mimeTypes = map[string]string{ |
Russ Cox | 91643ac | 2011-08-26 17:19:52 -0400 | [diff] [blame] | 15 | ".css": "text/css; charset=utf-8", |
Robert Griesemer | f44fa9b | 2010-03-02 13:46:51 -0800 | [diff] [blame] | 16 | ".gif": "image/gif", |
Russ Cox | 91643ac | 2011-08-26 17:19:52 -0400 | [diff] [blame] | 17 | ".htm": "text/html; charset=utf-8", |
| 18 | ".html": "text/html; charset=utf-8", |
Robert Griesemer | f44fa9b | 2010-03-02 13:46:51 -0800 | [diff] [blame] | 19 | ".jpg": "image/jpeg", |
| 20 | ".js": "application/x-javascript", |
| 21 | ".pdf": "application/pdf", |
| 22 | ".png": "image/png", |
Russ Cox | 91643ac | 2011-08-26 17:19:52 -0400 | [diff] [blame] | 23 | ".xml": "text/xml; charset=utf-8", |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 24 | } |
| 25 | |
Yuusei Kuwana | c21e2f3 | 2010-07-29 14:12:04 -0700 | [diff] [blame] | 26 | var mimeLock sync.RWMutex |
| 27 | |
Rob Pike | c78be46 | 2010-08-06 06:14:41 +1000 | [diff] [blame] | 28 | var once sync.Once |
| 29 | |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 30 | // TypeByExtension returns the MIME type associated with the file extension ext. |
| 31 | // The extension ext should begin with a leading dot, as in ".html". |
| 32 | // When ext has no associated type, TypeByExtension returns "". |
Brad Fitzpatrick | 9b64fef | 2010-07-14 17:26:14 -0700 | [diff] [blame] | 33 | // |
Alex Brainman | ac17fd4 | 2011-11-18 10:07:36 +1100 | [diff] [blame] | 34 | // The built-in table is small but on unix it is augmented by the local |
Brad Fitzpatrick | 9b64fef | 2010-07-14 17:26:14 -0700 | [diff] [blame] | 35 | // system's mime.types file(s) if available under one or more of these |
| 36 | // names: |
| 37 | // |
| 38 | // /etc/mime.types |
| 39 | // /etc/apache2/mime.types |
| 40 | // /etc/apache/mime.types |
Pascal S. de Kloe | bd3627c | 2011-08-26 16:55:25 -0400 | [diff] [blame] | 41 | // |
Alex Brainman | ac17fd4 | 2011-11-18 10:07:36 +1100 | [diff] [blame] | 42 | // Windows system mime types are extracted from registry. |
| 43 | // |
Pascal S. de Kloe | bd3627c | 2011-08-26 16:55:25 -0400 | [diff] [blame] | 44 | // Text types have the charset parameter set to "utf-8" by default. |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 45 | func TypeByExtension(ext string) string { |
| 46 | once.Do(initMime) |
Yuusei Kuwana | c21e2f3 | 2010-07-29 14:12:04 -0700 | [diff] [blame] | 47 | mimeLock.RLock() |
| 48 | typename := mimeTypes[ext] |
| 49 | mimeLock.RUnlock() |
| 50 | return typename |
| 51 | } |
| 52 | |
| 53 | // AddExtensionType sets the MIME type associated with |
| 54 | // the extension ext to typ. The extension should begin with |
| 55 | // a leading dot, as in ".html". |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 56 | func AddExtensionType(ext, typ string) error { |
Pascal S. de Kloe | bd3627c | 2011-08-26 16:55:25 -0400 | [diff] [blame] | 57 | if ext == "" || ext[0] != '.' { |
| 58 | return fmt.Errorf(`mime: extension "%s" misses dot`, ext) |
Yuusei Kuwana | c21e2f3 | 2010-07-29 14:12:04 -0700 | [diff] [blame] | 59 | } |
Pascal S. de Kloe | bd3627c | 2011-08-26 16:55:25 -0400 | [diff] [blame] | 60 | once.Do(initMime) |
| 61 | return setExtensionType(ext, typ) |
| 62 | } |
| 63 | |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 64 | func setExtensionType(extension, mimeType string) error { |
Brad Fitzpatrick | a00de45 | 2012-01-17 11:57:42 -0800 | [diff] [blame] | 65 | _, param, err := ParseMediaType(mimeType) |
Pascal S. de Kloe | bd3627c | 2011-08-26 16:55:25 -0400 | [diff] [blame] | 66 | if err != nil { |
| 67 | return err |
| 68 | } |
Brad Fitzpatrick | a00de45 | 2012-01-17 11:57:42 -0800 | [diff] [blame] | 69 | if strings.HasPrefix(mimeType, "text/") && param["charset"] == "" { |
| 70 | param["charset"] = "utf-8" |
| 71 | mimeType = FormatMediaType(mimeType, param) |
Pascal S. de Kloe | bd3627c | 2011-08-26 16:55:25 -0400 | [diff] [blame] | 72 | } |
Yuusei Kuwana | c21e2f3 | 2010-07-29 14:12:04 -0700 | [diff] [blame] | 73 | mimeLock.Lock() |
Pascal S. de Kloe | bd3627c | 2011-08-26 16:55:25 -0400 | [diff] [blame] | 74 | mimeTypes[extension] = mimeType |
Yuusei Kuwana | c21e2f3 | 2010-07-29 14:12:04 -0700 | [diff] [blame] | 75 | mimeLock.Unlock() |
| 76 | return nil |
Michael Hoisie | 0cba5fc | 2010-02-09 20:47:45 -0800 | [diff] [blame] | 77 | } |