Skip to content

Commit 08afc50

Browse files
AidanWelchgopherbot
authored andcommitted
mime: extend "builtinTypes" to include a more complete list of common types
Implement all agreed upon types, using IANA's listed media types to decide when there is a disagreement in type. Except in the case of `.wav` where `audio/wav` is used. Fixes #69530 Change-Id: Iec99a6ceb534073be83c8390f48799bec3e4cfc7 GitHub-Last-Rev: e314c5e GitHub-Pull-Request: #69533 Reviewed-on: https://go-review.googlesource.com/c/go/+/614376 Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Emmanuel Odeke <[email protected]> Auto-Submit: Sean Liao <[email protected]> Reviewed-by: Sean Liao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Carlos Amedee <[email protected]>
1 parent 97da068 commit 08afc50

File tree

2 files changed

+121
-18
lines changed

2 files changed

+121
-18
lines changed

src/mime/type.go

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var (
1717
mimeTypesLower sync.Map // map[string]string; ".z" => "application/x-compress"
1818

1919
// extensions maps from MIME type to list of lowercase file
20-
// extensions: "image/jpeg" => [".jpg", ".jpeg"]
20+
// extensions: "image/jpeg" => [".jfif", ".jpg", ".jpeg", ".pjp", ".pjpeg"]
2121
extensionsMu sync.Mutex // Guards stores (but not loads) on extensions.
2222
extensions sync.Map // map[string][]string; slice values are append-only.
2323
)
@@ -50,23 +50,82 @@ func setMimeTypes(lowerExt, mixExt map[string]string) {
5050
}
5151
}
5252

53+
// A type is listed here if both Firefox and Chrome included them in their own
54+
// lists. In the case where they contradict they are deconflicted using IANA's
55+
// listed media types https://www.iana.org/assignments/media-types/media-types.xhtml
56+
//
57+
// Chrome's MIME mappings to file extensions are defined at
58+
// https://chromium.googlesource.com/chromium/src.git/+/refs/heads/main/net/base/mime_util.cc
59+
//
60+
// Firefox's MIME types can be found at
61+
// https://github.com/mozilla-firefox/firefox/blob/main/netwerk/mime/nsMimeTypes.h
62+
// and the mappings to file extensions at
63+
// https://github.com/mozilla-firefox/firefox/blob/main/uriloader/exthandler/nsExternalHelperAppService.cpp
5364
var builtinTypesLower = map[string]string{
54-
".avif": "image/avif",
55-
".css": "text/css; charset=utf-8",
56-
".gif": "image/gif",
57-
".htm": "text/html; charset=utf-8",
58-
".html": "text/html; charset=utf-8",
59-
".jpeg": "image/jpeg",
60-
".jpg": "image/jpeg",
61-
".js": "text/javascript; charset=utf-8",
62-
".json": "application/json",
63-
".mjs": "text/javascript; charset=utf-8",
64-
".pdf": "application/pdf",
65-
".png": "image/png",
66-
".svg": "image/svg+xml",
67-
".wasm": "application/wasm",
68-
".webp": "image/webp",
69-
".xml": "text/xml; charset=utf-8",
65+
".ai": "application/postscript",
66+
".apk": "application/vnd.android.package-archive",
67+
".apng": "image/apng",
68+
".avif": "image/avif",
69+
".bin": "application/octet-stream",
70+
".bmp": "image/bmp",
71+
".com": "application/octet-stream",
72+
".css": "text/css; charset=utf-8",
73+
".csv": "text/csv; charset=utf-8",
74+
".doc": "application/msword",
75+
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
76+
".ehtml": "text/html; charset=utf-8",
77+
".eml": "message/rfc822",
78+
".eps": "application/postscript",
79+
".exe": "application/octet-stream",
80+
".flac": "audio/flac",
81+
".gif": "image/gif",
82+
".gz": "application/gzip",
83+
".htm": "text/html; charset=utf-8",
84+
".html": "text/html; charset=utf-8",
85+
".ico": "image/vnd.microsoft.icon",
86+
".ics": "text/calendar; charset=utf-8",
87+
".jfif": "image/jpeg",
88+
".jpeg": "image/jpeg",
89+
".jpg": "image/jpeg",
90+
".js": "text/javascript; charset=utf-8",
91+
".json": "application/json",
92+
".m4a": "audio/mp4",
93+
".mjs": "text/javascript; charset=utf-8",
94+
".mp3": "audio/mpeg",
95+
".mp4": "video/mp4",
96+
".oga": "audio/ogg",
97+
".ogg": "audio/ogg",
98+
".ogv": "video/ogg",
99+
".opus": "audio/ogg",
100+
".pdf": "application/pdf",
101+
".pjp": "image/jpeg",
102+
".pjpeg": "image/jpeg",
103+
".png": "image/png",
104+
".ppt": "application/vnd.ms-powerpoint",
105+
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
106+
".ps": "application/postscript",
107+
".rdf": "application/rdf+xml",
108+
".rtf": "application/rtf",
109+
".shtml": "text/html; charset=utf-8",
110+
".svg": "image/svg+xml",
111+
".text": "text/plain; charset=utf-8",
112+
".tif": "image/tiff",
113+
".tiff": "image/tiff",
114+
".txt": "text/plain; charset=utf-8",
115+
".vtt": "text/vtt; charset=utf-8",
116+
".wasm": "application/wasm",
117+
".wav": "audio/wav",
118+
".webm": "audio/webm",
119+
".webp": "image/webp",
120+
".xbl": "text/xml; charset=utf-8",
121+
".xbm": "image/x-xbitmap",
122+
".xht": "application/xhtml+xml",
123+
".xhtml": "application/xhtml+xml",
124+
".xls": "application/vnd.ms-excel",
125+
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
126+
".xml": "text/xml; charset=utf-8",
127+
".xsl": "text/xml; charset=utf-8",
128+
".zip": "application/zip",
70129
}
71130

72131
var once sync.Once // guards initMime

src/mime/type_test.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,51 @@ func TestExtensionsByType2(t *testing.T) {
208208
typ string
209209
want []string
210210
}{
211-
{typ: "image/jpeg", want: []string{".jpeg", ".jpg"}},
211+
{typ: "application/postscript", want: []string{".ai", ".eps", ".ps"}},
212+
{typ: "application/vnd.android.package-archive", want: []string{".apk"}},
213+
{typ: "image/apng", want: []string{".apng"}},
214+
{typ: "image/avif", want: []string{".avif"}},
215+
{typ: "application/octet-stream", want: []string{".bin", ".com", ".exe"}},
216+
{typ: "image/bmp", want: []string{".bmp"}},
217+
{typ: "text/css; charset=utf-8", want: []string{".css"}},
218+
{typ: "text/csv; charset=utf-8", want: []string{".csv"}},
219+
{typ: "application/msword", want: []string{".doc"}},
220+
{typ: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", want: []string{".docx"}},
221+
{typ: "text/html; charset=utf-8", want: []string{".ehtml", ".htm", ".html", ".shtml"}},
222+
{typ: "message/rfc822", want: []string{".eml"}},
223+
{typ: "audio/flac", want: []string{".flac"}},
224+
{typ: "image/gif", want: []string{".gif"}},
225+
{typ: "application/gzip", want: []string{".gz"}},
226+
{typ: "image/vnd.microsoft.icon", want: []string{".ico"}},
227+
{typ: "text/calendar; charset=utf-8", want: []string{".ics"}},
228+
{typ: "image/jpeg", want: []string{".jfif", ".jpeg", ".jpg", ".pjp", ".pjpeg"}},
229+
{typ: "text/javascript; charset=utf-8", want: []string{".js", ".mjs"}},
230+
{typ: "application/json", want: []string{".json"}},
231+
{typ: "audio/mp4", want: []string{".m4a"}},
232+
{typ: "audio/mpeg", want: []string{".mp3"}},
233+
{typ: "video/mp4", want: []string{".mp4"}},
234+
{typ: "audio/ogg", want: []string{".oga", ".ogg", ".opus"}},
235+
{typ: "video/ogg", want: []string{".ogv"}},
236+
{typ: "application/pdf", want: []string{".pdf"}},
237+
{typ: "image/png", want: []string{".png"}},
238+
{typ: "application/vnd.ms-powerpoint", want: []string{".ppt"}},
239+
{typ: "application/vnd.openxmlformats-officedocument.presentationml.presentation", want: []string{".pptx"}},
240+
{typ: "application/rdf+xml", want: []string{".rdf"}},
241+
{typ: "application/rtf", want: []string{".rtf"}},
242+
{typ: "image/svg+xml", want: []string{".svg"}},
243+
{typ: "text/plain; charset=utf-8", want: []string{".text", ".txt"}},
244+
{typ: "image/tiff", want: []string{".tif", ".tiff"}},
245+
{typ: "text/vtt; charset=utf-8", want: []string{".vtt"}},
246+
{typ: "application/wasm", want: []string{".wasm"}},
247+
{typ: "audio/wav", want: []string{".wav"}},
248+
{typ: "audio/webm", want: []string{".webm"}},
249+
{typ: "image/webp", want: []string{".webp"}},
250+
{typ: "text/xml; charset=utf-8", want: []string{".xbl", ".xml", ".xsl"}},
251+
{typ: "image/x-xbitmap", want: []string{".xbm"}},
252+
{typ: "application/xhtml+xml", want: []string{".xht", ".xhtml"}},
253+
{typ: "application/vnd.ms-excel", want: []string{".xls"}},
254+
{typ: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", want: []string{".xlsx"}},
255+
{typ: "application/zip", want: []string{".zip"}},
212256
}
213257

214258
for _, tt := range tests {

0 commit comments

Comments
 (0)