Skip to content

Commit 23ce3b8

Browse files
laurentsentagopherbot
authored andcommitted
http2: disable Content-Length when nilled
Change-Id: Iefef8dc1004a8e889d0e9f7243f594ae7b727a07 Reviewed-on: https://go-review.googlesource.com/c/net/+/471535 Reviewed-by: Damien Neil <[email protected]> Auto-Submit: Damien Neil <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Jorropo <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Run-TryBot: Damien Neil <[email protected]>
1 parent 120fc90 commit 23ce3b8

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

http2/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2569,7 +2569,8 @@ func (rws *responseWriterState) writeChunk(p []byte) (n int, err error) {
25692569
clen = ""
25702570
}
25712571
}
2572-
if clen == "" && rws.handlerDone && bodyAllowedForStatus(rws.status) && (len(p) > 0 || !isHeadResp) {
2572+
_, hasContentLength := rws.snapHeader["Content-Length"]
2573+
if !hasContentLength && clen == "" && rws.handlerDone && bodyAllowedForStatus(rws.status) && (len(p) > 0 || !isHeadResp) {
25732574
clen = strconv.Itoa(len(p))
25742575
}
25752576
_, hasContentType := rws.snapHeader["Content-Type"]

http2/server_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3555,6 +3555,30 @@ func TestServerNoDuplicateContentType(t *testing.T) {
35553555
}
35563556
}
35573557

3558+
func TestServerContentLengthCanBeDisabled(t *testing.T) {
3559+
st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
3560+
w.Header()["Content-Length"] = nil
3561+
fmt.Fprintf(w, "OK")
3562+
})
3563+
defer st.Close()
3564+
st.greet()
3565+
st.writeHeaders(HeadersFrameParam{
3566+
StreamID: 1,
3567+
BlockFragment: st.encodeHeader(),
3568+
EndStream: true,
3569+
EndHeaders: true,
3570+
})
3571+
h := st.wantHeaders()
3572+
headers := st.decodeHeader(h.HeaderBlockFragment())
3573+
want := [][2]string{
3574+
{":status", "200"},
3575+
{"content-type", "text/plain; charset=utf-8"},
3576+
}
3577+
if !reflect.DeepEqual(headers, want) {
3578+
t.Errorf("Headers mismatch.\n got: %q\nwant: %q\n", headers, want)
3579+
}
3580+
}
3581+
35583582
func disableGoroutineTracking() (restore func()) {
35593583
old := DebugGoroutines
35603584
DebugGoroutines = false

0 commit comments

Comments
 (0)