Commit 252c107f authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

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
parent 184b02ea
......@@ -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)
}
}
......
......@@ -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)
......
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