1 // Copyright 2011 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.
12 // The algorithm uses at most sniffLen bytes to make its decision.
15 // DetectContentType implements the algorithm described
16 // at https://mimesniff.spec.whatwg.org/ to determine the
17 // Content-Type of the given data. It considers at most the
18 // first 512 bytes of data. DetectContentType always returns
19 // a valid MIME type: if it cannot determine a more specific one, it
20 // returns "application/octet-stream".
21 func DetectContentType(data
[]byte) string {
22 if len(data
) > sniffLen
{
23 data
= data
[:sniffLen
]
26 // Index of the first non-whitespace byte in data.
28 for ; firstNonWS
< len(data
) && isWS(data
[firstNonWS
]); firstNonWS
++ {
31 for _
, sig
:= range sniffSignatures
{
32 if ct
:= sig
.match(data
, firstNonWS
); ct
!= "" {
37 return "application/octet-stream" // fallback
40 // isWS reports whether the provided byte is a whitespace byte (0xWS)
41 // as defined in https://mimesniff.spec.whatwg.org/#terminology.
42 func isWS(b
byte) bool {
44 case '\t', '\n', '\x0c', '\r', ' ':
50 // isTT reports whether the provided byte is a tag-terminating byte (0xTT)
51 // as defined in https://mimesniff.spec.whatwg.org/#terminology.
52 func isTT(b
byte) bool {
60 type sniffSig
interface {
61 // match returns the MIME type of the data, or "" if unknown.
62 match(data
[]byte, firstNonWS
int) string
65 // Data matching the table in section 6.
66 var sniffSignatures
= []sniffSig
{
67 htmlSig("<!DOCTYPE HTML"),
85 mask
: []byte("\xFF\xFF\xFF\xFF\xFF"),
88 ct
: "text/xml; charset=utf-8"},
89 &exactSig
{[]byte("%PDF-"), "application/pdf"},
90 &exactSig
{[]byte("%!PS-Adobe-"), "application/postscript"},
94 mask
: []byte("\xFF\xFF\x00\x00"),
95 pat
: []byte("\xFE\xFF\x00\x00"),
96 ct
: "text/plain; charset=utf-16be",
99 mask
: []byte("\xFF\xFF\x00\x00"),
100 pat
: []byte("\xFF\xFE\x00\x00"),
101 ct
: "text/plain; charset=utf-16le",
104 mask
: []byte("\xFF\xFF\xFF\x00"),
105 pat
: []byte("\xEF\xBB\xBF\x00"),
106 ct
: "text/plain; charset=utf-8",
110 // For posterity, we originally returned "image/vnd.microsoft.icon" from
111 // https://tools.ietf.org/html/draft-ietf-websec-mime-sniff-03#section-7
112 // https://codereview.appspot.com/4746042
113 // but that has since been replaced with "image/x-icon" in Section 6.2
114 // of https://mimesniff.spec.whatwg.org/#matching-an-image-type-pattern
115 &exactSig
{[]byte("\x00\x00\x01\x00"), "image/x-icon"},
116 &exactSig
{[]byte("\x00\x00\x02\x00"), "image/x-icon"},
117 &exactSig
{[]byte("BM"), "image/bmp"},
118 &exactSig
{[]byte("GIF87a"), "image/gif"},
119 &exactSig
{[]byte("GIF89a"), "image/gif"},
121 mask
: []byte("\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF"),
122 pat
: []byte("RIFF\x00\x00\x00\x00WEBPVP"),
125 &exactSig
{[]byte("\x89PNG\x0D\x0A\x1A\x0A"), "image/png"},
126 &exactSig
{[]byte("\xFF\xD8\xFF"), "image/jpeg"},
128 // Audio and Video types
129 // Enforce the pattern match ordering as prescribed in
130 // https://mimesniff.spec.whatwg.org/#matching-an-audio-or-video-type-pattern
132 mask
: []byte("\xFF\xFF\xFF\xFF"),
137 mask
: []byte("\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF"),
138 pat
: []byte("FORM\x00\x00\x00\x00AIFF"),
142 mask
: []byte("\xFF\xFF\xFF"),
147 mask
: []byte("\xFF\xFF\xFF\xFF\xFF"),
148 pat
: []byte("OggS\x00"),
149 ct
: "application/ogg",
152 mask
: []byte("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
153 pat
: []byte("MThd\x00\x00\x00\x06"),
157 mask
: []byte("\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF"),
158 pat
: []byte("RIFF\x00\x00\x00\x00AVI "),
162 mask
: []byte("\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF"),
163 pat
: []byte("RIFF\x00\x00\x00\x00WAVE"),
166 // 6.2.0.2. video/mp4
168 // 6.2.0.3. video/webm
169 &exactSig
{[]byte("\x1A\x45\xDF\xA3"), "video/webm"},
173 // 34 NULL bytes followed by the string "LP"
174 pat
: []byte("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00LP"),
175 // 34 NULL bytes followed by \xF\xF
176 mask
: []byte("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF"),
177 ct
: "application/vnd.ms-fontobject",
179 &exactSig
{[]byte("\x00\x01\x00\x00"), "font/ttf"},
180 &exactSig
{[]byte("OTTO"), "font/otf"},
181 &exactSig
{[]byte("ttcf"), "font/collection"},
182 &exactSig
{[]byte("wOFF"), "font/woff"},
183 &exactSig
{[]byte("wOF2"), "font/woff2"},
186 &exactSig
{[]byte("\x1F\x8B\x08"), "application/x-gzip"},
187 &exactSig
{[]byte("PK\x03\x04"), "application/zip"},
188 // RAR's signatures are incorrectly defined by the MIME spec as per
189 // https://github.com/whatwg/mimesniff/issues/63
190 // However, RAR Labs correctly defines it at:
191 // https://www.rarlab.com/technote.htm#rarsign
192 // so we use the definition from RAR Labs.
193 // TODO: do whatever the spec ends up doing.
194 &exactSig
{[]byte("Rar!\x1A\x07\x00"), "application/x-rar-compressed"}, // RAR v1.5-v4.0
195 &exactSig
{[]byte("Rar!\x1A\x07\x01\x00"), "application/x-rar-compressed"}, // RAR v5+
197 &exactSig
{[]byte("\x00\x61\x73\x6D"), "application/wasm"},
199 textSig
{}, // should be last
202 type exactSig
struct {
207 func (e
*exactSig
) match(data
[]byte, firstNonWS
int) string {
208 if bytes
.HasPrefix(data
, e
.sig
) {
214 type maskedSig
struct {
220 func (m
*maskedSig
) match(data
[]byte, firstNonWS
int) string {
221 // pattern matching algorithm section 6
222 // https://mimesniff.spec.whatwg.org/#pattern-matching-algorithm
225 data
= data
[firstNonWS
:]
227 if len(m
.pat
) != len(m
.mask
) {
230 if len(data
) < len(m
.pat
) {
233 for i
, pb
:= range m
.pat
{
234 maskedData
:= data
[i
] & m
.mask
[i
]
235 if maskedData
!= pb
{
244 func (h htmlSig
) match(data
[]byte, firstNonWS
int) string {
245 data
= data
[firstNonWS
:]
246 if len(data
) < len(h
)+1 {
249 for i
, b
:= range h
{
251 if 'A' <= b
&& b
<= 'Z' {
258 // Next byte must be a tag-terminating byte(0xTT).
259 if !isTT(data
[len(h
)]) {
262 return "text/html; charset=utf-8"
265 var mp4ftype
= []byte("ftyp")
266 var mp4
= []byte("mp4")
270 func (mp4Sig
) match(data
[]byte, firstNonWS
int) string {
271 // https://mimesniff.spec.whatwg.org/#signature-for-mp4
272 // c.f. section 6.2.1
276 boxSize
:= int(binary
.BigEndian
.Uint32(data
[:4]))
277 if len(data
) < boxSize || boxSize%4
!= 0 {
280 if !bytes
.Equal(data
[4:8], mp4ftype
) {
283 for st
:= 8; st
< boxSize
; st
+= 4 {
285 // Ignores the four bytes that correspond to the version number of the "major brand".
288 if bytes
.Equal(data
[st
:st
+3], mp4
) {
295 type textSig
struct{}
297 func (textSig
) match(data
[]byte, firstNonWS
int) string {
298 // c.f. section 5, step 4.
299 for _
, b
:= range data
[firstNonWS
:] {
303 0x0E <= b
&& b
<= 0x1A,
304 0x1C <= b
&& b
<= 0x1F:
308 return "text/plain; charset=utf-8"