Skip to content

Commit f9b4556

Browse files
committed
net/http: send one Transfer-Encoding header when "chunked" set manually
Fixes #15960 Change-Id: I7503f6ede33e6a1a93cee811d40f7b297edf47bc Reviewed-on: https://go-review.googlesource.com/23811 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent a871464 commit f9b4556

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/net/http/serve_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4166,6 +4166,20 @@ func testServerContext_ServerContextKey(t *testing.T, h2 bool) {
41664166
res.Body.Close()
41674167
}
41684168

4169+
// https://golang.org/issue/15960
4170+
func TestHandlerSetTransferEncodingChunked(t *testing.T) {
4171+
defer afterTest(t)
4172+
ht := newHandlerTest(HandlerFunc(func(w ResponseWriter, r *Request) {
4173+
w.Header().Set("Transfer-Encoding", "chunked")
4174+
w.Write([]byte("hello"))
4175+
}))
4176+
resp := ht.rawResponse("GET / HTTP/1.1\nHost: foo")
4177+
const hdr = "Transfer-Encoding: chunked"
4178+
if n := strings.Count(resp, hdr); n != 1 {
4179+
t.Errorf("want 1 occurrence of %q in response, got %v\nresponse: %v", hdr, n, resp)
4180+
}
4181+
}
4182+
41694183
func BenchmarkClientServer(b *testing.B) {
41704184
b.ReportAllocs()
41714185
b.StopTimer()

src/net/http/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,7 @@ func (cw *chunkWriter) writeHeader(p []byte) {
11471147
// to avoid closing the connection at EOF.
11481148
cw.chunking = true
11491149
setHeader.transferEncoding = "chunked"
1150+
delHeader("Transfer-Encoding")
11501151
}
11511152
} else {
11521153
// HTTP version < 1.1: cannot do chunked transfer

0 commit comments

Comments
 (0)