Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions example_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,12 @@ func TestCompressionBrotli(t *testing.T) {
}
defer resp.Body.Close()

// HTTP/2 decodes the body even though the caller set Accept-Encoding, HTTP/1.1 only when it set it itself.
if resp.ProtoMajor == 2 {
if !resp.Uncompressed {
t.Fatal("brotli body was not decoded")
}
} else if h := resp.Header.Get("content-encoding"); h != "br" {
t.Fatalf("Got content-encoding header %v, expected br", h)
// Both protocols decode the body even though the caller set Accept-Encoding.
if !resp.Uncompressed {
t.Fatal("brotli body was not decoded")
}
if h := resp.Header.Get("content-encoding"); h != "" {
t.Fatalf("Got content-encoding header %v, expected none", h)
}
}

Expand Down Expand Up @@ -370,9 +369,9 @@ func deflateServer(t *testing.T, zlibWrapped bool) *httptest.Server {
}))
}

// A caller that sets accept-encoding itself owns the decoding: the transport
// must not decompress the body or strip Content-Encoding. Verify that, and
// that the library decodes both deflate flavours.
// The transport decodes by the response's Content-Encoding even when the caller
// set accept-encoding itself, and strips the header. Verify that for both deflate
// flavours.
func testCompressionDeflate(t *testing.T, zlibWrapped bool) {
t.Helper()

Expand All @@ -393,11 +392,11 @@ func testCompressionDeflate(t *testing.T, zlibWrapped bool) {
}
defer resp.Body.Close()

if h := resp.Header.Get("content-encoding"); h != "deflate" {
t.Fatalf("Expected content encoding deflate, got %q", h)
if h := resp.Header.Get("content-encoding"); h != "" {
t.Fatalf("Expected no content encoding, got %q", h)
}

got, err := io.ReadAll(http.DecompressBodyByType(resp.Body, "deflate"))
got, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
Expand Down
21 changes: 4 additions & 17 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -2190,7 +2190,7 @@ func (pc *persistConn) readLoop() {

resp.Body = body

if rc.addedGzip {
if !pc.t.DisableCompression {
resp.Body = DecompressBody(resp)
}

Expand Down Expand Up @@ -2467,11 +2467,6 @@ type requestAndChan struct {
cancelKey cancelKey
ch chan responseAndError // unbuffered; always send in select on callerGone

// whether the Transport (as opposed to the user client code)
// added the Accept-Encoding gzip header. If the Transport
// set it, only then do we transparently decode the gzip.
addedGzip bool

// Optional blocking chan for Expect: 100-continue (for send).
// If the request has an "Expect: 100-continue" header and
// the server responds 100 Continue, readLoop send a value
Expand Down Expand Up @@ -2541,11 +2536,10 @@ func (pc *persistConn) roundTrip(req *transportRequest) (resp *Response, err err
}

// Ask for a compressed version if the caller didn't set their
// own value for Accept-Encoding. We only attempt to
// uncompress the gzip stream if we were the layer that
// requested it.
// own value for Accept-Encoding. The response body is
// transparently decoded based on the response's actual
// Content-Encoding regardless of which layer asked for it.

requestedGzip := false
if !pc.t.DisableCompression &&
req.Header.Get("Accept-Encoding") == "" &&
req.Header.get("accept-encoding") == "" &&
Expand All @@ -2562,15 +2556,9 @@ func (pc *persistConn) roundTrip(req *transportRequest) (resp *Response, err err
// We don't request gzip if the request is for a range, since
// auto-decoding a portion of a gzipped document will just fail
// anyway. See https://golang.org/issue/8923
requestedGzip = true
req.extraHeaders().Set("Accept-Encoding", "gzip, deflate, br, zstd")
}

// Set gzip to true if the caller's Accept-Encoding includes gzip.
if !pc.t.DisableCompression && (strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") || strings.Contains(req.Request.Header.get("accept-encoding"), "gzip")) {
requestedGzip = true
}

var continueCh chan struct{}
if req.ProtoAtLeast(1, 1) && req.Body != nil && req.expectsContinue() {
continueCh = make(chan struct{}, 1)
Expand Down Expand Up @@ -2605,7 +2593,6 @@ func (pc *persistConn) roundTrip(req *transportRequest) (resp *Response, err err
req: req.Request,
cancelKey: req.cancelKey,
ch: resc,
addedGzip: requestedGzip,
continueCh: continueCh,
callerGone: gone,
}
Expand Down