Skip to content

Commit 51b2213

Browse files
JanBerktoldbradfitz
authored andcommitted
net/http: make Transport respect non lower case Content-Encoding
The existing Transport implementation does not detect gzip encoding when the Content-Encoding header is not lower-case. This is not compliant with RFC2616 section 3.5 "All content-coding values are case-insensitive." and caused issues in the wild. Fixes #19248 Change-Id: I1b49992832dc3c8ef700058596a27dd9909640a3 Reviewed-on: https://go-review.googlesource.com/37431 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent d4a8828 commit 51b2213

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/net/http/transport.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,7 @@ func (pc *persistConn) readLoop() {
16261626
}
16271627

16281628
resp.Body = body
1629-
if rc.addedGzip && resp.Header.Get("Content-Encoding") == "gzip" {
1629+
if rc.addedGzip && strings.EqualFold(resp.Header.Get("Content-Encoding"), "gzip") {
16301630
resp.Body = &gzipReader{body: body}
16311631
resp.Header.Del("Content-Encoding")
16321632
resp.Header.Del("Content-Length")

src/net/http/transport_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2900,6 +2900,40 @@ func TestTransportResponseCancelRace(t *testing.T) {
29002900
res.Body.Close()
29012901
}
29022902

2903+
// Test for issue 19248: Content-Encoding's value is case insensitive.
2904+
func TestTransportContentEncodingCaseInsensitive(t *testing.T) {
2905+
setParallel(t)
2906+
defer afterTest(t)
2907+
for _, ce := range []string{"gzip", "GZIP"} {
2908+
ce := ce
2909+
t.Run(ce, func(t *testing.T) {
2910+
const encodedString = "aaaa"
2911+
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
2912+
conn, _, _ := w.(Hijacker).Hijack()
2913+
fmt.Fprintf(conn, "HTTP/1.1 200 OK\r\nContent-Encoding: %s\r\nContent-Length: 28\r\n\r\n", ce)
2914+
conn.Write([]byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x4a\x4c\x4c\x4c\x04\x04\x00\x00\xff\xff\x45\xe5\x98\xad\x04\x00\x00\x00"))
2915+
conn.Close()
2916+
}))
2917+
defer ts.Close()
2918+
2919+
res, err := ts.Client().Get(ts.URL)
2920+
if err != nil {
2921+
t.Fatal(err)
2922+
}
2923+
2924+
body, err := ioutil.ReadAll(res.Body)
2925+
res.Body.Close()
2926+
if err != nil {
2927+
t.Fatal(err)
2928+
}
2929+
2930+
if string(body) != encodedString {
2931+
t.Fatalf("Expected body %q, got: %q\n", encodedString, string(body))
2932+
}
2933+
})
2934+
}
2935+
}
2936+
29032937
func TestTransportDialCancelRace(t *testing.T) {
29042938
defer afterTest(t)
29052939

0 commit comments

Comments
 (0)