summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAustin Clements <austin@google.com>2023-05-15 21:19:08 +0000
committerGopher Robot <gobot@golang.org>2023-05-15 21:31:16 +0000
commit7213f2e72003325df2cebb731de838ac01f20fb6 (patch)
treea65d994d3f08eda3fa71f46c0dfbd7c9af264417 /src
parentff3aefbad4bed0cdd25688329e5cc4f908276a46 (diff)
downloadgo-git-7213f2e72003325df2cebb731de838ac01f20fb6.tar.gz
Revert "net/http: do not force the Content-Length header if nilled"
This reverts CL 469095. The newly added TestDisableContentLength is failing on all longtest builders. Change-Id: Id307df61c7bf80691d9c276e8d200eebf6d4a59c Reviewed-on: https://go-review.googlesource.com/c/go/+/495017 Auto-Submit: Austin Clements <austin@google.com> Run-TryBot: Austin Clements <austin@google.com> Reviewed-by: Damien Neil <dneil@google.com> Reviewed-by: Heschi Kreinick <heschi@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/net/http/serve_test.go34
-rw-r--r--src/net/http/server.go2
2 files changed, 1 insertions, 35 deletions
diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go
index 12f6b768bd..33a7b54d5d 100644
--- a/src/net/http/serve_test.go
+++ b/src/net/http/serve_test.go
@@ -6825,37 +6825,3 @@ func testHeadBody(t *testing.T, mode testMode, chunked bool, method string) {
}
}
}
-
-// TestContentLengthResponseCanBeNilled verifies that the Content-Length is set by default
-// or disabled when the header is set to nil.
-func TestDisableContentLength(t *testing.T) { run(t, testDisableContentLength) }
-func testDisableContentLength(t *testing.T, mode testMode) {
- if mode == http2Mode {
- t.Skip("skipping until h2_bundle.go is updated; see https://go-review.googlesource.com/c/net/+/471535")
- }
-
- noCL := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
- w.Header()["Content-Length"] = nil // disable the default Content-Length response
- fmt.Fprintf(w, "OK")
- }))
-
- res, err := noCL.c.Get(noCL.ts.URL)
- if err != nil {
- t.Error(err)
- }
- if got, haveCL := res.Header["Content-Length"]; haveCL {
- t.Errorf("Unexpected Content-Length: %q", got)
- }
-
- withCL := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
- fmt.Fprintf(w, "OK")
- }))
-
- res, err = withCL.c.Get(withCL.ts.URL)
- if err != nil {
- t.Error(err)
- }
- if got := res.Header.Get("Content-Length"); got != "2" {
- t.Errorf("Content-Length: %q; want 2", got)
- }
-}
diff --git a/src/net/http/server.go b/src/net/http/server.go
index 680c5f68f4..efdc031740 100644
--- a/src/net/http/server.go
+++ b/src/net/http/server.go
@@ -1320,7 +1320,7 @@ func (cw *chunkWriter) writeHeader(p []byte) {
// send a Content-Length header.
// Further, we don't send an automatic Content-Length if they
// set a Transfer-Encoding, because they're generally incompatible.
- if w.handlerDone.Load() && !trailers && !hasTE && bodyAllowedForStatus(w.status) && !header.has("Content-Length") && (!isHEAD || len(p) > 0) {
+ if w.handlerDone.Load() && !trailers && !hasTE && bodyAllowedForStatus(w.status) && header.get("Content-Length") == "" && (!isHEAD || len(p) > 0) {
w.contentLength = int64(len(p))
setHeader.contentLength = strconv.AppendInt(cw.res.clenBuf[:0], int64(len(p)), 10)
}