]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: avoid some allocations in DetectContentType
authorBrad Fitzpatrick <brad@danga.com>
Tue, 9 Dec 2014 00:45:19 +0000 (11:45 +1100)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 9 Dec 2014 02:00:09 +0000 (02:00 +0000)
Change-Id: I64985f8de7ca09e63208e8c949a5d4f4fc09073f
Reviewed-on: https://go-review.googlesource.com/1230
Reviewed-by: David Symonds <dsymonds@golang.org>
src/net/http/sniff.go

index 68f519b0542099a1f9dca3994abda70e2b43dee4..3be8c865d3b3933c18ecfa456ec99b322d1b9bdd 100644 (file)
@@ -38,7 +38,11 @@ func DetectContentType(data []byte) string {
 }
 
 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 {
@@ -161,6 +165,8 @@ func (h htmlSig) match(data []byte, firstNonWS int) string {
        return "text/html; charset=utf-8"
 }
 
+var mp4ftype = []byte("ftyp")
+
 type mp4Sig int
 
 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 {
                return ""
        }
-       if !bytes.Equal(data[4:8], []byte("ftyp")) {
+       if !bytes.Equal(data[4:8], mp4ftype) {
                return ""
        }
        for st := 8; st < boxSize; st += 4 {