Skip to content

Commit 0ec6256

Browse files
committed
net/http: pass through server side Transfer-Encoding headers
Fixes #16063 Change-Id: I2e8695beb657b0aef067e83f086828d8857787ed Reviewed-on: https://go-review.googlesource.com/24130 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent c4692da commit 0ec6256

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/net/http/serve_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package http_test
99
import (
1010
"bufio"
1111
"bytes"
12+
"compress/gzip"
1213
"context"
1314
"crypto/tls"
1415
"encoding/json"
@@ -4200,6 +4201,24 @@ func TestHandlerSetTransferEncodingChunked(t *testing.T) {
42004201
}
42014202
}
42024203

4204+
// https://golang.org/issue/16063
4205+
func TestHandlerSetTransferEncodingGzip(t *testing.T) {
4206+
defer afterTest(t)
4207+
ht := newHandlerTest(HandlerFunc(func(w ResponseWriter, r *Request) {
4208+
w.Header().Set("Transfer-Encoding", "gzip")
4209+
gz := gzip.NewWriter(w)
4210+
gz.Write([]byte("hello"))
4211+
gz.Close()
4212+
}))
4213+
resp := ht.rawResponse("GET / HTTP/1.1\nHost: foo")
4214+
for _, v := range []string{"gzip", "chunked"} {
4215+
hdr := "Transfer-Encoding: " + v
4216+
if n := strings.Count(resp, hdr); n != 1 {
4217+
t.Errorf("want 1 occurrence of %q in response, got %v\nresponse: %v", hdr, n, resp)
4218+
}
4219+
}
4220+
}
4221+
42034222
func BenchmarkClientServer(b *testing.B) {
42044223
b.ReportAllocs()
42054224
b.StopTimer()

src/net/http/server.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,10 @@ 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")
1150+
if hasTE && te == "chunked" {
1151+
// We will send the chunked Transfer-Encoding header later.
1152+
delHeader("Transfer-Encoding")
1153+
}
11511154
}
11521155
} else {
11531156
// HTTP version < 1.1: cannot do chunked transfer

0 commit comments

Comments
 (0)