Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: Content-Type is set even with empty body #20784

Closed
tw4452852 opened this issue Jun 24, 2017 · 8 comments

Comments

Projects
None yet
3 participants
@tw4452852
Copy link
Contributor

commented Jun 24, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version devel +1948b7f Wed May 31 10:28:05 2017 +0000 linux/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/tw/golib"
GORACE=""
GOROOT="/home/tw/goroot"
GOTOOLDIR="/home/tw/goroot/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build555764928=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config

What did you do?

package main

import (
        "fmt"
        "log"
        "net/http"
        "net/http/httptest"
        "net/http/httputil"
        "net/url"
)

func main() {
        backendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                w.WriteHeader(http.StatusOK)
        }))
        defer backendServer.Close()

        rpURL, err := url.Parse(backendServer.URL)
        if err != nil {
                log.Fatal(err)
        }
        frontendProxy := httptest.NewServer(httputil.NewSingleHostReverseProxy(rpURL))
        defer frontendProxy.Close()

        resp, err := http.Get(frontendProxy.URL)
        if err != nil {
                log.Fatal(err)
        }

        fmt.Printf("response header: %#v\n", resp.Header)
}

What did you expect to see?

Content-Type should not be set with empty content.

What did you see instead?

Content-Type is set automatically by Go http server.

response header: http.Header{"Content-Length":[]string{"0"}, "Content-Type":[]string{"text/plain; charset=utf-8"}, "Date":[]string{"Sat, 24 Jun 2017 05:18:32 GMT"}}

I suppose this patch should work:

diff --git a/src/net/http/server.go b/src/net/http/server.go
index add05c2..a48b88b 100644
--- a/src/net/http/server.go
+++ b/src/net/http/server.go
@@ -1304,7 +1304,7 @@ func (cw *chunkWriter) writeHeader(p []byte) {
        if bodyAllowedForStatus(code) {
                // If no content type, apply sniffing algorithm to body.
                _, haveType := header["Content-Type"]
-               if !haveType && !hasTE {
+               if !haveType && !hasTE && len(p) > 0 {
                        setHeader.contentType = DetectContentType(p)
                }
        } else {

Any thoughts ?

@bradfitz bradfitz added this to the Go1.10 milestone Jun 24, 2017

@bradfitz

This comment has been minimized.

Copy link
Member

commented Jun 24, 2017

Sure, seems fine to change. It's harmless as-is and not wrong, but not sending it saves some bandwidth at least.

Feel free to send a change + test: see https://tip.golang.org/doc/contribute.html

@gopherbot

This comment has been minimized.

Copy link

commented Jun 24, 2017

CL https://golang.org/cl/46631 mentions this issue.

@tw4452852

This comment has been minimized.

Copy link
Contributor Author

commented Jun 24, 2017

@bradfitz Yes, it's alright in most cases, but some clients don't expect this header when they encounter a response with a empty blank. I find this when resolving this caddy issue.

@gopherbot

This comment has been minimized.

Copy link

commented Nov 28, 2017

Change https://golang.org/cl/80135 mentions this issue: http2: don't autodetect Content-Type when the response has an empty body

gopherbot pushed a commit to golang/net that referenced this issue Nov 28, 2017

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 <tombergan@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
@gopherbot

This comment has been minimized.

Copy link

commented Nov 28, 2017

Change https://golang.org/cl/80078 mentions this issue: net/http: update bundled http2

gopherbot pushed a commit that referenced this issue Nov 28, 2017

net/http: update bundled http2
Update http2 to x/net git rev db473f6b23.

(And un-skip TestWriteHeader0_h2 added in CL 80077, now fixed.)

Includes:

   http2: remove afterReqBodyWriteError wrapper
   https://golang.org/cl/75252

   http2: fix transport data race on reused *http.Request objects
   https://golang.org/cl/75530

   http2: require either ECDSA or RSA ciphersuite
   https://golang.org/cl/30721

   http2: don't log about timeouts reading client preface on new connections
   https://golang.org/cl/79498

   http2: don't crash in Transport on server's DATA following bogus HEADERS
   https://golang.org/cl/80056

   http2: panic on invalid WriteHeader status code
   https://golang.org/cl/80076

   http2: fix race on ClientConn.maxFrameSize
   https://golang.org/cl/79238

   http2: don't autodetect Content-Type when the response has an empty body
   https://golang.org/cl/80135

Fixes #18776
Updates #20784
Fixes #21316
Fixes #22721
Fixes #22880

Change-Id: Ie86e24e0ee2582a5a82afe5de3c7c801528be069
Reviewed-on: https://go-review.googlesource.com/80078
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Tom Bergan <tombergan@google.com>
@gopherbot

This comment has been minimized.

Copy link

commented Dec 5, 2017

Change https://golang.org/cl/82016 mentions this issue: doc: mention net/http Server Content-Type change

@gopherbot

This comment has been minimized.

Copy link

commented Dec 5, 2017

Change https://golang.org/cl/82017 mentions this issue: internal: fix test on Go 1.10

gopherbot pushed a commit to golang/oauth2 that referenced this issue Dec 5, 2017

internal: fix test on Go 1.10
Go 1.10 no longer sets implicit Content-Type on empty output.

Updates golang/go#20784

Change-Id: I3f13f76b94b58869481218ea2e1805f5f4175fd7
Reviewed-on: https://go-review.googlesource.com/82017
Reviewed-by: Ian Lance Taylor <iant@golang.org>

gopherbot pushed a commit that referenced this issue Feb 13, 2018

doc: mention net/http Server Content-Type change
Updates #20784

Change-Id: Ic07c78a86da5026e407ac9ecb3117d320c198048
Reviewed-on: https://go-review.googlesource.com/82016
Reviewed-by: Andrew Bonventre <andybons@golang.org>
@gopherbot

This comment has been minimized.

Copy link

commented Feb 13, 2018

Change https://golang.org/cl/93695 mentions this issue: [release-branch.go1.10] doc: mention net/http Server Content-Type change

gopherbot pushed a commit that referenced this issue Feb 15, 2018

[release-branch.go1.10] doc: mention net/http Server Content-Type change
Updates #20784

Change-Id: Ic07c78a86da5026e407ac9ecb3117d320c198048
Reviewed-on: https://go-review.googlesource.com/93695
Reviewed-by: Andrew Bonventre <andybons@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>

c3mb0 pushed a commit to c3mb0/net that referenced this issue Apr 2, 2018

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 <tombergan@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>

jasonwbarnett pushed a commit to jasonwbarnett/fileserver that referenced this issue Jul 11, 2018

net/http: update bundled http2
Update http2 to x/net git rev db473f6b23.

(And un-skip TestWriteHeader0_h2 added in CL 80077, now fixed.)

Includes:

   http2: remove afterReqBodyWriteError wrapper
   https://golang.org/cl/75252

   http2: fix transport data race on reused *http.Request objects
   https://golang.org/cl/75530

   http2: require either ECDSA or RSA ciphersuite
   https://golang.org/cl/30721

   http2: don't log about timeouts reading client preface on new connections
   https://golang.org/cl/79498

   http2: don't crash in Transport on server's DATA following bogus HEADERS
   https://golang.org/cl/80056

   http2: panic on invalid WriteHeader status code
   https://golang.org/cl/80076

   http2: fix race on ClientConn.maxFrameSize
   https://golang.org/cl/79238

   http2: don't autodetect Content-Type when the response has an empty body
   https://golang.org/cl/80135

Fixes golang/go#18776
Updates golang/go#20784
Fixes golang/go#21316
Fixes golang/go#22721
Fixes golang/go#22880

Change-Id: Ie86e24e0ee2582a5a82afe5de3c7c801528be069
Reviewed-on: https://go-review.googlesource.com/80078
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Tom Bergan <tombergan@google.com>

@golang golang locked and limited conversation to collaborators Feb 13, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.