From 42b20297d314bd72195e9abff55b0c607c2619a8 Mon Sep 17 00:00:00 2001 From: "carl.tao" Date: Thu, 21 Sep 2023 23:38:33 +0800 Subject: [PATCH] net/http: remove Content-Encoding header in roundtrip_js The fetch api will decode the gzip, but Content-Encoding not be deleted. To ensure that the behavior of roundtrip_js is consistent with native. delete the Content-Encoding header when the response body is decompressed by js fetch api. Fixes #63139 Change-Id: Ie35b3aa050786e2ef865f9ffa992e30ab060506e Reviewed-on: https://go-review.googlesource.com/c/go/+/530155 Commit-Queue: Damien Neil Reviewed-by: Bryan Mills Reviewed-by: Damien Neil LUCI-TryBot-Result: Go LUCI Auto-Submit: Damien Neil --- src/net/http/roundtrip_js.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/net/http/roundtrip_js.go b/src/net/http/roundtrip_js.go index 9f9f0cb67d..cbf978af18 100644 --- a/src/net/http/roundtrip_js.go +++ b/src/net/http/roundtrip_js.go @@ -10,6 +10,7 @@ import ( "errors" "fmt" "io" + "net/http/internal/ascii" "strconv" "strings" "syscall/js" @@ -184,11 +185,22 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) { } code := result.Get("status").Int() + + uncompressed := false + if ascii.EqualFold(header.Get("Content-Encoding"), "gzip") { + // The fetch api will decode the gzip, but Content-Encoding not be deleted. + header.Del("Content-Encoding") + header.Del("Content-Length") + contentLength = -1 + uncompressed = true + } + respCh <- &Response{ Status: fmt.Sprintf("%d %s", code, StatusText(code)), StatusCode: code, Header: header, ContentLength: contentLength, + Uncompressed: uncompressed, Body: body, Request: req, } -- 2.50.0