Commit e67a39ed authored by Brad Fitzpatrick's avatar Brad Fitzpatrick Committed by Brad Fitzpatrick

net/http: avoid some allocations in DetectContentType

Change-Id: I64985f8de7ca09e63208e8c949a5d4f4fc09073f
Reviewed-on: https://go-review.googlesource.com/1230Reviewed-by: default avatarDavid Symonds <dsymonds@golang.org>
parent c5de72b2
...@@ -38,7 +38,11 @@ func DetectContentType(data []byte) string { ...@@ -38,7 +38,11 @@ func DetectContentType(data []byte) string {
} }
func isWS(b byte) bool { func isWS(b byte) bool {
return bytes.IndexByte([]byte("\t\n\x0C\r "), b) != -1 switch b {
case '\t', '\n', '\x0c', '\r', ' ':
return true
}
return false
} }
type sniffSig interface { type sniffSig interface {
...@@ -161,6 +165,8 @@ func (h htmlSig) match(data []byte, firstNonWS int) string { ...@@ -161,6 +165,8 @@ func (h htmlSig) match(data []byte, firstNonWS int) string {
return "text/html; charset=utf-8" return "text/html; charset=utf-8"
} }
var mp4ftype = []byte("ftyp")
type mp4Sig int type mp4Sig int
func (mp4Sig) match(data []byte, firstNonWS int) string { func (mp4Sig) match(data []byte, firstNonWS int) string {
...@@ -172,7 +178,7 @@ func (mp4Sig) match(data []byte, firstNonWS int) string { ...@@ -172,7 +178,7 @@ func (mp4Sig) match(data []byte, firstNonWS int) string {
if boxSize%4 != 0 || len(data) < boxSize { if boxSize%4 != 0 || len(data) < boxSize {
return "" return ""
} }
if !bytes.Equal(data[4:8], []byte("ftyp")) { if !bytes.Equal(data[4:8], mp4ftype) {
return "" return ""
} }
for st := 8; st < boxSize; st += 4 { for st := 8; st < boxSize; st += 4 {
......
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