1
0
mirror of https://github.com/golang/go synced 2024-11-18 04:14:49 -07:00

net/http: don't MIME sniff if handler set an empty string Content-Type

Fixes #5953

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/12117043
This commit is contained in:
Brad Fitzpatrick 2013-07-31 23:38:32 -07:00
parent 184b02ea9f
commit 252c107f2f
2 changed files with 26 additions and 1 deletions

View File

@ -792,7 +792,8 @@ func (cw *chunkWriter) writeHeader(p []byte) {
}
} else {
// If no content type, apply sniffing algorithm to body.
if header.get("Content-Type") == "" && w.req.Method != "HEAD" {
_, haveType := header["Content-Type"]
if !haveType && w.req.Method != "HEAD" {
setHeader.contentType = DetectContentType(p)
}
}

View File

@ -12,6 +12,7 @@ import (
"log"
. "net/http"
"net/http/httptest"
"reflect"
"strconv"
"strings"
"testing"
@ -84,6 +85,29 @@ func TestServerContentType(t *testing.T) {
}
}
// Issue 5953: shouldn't sniff if the handler set a Content-Type header,
// even if it's the empty string.
func TestServerIssue5953(t *testing.T) {
defer afterTest(t)
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
w.Header()["Content-Type"] = []string{""}
fmt.Fprintf(w, "<html><head></head><body>hi</body></html>")
}))
defer ts.Close()
resp, err := Get(ts.URL)
if err != nil {
t.Fatal(err)
}
got := resp.Header["Content-Type"]
want := []string{""}
if !reflect.DeepEqual(got, want) {
t.Errorf("Content-Type = %q; want %q", got, want)
}
resp.Body.Close()
}
func TestContentTypeWithCopy(t *testing.T) {
defer afterTest(t)