Skip to content

Commit

Permalink
Revert "net/http: do not force the Content-Length header if nilled"
Browse files Browse the repository at this point in the history
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 <[email protected]>
Run-TryBot: Austin Clements <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
Reviewed-by: Heschi Kreinick <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
aclements authored and gopherbot committed May 15, 2023
1 parent ff3aefb commit 7213f2e
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 35 deletions.
34 changes: 0 additions & 34 deletions src/net/http/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion src/net/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 7213f2e

Please sign in to comment.