Skip to content

Commit

Permalink
fix: decode body if enable gzip (AlistGo#7003)
Browse files Browse the repository at this point in the history
  • Loading branch information
1-1-2 authored and Three-taile-dragon committed Sep 26, 2024
1 parent b816a52 commit 45d66ce
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion internal/net/serve.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net

import (
"compress/gzip"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 45d66ce

Please sign in to comment.