Skip to content

Commit 949d0f4

Browse files
laurentsentagopherbot
authored andcommitted
net/http: do not force the Content-Length header if nilled
According to the ResponseWriter documentation: To suppress automatic response headers (such as "Date"), set their value to nil. In some cases, this documentation is incorrect: chunkWriter writes a Content-Length header even if the value was set to nil. Meaning there is no way to suppress this header. This patch replaces the empty string comparison with a call to `header.has` which takes into account nil values as expected. This is similar to the way we handle the "Date" header. Change-Id: Ie10d54ab0bb7d41270bc944ff867e035fe2bd0c5 GitHub-Last-Rev: e0616dd GitHub-Pull-Request: #58578 Reviewed-on: https://go-review.googlesource.com/c/go/+/469095 Reviewed-by: Heschi Kreinick <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Damien Neil <[email protected]> Run-TryBot: Jorropo <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent 1b896bf commit 949d0f4

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/net/http/serve_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6756,3 +6756,37 @@ func testHeadBody(t *testing.T, mode testMode, chunked bool, method string) {
67566756
}
67576757
}
67586758
}
6759+
6760+
// TestContentLengthResponseCanBeNilled verifies that the Content-Length is set by default
6761+
// or disabled when the header is set to nil.
6762+
func TestDisableContentLength(t *testing.T) { run(t, testDisableContentLength) }
6763+
func testDisableContentLength(t *testing.T, mode testMode) {
6764+
if mode == http2Mode {
6765+
t.Skip("skipping until h2_bundle.go is updated; see https://go-review.googlesource.com/c/net/+/471535")
6766+
}
6767+
6768+
noCL := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
6769+
w.Header()["Content-Length"] = nil // disable the default Content-Length response
6770+
fmt.Fprintf(w, "OK")
6771+
}))
6772+
6773+
res, err := noCL.c.Get(noCL.ts.URL)
6774+
if err != nil {
6775+
t.Error(err)
6776+
}
6777+
if got, haveCL := res.Header["Content-Length"]; haveCL {
6778+
t.Errorf("Unexpected Content-Length: %q", got)
6779+
}
6780+
6781+
withCL := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
6782+
fmt.Fprintf(w, "OK")
6783+
}))
6784+
6785+
res, err = withCL.c.Get(withCL.ts.URL)
6786+
if err != nil {
6787+
t.Error(err)
6788+
}
6789+
if got := res.Header.Get("Content-Length"); got != "2" {
6790+
t.Errorf("Content-Length: %q; want 2", got)
6791+
}
6792+
}

src/net/http/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@ func (cw *chunkWriter) writeHeader(p []byte) {
13171317
// send a Content-Length header.
13181318
// Further, we don't send an automatic Content-Length if they
13191319
// set a Transfer-Encoding, because they're generally incompatible.
1320-
if w.handlerDone.Load() && !trailers && !hasTE && bodyAllowedForStatus(w.status) && header.get("Content-Length") == "" && (!isHEAD || len(p) > 0) {
1320+
if w.handlerDone.Load() && !trailers && !hasTE && bodyAllowedForStatus(w.status) && !header.has("Content-Length") && (!isHEAD || len(p) > 0) {
13211321
w.contentLength = int64(len(p))
13221322
setHeader.contentLength = strconv.AppendInt(cw.res.clenBuf[:0], int64(len(p)), 10)
13231323
}

0 commit comments

Comments
 (0)