Commit 8b1152a5 authored by Emmanuel Odeke's avatar Emmanuel Odeke Committed by Brad Fitzpatrick

net/http: add/update mp4 sniffing

Completes sniffing for mp4 signature according
to the spec at:
  https://mimesniff.spec.whatwg.org/#signature-for-mp4
Clause 6.2.1

Fixes #8773

Change-Id: Icfc4a23324ae249db52c94a21c0e8509e1833e19
Reviewed-on: https://go-review.googlesource.com/16951Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent ce8f49f4
...@@ -102,10 +102,9 @@ var sniffSignatures = []sniffSig{ ...@@ -102,10 +102,9 @@ var sniffSignatures = []sniffSig{
&exactSig{[]byte("\x50\x4B\x03\x04"), "application/zip"}, &exactSig{[]byte("\x50\x4B\x03\x04"), "application/zip"},
&exactSig{[]byte("\x1F\x8B\x08"), "application/x-gzip"}, &exactSig{[]byte("\x1F\x8B\x08"), "application/x-gzip"},
// TODO(dsymonds): Re-enable this when the spec is sorted w.r.t. MP4. mp4Sig{},
//mp4Sig(0),
textSig(0), // should be last textSig{}, // should be last
} }
type exactSig struct { type exactSig struct {
...@@ -166,12 +165,14 @@ func (h htmlSig) match(data []byte, firstNonWS int) string { ...@@ -166,12 +165,14 @@ func (h htmlSig) match(data []byte, firstNonWS int) string {
} }
var mp4ftype = []byte("ftyp") var mp4ftype = []byte("ftyp")
var mp4 = []byte("mp4")
type mp4Sig int type mp4Sig struct{}
func (mp4Sig) match(data []byte, firstNonWS int) string { func (mp4Sig) match(data []byte, firstNonWS int) string {
// c.f. section 6.1. // https://mimesniff.spec.whatwg.org/#signature-for-mp4
if len(data) < 8 { // c.f. section 6.2.1
if len(data) < 12 {
return "" return ""
} }
boxSize := int(binary.BigEndian.Uint32(data[:4])) boxSize := int(binary.BigEndian.Uint32(data[:4]))
...@@ -186,24 +187,14 @@ func (mp4Sig) match(data []byte, firstNonWS int) string { ...@@ -186,24 +187,14 @@ func (mp4Sig) match(data []byte, firstNonWS int) string {
// minor version number // minor version number
continue continue
} }
seg := string(data[st : st+3]) if bytes.Equal(data[st:st+3], mp4) {
switch seg {
case "mp4", "iso", "M4V", "M4P", "M4B":
return "video/mp4" return "video/mp4"
/* The remainder are not in the spec.
case "M4A":
return "audio/mp4"
case "3gp":
return "video/3gpp"
case "jp2":
return "image/jp2" // JPEG 2000
*/
} }
} }
return "" return ""
} }
type textSig int type textSig struct{}
func (textSig) match(data []byte, firstNonWS int) string { func (textSig) match(data []byte, firstNonWS int) string {
// c.f. section 5, step 4. // c.f. section 5, step 4.
......
...@@ -40,9 +40,7 @@ var sniffTests = []struct { ...@@ -40,9 +40,7 @@ var sniffTests = []struct {
{"GIF 87a", []byte(`GIF87a`), "image/gif"}, {"GIF 87a", []byte(`GIF87a`), "image/gif"},
{"GIF 89a", []byte(`GIF89a...`), "image/gif"}, {"GIF 89a", []byte(`GIF89a...`), "image/gif"},
// TODO(dsymonds): Re-enable this when the spec is sorted w.r.t. MP4. {"MP4 video", []byte("\x00\x00\x00\x18ftypmp42\x00\x00\x00\x00mp42isom<\x06t\xbfmdat"), "video/mp4"},
//{"MP4 video", []byte("\x00\x00\x00\x18ftypmp42\x00\x00\x00\x00mp42isom<\x06t\xbfmdat"), "video/mp4"},
//{"MP4 audio", []byte("\x00\x00\x00\x20ftypM4A \x00\x00\x00\x00M4A mp42isom\x00\x00\x00\x00"), "audio/mp4"},
} }
func TestDetectContentType(t *testing.T) { func TestDetectContentType(t *testing.T) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment