http2: disable Content-Length when nilled

Change-Id: Iefef8dc1004a8e889d0e9f7243f594ae7b727a07
Reviewed-on: https://go-review.googlesource.com/c/net/+/471535
Reviewed-by: Damien Neil <dneil@google.com>
Auto-Submit: Damien Neil <dneil@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
Run-TryBot: Damien Neil <dneil@google.com>
This commit is contained in:
Laurent Senta
2023-02-27 10:49:56 +01:00
committed by Gopher Robot
parent 120fc906b3
commit 23ce3b89bc
2 changed files with 26 additions and 1 deletions

View File

@@ -2569,7 +2569,8 @@ func (rws *responseWriterState) writeChunk(p []byte) (n int, err error) {
clen = ""
}
}
if clen == "" && rws.handlerDone && bodyAllowedForStatus(rws.status) && (len(p) > 0 || !isHeadResp) {
_, hasContentLength := rws.snapHeader["Content-Length"]
if !hasContentLength && clen == "" && rws.handlerDone && bodyAllowedForStatus(rws.status) && (len(p) > 0 || !isHeadResp) {
clen = strconv.Itoa(len(p))
}
_, hasContentType := rws.snapHeader["Content-Type"]

View File

@@ -3555,6 +3555,30 @@ func TestServerNoDuplicateContentType(t *testing.T) {
}
}
func TestServerContentLengthCanBeDisabled(t *testing.T) {
st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
w.Header()["Content-Length"] = nil
fmt.Fprintf(w, "OK")
})
defer st.Close()
st.greet()
st.writeHeaders(HeadersFrameParam{
StreamID: 1,
BlockFragment: st.encodeHeader(),
EndStream: true,
EndHeaders: true,
})
h := st.wantHeaders()
headers := st.decodeHeader(h.HeaderBlockFragment())
want := [][2]string{
{":status", "200"},
{"content-type", "text/plain; charset=utf-8"},
}
if !reflect.DeepEqual(headers, want) {
t.Errorf("Headers mismatch.\n got: %q\nwant: %q\n", headers, want)
}
}
func disableGoroutineTracking() (restore func()) {
old := DebugGoroutines
DebugGoroutines = false