From c1ce2a6e83115c4480c5bc1f2bc3e31a6b645253 Mon Sep 17 00:00:00 2001 From: 1-1-2 <50569812+1-1-2@users.noreply.github.com> Date: Tue, 13 Aug 2024 16:14:57 +0000 Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E4=BF=AE=E5=A4=8D=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E4=BF=A1=E6=81=AF=E4=B9=B1=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/net/serve.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/internal/net/serve.go b/internal/net/serve.go index adee75ae1d6..e58d7eb9f46 100644 --- a/internal/net/serve.go +++ b/internal/net/serve.go @@ -1,6 +1,7 @@ package net import ( + "compress/gzip" "context" "fmt" "io" @@ -222,8 +223,19 @@ func RequestHttp(ctx context.Context, httpMethod string, headerOverride http.Hea } // TODO clean header with blocklist or passlist res.Header.Del("set-cookie") + var reader io.Reader if res.StatusCode >= 400 { - all, _ := io.ReadAll(res.Body) + // 根据 Content-Encoding 判断 Body 是否压缩 + switch res.Header.Get("Content-Encoding") { + case "gzip": + // 使用gzip.NewReader解压缩 + reader, _ = gzip.NewReader(res.Body) + defer reader.(*gzip.Reader).Close() + default: + // 没有Content-Encoding,直接读取 + reader = res.Body + } + all, _ := io.ReadAll(reader) _ = res.Body.Close() msg := string(all) log.Debugln(msg)