From 583fddf3bc85802869ce2d286fe8b32cc6728bc8 Mon Sep 17 00:00:00 2001 From: Kshitij Saraogi Date: Mon, 4 Mar 2019 16:29:25 +0530 Subject: [PATCH] net/http: remove discrepancies between the MIME Sniffing Spec and its implementation The change fixes the following deviations between the existing implementation and the Spec: 1. Using pattern instead of "mask" for assertion and iteration in the Pattern Matching Algorithm. 2. Rename "image/vnd.microsoft.icon" to "image/x-icon" and add another signature for the same. 3. Using named strings instead of hexadecimal representation in "application/zip" and "application/x-rar-compressed". 4. Reordering "sniffSignatures" in accordance with the Spec section "Identifying a resource with an unknown MIME type". In addition to the above fixes, unit tests for Image MIME type group are added. Fixes #30570 Change-Id: I97d2ae22b426c3c57bf8efd2ed9396c0be983688 Reviewed-on: https://go-review.googlesource.com/c/go/+/165277 Reviewed-by: Emmanuel Odeke Run-TryBot: Emmanuel Odeke TryBot-Result: Gobot Gobot --- src/net/http/sniff.go | 110 +++++++++++++++++++++++++------------ src/net/http/sniff_test.go | 6 ++ 2 files changed, 80 insertions(+), 36 deletions(-) diff --git a/src/net/http/sniff.go b/src/net/http/sniff.go index c1494abb4c..f03f723542 100644 --- a/src/net/http/sniff.go +++ b/src/net/http/sniff.go @@ -37,6 +37,8 @@ func DetectContentType(data []byte) string { return "application/octet-stream" // fallback } +// isWS reports whether the provided byte is a whitespace byte (0xWS) +// as defined in https://mimesniff.spec.whatwg.org/#terminology. func isWS(b byte) bool { switch b { case '\t', '\n', '\x0c', '\r', ' ': @@ -45,6 +47,16 @@ func isWS(b byte) bool { return false } +// isTT reports whether the provided byte is a tag-terminating byte (0xTT) +// as defined in https://mimesniff.spec.whatwg.org/#terminology. +func isTT(b byte) bool { + switch b { + case ' ', '>': + return true + } + return false +} + type sniffSig interface { // match returns the MIME type of the data, or "" if unknown. match(data []byte, firstNonWS int) string @@ -69,33 +81,57 @@ var sniffSignatures = []sniffSig{ htmlSig("