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 func isWS(b
byte) bool {
42 case '\t', '\n', '\x0c', '\r', ' ':
48 type sniffSig
interface {
49 // match returns the MIME type of the data, or "" if unknown.
50 match(data
[]byte, firstNonWS
int) string
53 // Data matching the table in section 6.
54 var sniffSignatures
= []sniffSig
{
55 htmlSig("<!DOCTYPE HTML"),
73 &maskedSig
{mask
: []byte("\xFF\xFF\xFF\xFF\xFF"), pat
: []byte("<?xml"), skipWS
: true, ct
: "text/xml; charset=utf-8"},
75 &exactSig
{[]byte("%PDF-"), "application/pdf"},
76 &exactSig
{[]byte("%!PS-Adobe-"), "application/postscript"},
79 &maskedSig
{mask
: []byte("\xFF\xFF\x00\x00"), pat
: []byte("\xFE\xFF\x00\x00"), ct
: "text/plain; charset=utf-16be"},
80 &maskedSig
{mask
: []byte("\xFF\xFF\x00\x00"), pat
: []byte("\xFF\xFE\x00\x00"), ct
: "text/plain; charset=utf-16le"},
81 &maskedSig
{mask
: []byte("\xFF\xFF\xFF\x00"), pat
: []byte("\xEF\xBB\xBF\x00"), ct
: "text/plain; charset=utf-8"},
83 &exactSig
{[]byte("GIF87a"), "image/gif"},
84 &exactSig
{[]byte("GIF89a"), "image/gif"},
85 &exactSig
{[]byte("\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"), "image/png"},
86 &exactSig
{[]byte("\xFF\xD8\xFF"), "image/jpeg"},
87 &exactSig
{[]byte("BM"), "image/bmp"},
89 mask
: []byte("\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF"),
90 pat
: []byte("RIFF\x00\x00\x00\x00WEBPVP"),
93 &exactSig
{[]byte("\x00\x00\x01\x00"), "image/vnd.microsoft.icon"},
96 mask
: []byte("\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF"),
97 pat
: []byte("RIFF\x00\x00\x00\x00WAVE"),
101 mask
: []byte("\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF"),
102 pat
: []byte("FORM\x00\x00\x00\x00AIFF"),
106 mask
: []byte("\xFF\xFF\xFF\xFF"),
111 mask
: []byte("\xFF\xFF\xFF\xFF\xFF"),
112 pat
: []byte("OggS\x00"),
113 ct
: "application/ogg",
116 mask
: []byte("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
117 pat
: []byte("MThd\x00\x00\x00\x06"),
121 mask
: []byte("\xFF\xFF\xFF"),
126 mask
: []byte("\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF"),
127 pat
: []byte("RIFF\x00\x00\x00\x00AVI "),
133 // 34 NULL bytes followed by the string "LP"
134 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\x00\x4C\x50"),
135 // 34 NULL bytes followed by \xF\xF
136 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"),
137 ct
: "application/vnd.ms-fontobject",
139 &exactSig
{[]byte("\x00\x01\x00\x00"), "font/ttf"},
140 &exactSig
{[]byte("OTTO"), "font/otf"},
141 &exactSig
{[]byte("ttcf"), "font/collection"},
142 &exactSig
{[]byte("wOFF"), "font/woff"},
143 &exactSig
{[]byte("wOF2"), "font/woff2"},
145 &exactSig
{[]byte("\x1A\x45\xDF\xA3"), "video/webm"},
146 &exactSig
{[]byte("\x52\x61\x72\x20\x1A\x07\x00"), "application/x-rar-compressed"},
147 &exactSig
{[]byte("\x50\x4B\x03\x04"), "application/zip"},
148 &exactSig
{[]byte("\x1F\x8B\x08"), "application/x-gzip"},
150 &exactSig
{[]byte("\x00\x61\x73\x6D"), "application/wasm"},
154 textSig
{}, // should be last
157 type exactSig
struct {
162 func (e
*exactSig
) match(data
[]byte, firstNonWS
int) string {
163 if bytes
.HasPrefix(data
, e
.sig
) {
169 type maskedSig
struct {
175 func (m
*maskedSig
) match(data
[]byte, firstNonWS
int) string {
176 // pattern matching algorithm section 6
177 // https://mimesniff.spec.whatwg.org/#pattern-matching-algorithm
180 data
= data
[firstNonWS
:]
182 if len(m
.pat
) != len(m
.mask
) {
185 if len(data
) < len(m
.mask
) {
188 for i
, mask
:= range m
.mask
{
199 func (h htmlSig
) match(data
[]byte, firstNonWS
int) string {
200 data
= data
[firstNonWS
:]
201 if len(data
) < len(h
)+1 {
204 for i
, b
:= range h
{
206 if 'A' <= b
&& b
<= 'Z' {
213 // Next byte must be space or right angle bracket.
214 if db
:= data
[len(h
)]; db
!= ' ' && db
!= '>' {
217 return "text/html; charset=utf-8"
220 var mp4ftype
= []byte("ftyp")
221 var mp4
= []byte("mp4")
225 func (mp4Sig
) match(data
[]byte, firstNonWS
int) string {
226 // https://mimesniff.spec.whatwg.org/#signature-for-mp4
227 // c.f. section 6.2.1
231 boxSize
:= int(binary
.BigEndian
.Uint32(data
[:4]))
232 if boxSize%4
!= 0 ||
len(data
) < boxSize
{
235 if !bytes
.Equal(data
[4:8], mp4ftype
) {
238 for st
:= 8; st
< boxSize
; st
+= 4 {
240 // minor version number
243 if bytes
.Equal(data
[st
:st
+3], mp4
) {
250 type textSig
struct{}
252 func (textSig
) match(data
[]byte, firstNonWS
int) string {
253 // c.f. section 5, step 4.
254 for _
, b
:= range data
[firstNonWS
:] {
258 0x0E <= b
&& b
<= 0x1A,
259 0x1C <= b
&& b
<= 0x1F:
263 return "text/plain; charset=utf-8"