]> Cypherpunks.ru repositories - gostls13.git/commitdiff
net/http: second do not force the Content-Length header if nilled
authorJorropo <jorropo.pgm@gmail.com>
Tue, 16 May 2023 07:19:58 +0000 (09:19 +0200)
committerGopher Robot <gobot@golang.org>
Wed, 24 May 2023 22:39:33 +0000 (22:39 +0000)
This is a second round of CL 469095 which has been fixed after
the issue discovered in the revert CL 495017.

The issue was a missing res.Body.Close() in the newly added test.

Change-Id: Ifd9d8458022e59f4486397443a2862d06383e990
Reviewed-on: https://go-review.googlesource.com/c/go/+/495115
Reviewed-by: Damien Neil <dneil@google.com>
Run-TryBot: Jorropo <jorropo.pgm@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>

src/net/http/serve_test.go
src/net/http/server.go

index 33a7b54d5d903f2c4b205e76e183a9a1690aa99c..b712f92cb7148c55dade134d9bdf94b8d4ac0190 100644 (file)
@@ -6825,3 +6825,43 @@ func testHeadBody(t *testing.T, mode testMode, chunked bool, method string) {
                }
        }
 }
+
+// TestDisableContentLength 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.Fatal(err)
+       }
+       if got, haveCL := res.Header["Content-Length"]; haveCL {
+               t.Errorf("Unexpected Content-Length: %q", got)
+       }
+       if err := res.Body.Close(); err != nil {
+               t.Fatal(err)
+       }
+
+       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.Fatal(err)
+       }
+       if got := res.Header.Get("Content-Length"); got != "2" {
+               t.Errorf("Content-Length: %q; want 2", got)
+       }
+       if err := res.Body.Close(); err != nil {
+               t.Fatal(err)
+       }
+}
index efdc031740081f8453850d0cd8422d2140c8226b..680c5f68f425fa2a4e01af43d5180e3e3aa55389 100644 (file)
@@ -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.get("Content-Length") == "" && (!isHEAD || len(p) > 0) {
+       if w.handlerDone.Load() && !trailers && !hasTE && bodyAllowedForStatus(w.status) && !header.has("Content-Length") && (!isHEAD || len(p) > 0) {
                w.contentLength = int64(len(p))
                setHeader.contentLength = strconv.AppendInt(cw.res.clenBuf[:0], int64(len(p)), 10)
        }