Commit 1490cf67 authored by Emmanuel Odeke's avatar Emmanuel Odeke Committed by Brad Fitzpatrick

net/http: implement sniffing for some fonts

Implement sniffing for fonts:
* MS Font object  --> "application/vnd.ms-fontobject"
* ttf		  --> "application/font-ttf"

* off		  --> "application/font-off"
* otf		  --> "application/font-off"

* cff		  --> "application/font-cff"
* woff		  --> "application/font-woff"

Fixes #20808

Change-Id: Ibe02a87d3c9d610c6a30e1b6c03f4e520404e70f
Reviewed-on: https://go-review.googlesource.com/47553
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 6ecd843e
...@@ -91,6 +91,7 @@ var sniffSignatures = []sniffSig{ ...@@ -91,6 +91,7 @@ var sniffSignatures = []sniffSig{
ct: "image/webp", ct: "image/webp",
}, },
&exactSig{[]byte("\x00\x00\x01\x00"), "image/vnd.microsoft.icon"}, &exactSig{[]byte("\x00\x00\x01\x00"), "image/vnd.microsoft.icon"},
&maskedSig{ &maskedSig{
mask: []byte("\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF"), mask: []byte("\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF"),
pat: []byte("RIFF\x00\x00\x00\x00WAVE"), pat: []byte("RIFF\x00\x00\x00\x00WAVE"),
...@@ -126,6 +127,20 @@ var sniffSignatures = []sniffSig{ ...@@ -126,6 +127,20 @@ var sniffSignatures = []sniffSig{
pat: []byte("RIFF\x00\x00\x00\x00AVI "), pat: []byte("RIFF\x00\x00\x00\x00AVI "),
ct: "video/avi", ct: "video/avi",
}, },
// Fonts
&maskedSig{
// 34 NULL bytes followed by the string "LP"
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"),
// 34 NULL bytes followed by \xF\xF
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"),
ct: "application/vnd.ms-fontobject",
},
&exactSig{[]byte("\x00\x01\x00\x00"), "application/font-ttf"},
&exactSig{[]byte("OTTO"), "application/font-off"},
&exactSig{[]byte("ttcf"), "application/font-cff"},
&exactSig{[]byte("wOFF"), "application/font-woff"},
&exactSig{[]byte("\x1A\x45\xDF\xA3"), "video/webm"}, &exactSig{[]byte("\x1A\x45\xDF\xA3"), "video/webm"},
&exactSig{[]byte("\x52\x61\x72\x20\x1A\x07\x00"), "application/x-rar-compressed"}, &exactSig{[]byte("\x52\x61\x72\x20\x1A\x07\x00"), "application/x-rar-compressed"},
&exactSig{[]byte("\x50\x4B\x03\x04"), "application/zip"}, &exactSig{[]byte("\x50\x4B\x03\x04"), "application/zip"},
......
...@@ -54,6 +54,17 @@ var sniffTests = []struct { ...@@ -54,6 +54,17 @@ var sniffTests = []struct {
{"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"},
{"AVI video #1", []byte("RIFF,O\n\x00AVI LISTÀ"), "video/avi"}, {"AVI video #1", []byte("RIFF,O\n\x00AVI LISTÀ"), "video/avi"},
{"AVI video #2", []byte("RIFF,\n\x00\x00AVI LISTÀ"), "video/avi"}, {"AVI video #2", []byte("RIFF,\n\x00\x00AVI LISTÀ"), "video/avi"},
// Font types.
// {"MS.FontObject", []byte("\x00\x00")},
{"TTF sample I", []byte("\x00\x01\x00\x00\x00\x17\x01\x00\x00\x04\x01\x60\x4f"), "application/font-ttf"},
{"TTF sample II", []byte("\x00\x01\x00\x00\x00\x0e\x00\x80\x00\x03\x00\x60\x46"), "application/font-ttf"},
{"OTTO sample I", []byte("\x4f\x54\x54\x4f\x00\x0e\x00\x80\x00\x03\x00\x60\x42\x41\x53\x45"), "application/font-off"},
{"woff sample I", []byte("\x77\x4f\x46\x46\x00\x01\x00\x00\x00\x00\x30\x54\x00\x0d\x00\x00"), "application/font-woff"},
// Woff2 is not yet recognized, change this test once mime-sniff working group adds woff2
{"woff2 not recognized", []byte("\x77\x4f\x46\x32\x00\x01\x00\x00\x00"), "application/octet-stream"},
} }
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