From db473f6b236423f13eb55a9d65bc6e9fc9bd4e03 Mon Sep 17 00:00:00 2001 From: Tom Bergan Date: Mon, 27 Nov 2017 16:21:05 -0800 Subject: [PATCH] http2: don't autodetect Content-Type when the response has an empty body This change was originally made in https://golang.org/cl/46631, however, that change was applited to net/http/h2_bundle.go instead of x/net/http2. Updates golang/go#20784 Change-Id: I947fa4c19f3efc400856573768140bece28276a2 Reviewed-on: https://go-review.googlesource.com/80135 Run-TryBot: Tom Bergan TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- http2/go19_test.go | 1 - http2/server.go | 2 +- http2/server_test.go | 3 --- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/http2/go19_test.go b/http2/go19_test.go index 1675d248..22b00066 100644 --- a/http2/go19_test.go +++ b/http2/go19_test.go @@ -46,7 +46,6 @@ func TestServerGracefulShutdown(t *testing.T) { wanth := [][2]string{ {":status", "200"}, {"x-foo", "bar"}, - {"content-type", "text/plain; charset=utf-8"}, {"content-length", "0"}, } if !reflect.DeepEqual(goth, wanth) { diff --git a/http2/server.go b/http2/server.go index 8e2a33c7..7a502263 100644 --- a/http2/server.go +++ b/http2/server.go @@ -2322,7 +2322,7 @@ func (rws *responseWriterState) writeChunk(p []byte) (n int, err error) { clen = strconv.Itoa(len(p)) } _, hasContentType := rws.snapHeader["Content-Type"] - if !hasContentType && bodyAllowedForStatus(rws.status) { + if !hasContentType && bodyAllowedForStatus(rws.status) && len(p) > 0 { ctype = http.DetectContentType(p) } var date string diff --git a/http2/server_test.go b/http2/server_test.go index 91db6a2c..bd1ba20d 100644 --- a/http2/server_test.go +++ b/http2/server_test.go @@ -1718,7 +1718,6 @@ func TestServer_Response_NoData_Header_FooBar(t *testing.T) { wanth := [][2]string{ {":status", "200"}, {"foo-bar", "some-value"}, - {"content-type", "text/plain; charset=utf-8"}, {"content-length", "0"}, } if !reflect.DeepEqual(goth, wanth) { @@ -2953,7 +2952,6 @@ func TestServerDoesntWriteInvalidHeaders(t *testing.T) { wanth := [][2]string{ {":status", "200"}, {"ok1", "x"}, - {"content-type", "text/plain; charset=utf-8"}, {"content-length", "0"}, } if !reflect.DeepEqual(goth, wanth) { @@ -3266,7 +3264,6 @@ func TestServerNoAutoContentLengthOnHead(t *testing.T) { headers := st.decodeHeader(h.HeaderBlockFragment()) want := [][2]string{ {":status", "200"}, - {"content-type", "text/plain; charset=utf-8"}, } if !reflect.DeepEqual(headers, want) { t.Errorf("Headers mismatch.\n got: %q\nwant: %q\n", headers, want)