mirror of
https://github.com/golang/go
synced 2024-11-23 15:40:06 -07:00
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 <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
d4a8828eee
commit
51b22130b5
@ -1626,7 +1626,7 @@ func (pc *persistConn) readLoop() {
|
||||
}
|
||||
|
||||
resp.Body = body
|
||||
if rc.addedGzip && resp.Header.Get("Content-Encoding") == "gzip" {
|
||||
if rc.addedGzip && strings.EqualFold(resp.Header.Get("Content-Encoding"), "gzip") {
|
||||
resp.Body = &gzipReader{body: body}
|
||||
resp.Header.Del("Content-Encoding")
|
||||
resp.Header.Del("Content-Length")
|
||||
|
@ -2900,6 +2900,40 @@ func TestTransportResponseCancelRace(t *testing.T) {
|
||||
res.Body.Close()
|
||||
}
|
||||
|
||||
// Test for issue 19248: Content-Encoding's value is case insensitive.
|
||||
func TestTransportContentEncodingCaseInsensitive(t *testing.T) {
|
||||
setParallel(t)
|
||||
defer afterTest(t)
|
||||
for _, ce := range []string{"gzip", "GZIP"} {
|
||||
ce := ce
|
||||
t.Run(ce, func(t *testing.T) {
|
||||
const encodedString = "aaaa"
|
||||
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
|
||||
conn, _, _ := w.(Hijacker).Hijack()
|
||||
fmt.Fprintf(conn, "HTTP/1.1 200 OK\r\nContent-Encoding: %s\r\nContent-Length: 28\r\n\r\n", ce)
|
||||
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"))
|
||||
conn.Close()
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
res, err := ts.Client().Get(ts.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
res.Body.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if string(body) != encodedString {
|
||||
t.Fatalf("Expected body %q, got: %q\n", encodedString, string(body))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransportDialCancelRace(t *testing.T) {
|
||||
defer afterTest(t)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user