From: Brad Fitzpatrick Date: Thu, 1 Aug 2013 06:38:32 +0000 (-0700) Subject: net/http: don't MIME sniff if handler set an empty string Content-Type X-Git-Tag: go1.2rc2~843 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=252c107f2fe3e1ba8a58e06ec0e63fa8c8f90bb5;p=gostls13.git 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 --- diff --git a/src/pkg/net/http/server.go b/src/pkg/net/http/server.go index e0f629347e..5332239ede 100644 --- a/src/pkg/net/http/server.go +++ b/src/pkg/net/http/server.go @@ -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) } } diff --git a/src/pkg/net/http/sniff_test.go b/src/pkg/net/http/sniff_test.go index 106d94ec1c..24ca27afc1 100644 --- a/src/pkg/net/http/sniff_test.go +++ b/src/pkg/net/http/sniff_test.go @@ -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, "hi") + })) + 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)