Skip to content

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

Closed
@tw4452852

Description

@tw4452852

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions