mirror of https://github.com/Xhofe/alist
fix: decode body if enable gzip (#7003)
parent
1f652e2e7d
commit
51c95ee117
|
@ -1,6 +1,7 @@
|
||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"compress/gzip"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
@ -222,8 +223,19 @@ func RequestHttp(ctx context.Context, httpMethod string, headerOverride http.Hea
|
||||||
}
|
}
|
||||||
// TODO clean header with blocklist or passlist
|
// TODO clean header with blocklist or passlist
|
||||||
res.Header.Del("set-cookie")
|
res.Header.Del("set-cookie")
|
||||||
|
var reader io.Reader
|
||||||
if res.StatusCode >= 400 {
|
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()
|
_ = res.Body.Close()
|
||||||
msg := string(all)
|
msg := string(all)
|
||||||
log.Debugln(msg)
|
log.Debugln(msg)
|
||||||
|
|
Loading…
Reference in New Issue