Russ Cox | e1b2e20 | 2011-07-15 01:01:49 -0400 | [diff] [blame] | 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. |
| 4 | |
| 5 | package http |
| 6 | |
David Symonds | 78c89d2 | 2011-07-21 08:38:35 +1000 | [diff] [blame] | 7 | import ( |
| 8 | "bytes" |
David Symonds | e527d49 | 2011-07-26 13:28:20 +1000 | [diff] [blame] | 9 | "encoding/binary" |
David Symonds | 78c89d2 | 2011-07-21 08:38:35 +1000 | [diff] [blame] | 10 | ) |
| 11 | |
Russ Cox | d9da346 | 2012-02-12 23:14:48 -0500 | [diff] [blame] | 12 | // The algorithm uses at most sniffLen bytes to make its decision. |
David Symonds | 78c89d2 | 2011-07-21 08:38:35 +1000 | [diff] [blame] | 13 | const sniffLen = 512 |
Russ Cox | e1b2e20 | 2011-07-15 01:01:49 -0400 | [diff] [blame] | 14 | |
Russ Cox | d9da346 | 2012-02-12 23:14:48 -0500 | [diff] [blame] | 15 | // DetectContentType implements the algorithm described |
| 16 | // at http://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". |
David Symonds | 78c89d2 | 2011-07-21 08:38:35 +1000 | [diff] [blame] | 21 | func DetectContentType(data []byte) string { |
| 22 | if len(data) > sniffLen { |
| 23 | data = data[:sniffLen] |
| 24 | } |
| 25 | |
| 26 | // Index of the first non-whitespace byte in data. |
| 27 | firstNonWS := 0 |
| 28 | for ; firstNonWS < len(data) && isWS(data[firstNonWS]); firstNonWS++ { |
| 29 | } |
| 30 | |
| 31 | for _, sig := range sniffSignatures { |
| 32 | if ct := sig.match(data, firstNonWS); ct != "" { |
| 33 | return ct |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | return "application/octet-stream" // fallback |
| 38 | } |
| 39 | |
| 40 | func isWS(b byte) bool { |
Brad Fitzpatrick | e67a39e | 2014-12-09 11:45:19 +1100 | [diff] [blame] | 41 | switch b { |
| 42 | case '\t', '\n', '\x0c', '\r', ' ': |
| 43 | return true |
| 44 | } |
| 45 | return false |
David Symonds | 78c89d2 | 2011-07-21 08:38:35 +1000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | type sniffSig interface { |
| 49 | // match returns the MIME type of the data, or "" if unknown. |
| 50 | match(data []byte, firstNonWS int) string |
| 51 | } |
| 52 | |
| 53 | // Data matching the table in section 6. |
| 54 | var sniffSignatures = []sniffSig{ |
Russ Cox | 6e3e380 | 2011-11-22 12:30:02 -0500 | [diff] [blame] | 55 | htmlSig("<!DOCTYPE HTML"), |
| 56 | htmlSig("<HTML"), |
| 57 | htmlSig("<HEAD"), |
| 58 | htmlSig("<SCRIPT"), |
| 59 | htmlSig("<IFRAME"), |
| 60 | htmlSig("<H1"), |
| 61 | htmlSig("<DIV"), |
| 62 | htmlSig("<FONT"), |
| 63 | htmlSig("<TABLE"), |
| 64 | htmlSig("<A"), |
| 65 | htmlSig("<STYLE"), |
| 66 | htmlSig("<TITLE"), |
| 67 | htmlSig("<B"), |
| 68 | htmlSig("<BODY"), |
| 69 | htmlSig("<BR"), |
| 70 | htmlSig("<P"), |
| 71 | htmlSig("<!--"), |
David Symonds | 78c89d2 | 2011-07-21 08:38:35 +1000 | [diff] [blame] | 72 | |
| 73 | &maskedSig{mask: []byte("\xFF\xFF\xFF\xFF\xFF"), pat: []byte("<?xml"), skipWS: true, ct: "text/xml; charset=utf-8"}, |
| 74 | |
| 75 | &exactSig{[]byte("%PDF-"), "application/pdf"}, |
| 76 | &exactSig{[]byte("%!PS-Adobe-"), "application/postscript"}, |
| 77 | |
| 78 | // UTF BOMs. |
| 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"}, |
| 82 | |
| 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"}, |
| 88 | &maskedSig{ |
| 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"), |
| 91 | ct: "image/webp", |
| 92 | }, |
| 93 | &exactSig{[]byte("\x00\x00\x01\x00"), "image/vnd.microsoft.icon"}, |
| 94 | &exactSig{[]byte("\x4F\x67\x67\x53\x00"), "application/ogg"}, |
| 95 | &maskedSig{ |
| 96 | mask: []byte("\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF"), |
| 97 | pat: []byte("RIFF\x00\x00\x00\x00WAVE"), |
| 98 | ct: "audio/wave", |
| 99 | }, |
| 100 | &exactSig{[]byte("\x1A\x45\xDF\xA3"), "video/webm"}, |
| 101 | &exactSig{[]byte("\x52\x61\x72\x20\x1A\x07\x00"), "application/x-rar-compressed"}, |
| 102 | &exactSig{[]byte("\x50\x4B\x03\x04"), "application/zip"}, |
| 103 | &exactSig{[]byte("\x1F\x8B\x08"), "application/x-gzip"}, |
| 104 | |
Emmanuel Odeke | 8b1152a | 2015-11-16 20:24:58 -0700 | [diff] [blame] | 105 | mp4Sig{}, |
David Symonds | 78c89d2 | 2011-07-21 08:38:35 +1000 | [diff] [blame] | 106 | |
Emmanuel Odeke | 8b1152a | 2015-11-16 20:24:58 -0700 | [diff] [blame] | 107 | textSig{}, // should be last |
David Symonds | 78c89d2 | 2011-07-21 08:38:35 +1000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | type exactSig struct { |
| 111 | sig []byte |
| 112 | ct string |
| 113 | } |
| 114 | |
| 115 | func (e *exactSig) match(data []byte, firstNonWS int) string { |
| 116 | if bytes.HasPrefix(data, e.sig) { |
| 117 | return e.ct |
| 118 | } |
| 119 | return "" |
| 120 | } |
| 121 | |
| 122 | type maskedSig struct { |
| 123 | mask, pat []byte |
| 124 | skipWS bool |
| 125 | ct string |
| 126 | } |
| 127 | |
| 128 | func (m *maskedSig) match(data []byte, firstNonWS int) string { |
| 129 | if m.skipWS { |
| 130 | data = data[firstNonWS:] |
| 131 | } |
| 132 | if len(data) < len(m.mask) { |
| 133 | return "" |
| 134 | } |
| 135 | for i, mask := range m.mask { |
| 136 | db := data[i] & mask |
| 137 | if db != m.pat[i] { |
| 138 | return "" |
| 139 | } |
| 140 | } |
| 141 | return m.ct |
| 142 | } |
| 143 | |
| 144 | type htmlSig []byte |
| 145 | |
| 146 | func (h htmlSig) match(data []byte, firstNonWS int) string { |
| 147 | data = data[firstNonWS:] |
| 148 | if len(data) < len(h)+1 { |
| 149 | return "" |
| 150 | } |
| 151 | for i, b := range h { |
| 152 | db := data[i] |
| 153 | if 'A' <= b && b <= 'Z' { |
| 154 | db &= 0xDF |
| 155 | } |
| 156 | if b != db { |
| 157 | return "" |
| 158 | } |
| 159 | } |
| 160 | // Next byte must be space or right angle bracket. |
| 161 | if db := data[len(h)]; db != ' ' && db != '>' { |
| 162 | return "" |
| 163 | } |
Russ Cox | e1b2e20 | 2011-07-15 01:01:49 -0400 | [diff] [blame] | 164 | return "text/html; charset=utf-8" |
| 165 | } |
David Symonds | 78c89d2 | 2011-07-21 08:38:35 +1000 | [diff] [blame] | 166 | |
Brad Fitzpatrick | e67a39e | 2014-12-09 11:45:19 +1100 | [diff] [blame] | 167 | var mp4ftype = []byte("ftyp") |
Emmanuel Odeke | 8b1152a | 2015-11-16 20:24:58 -0700 | [diff] [blame] | 168 | var mp4 = []byte("mp4") |
Brad Fitzpatrick | e67a39e | 2014-12-09 11:45:19 +1100 | [diff] [blame] | 169 | |
Emmanuel Odeke | 8b1152a | 2015-11-16 20:24:58 -0700 | [diff] [blame] | 170 | type mp4Sig struct{} |
David Symonds | e527d49 | 2011-07-26 13:28:20 +1000 | [diff] [blame] | 171 | |
| 172 | func (mp4Sig) match(data []byte, firstNonWS int) string { |
Emmanuel Odeke | 8b1152a | 2015-11-16 20:24:58 -0700 | [diff] [blame] | 173 | // https://mimesniff.spec.whatwg.org/#signature-for-mp4 |
| 174 | // c.f. section 6.2.1 |
| 175 | if len(data) < 12 { |
David Symonds | e527d49 | 2011-07-26 13:28:20 +1000 | [diff] [blame] | 176 | return "" |
| 177 | } |
| 178 | boxSize := int(binary.BigEndian.Uint32(data[:4])) |
| 179 | if boxSize%4 != 0 || len(data) < boxSize { |
| 180 | return "" |
| 181 | } |
Brad Fitzpatrick | e67a39e | 2014-12-09 11:45:19 +1100 | [diff] [blame] | 182 | if !bytes.Equal(data[4:8], mp4ftype) { |
David Symonds | e527d49 | 2011-07-26 13:28:20 +1000 | [diff] [blame] | 183 | return "" |
| 184 | } |
| 185 | for st := 8; st < boxSize; st += 4 { |
| 186 | if st == 12 { |
| 187 | // minor version number |
| 188 | continue |
| 189 | } |
Emmanuel Odeke | 8b1152a | 2015-11-16 20:24:58 -0700 | [diff] [blame] | 190 | if bytes.Equal(data[st:st+3], mp4) { |
David Symonds | e527d49 | 2011-07-26 13:28:20 +1000 | [diff] [blame] | 191 | return "video/mp4" |
| 192 | } |
| 193 | } |
| 194 | return "" |
| 195 | } |
| 196 | |
Emmanuel Odeke | 8b1152a | 2015-11-16 20:24:58 -0700 | [diff] [blame] | 197 | type textSig struct{} |
David Symonds | 78c89d2 | 2011-07-21 08:38:35 +1000 | [diff] [blame] | 198 | |
| 199 | func (textSig) match(data []byte, firstNonWS int) string { |
| 200 | // c.f. section 5, step 4. |
| 201 | for _, b := range data[firstNonWS:] { |
| 202 | switch { |
Todd Neal | c1aee8c | 2015-08-25 18:44:30 -0500 | [diff] [blame] | 203 | case b <= 0x08, |
David Symonds | 78c89d2 | 2011-07-21 08:38:35 +1000 | [diff] [blame] | 204 | b == 0x0B, |
| 205 | 0x0E <= b && b <= 0x1A, |
| 206 | 0x1C <= b && b <= 0x1F: |
| 207 | return "" |
| 208 | } |
| 209 | } |
| 210 | return "text/plain; charset=utf-8" |
| 211 | } |