Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:gzip body导致的乱码 #7003

Merged
merged 1 commit into from
Aug 15, 2024
Merged

fix:gzip body导致的乱码 #7003

merged 1 commit into from
Aug 15, 2024

Conversation

1-1-2
Copy link
Contributor

@1-1-2 1-1-2 commented Aug 13, 2024

观察到日志中出现以下乱码内容

ERRO[2024-08-14 01:12:26] http request [https://d.pcs.baidu.com/*] failure,status: 403 response:�-K�@0�ЫLfE?A2R:1iE-ߍ$r0�08lʌ�\a8p��Υ/33ř �QyQCN�|M{ζzm��]i

排查原因,判断是在处理HTTP响应时,Body是经过gzip压缩的。在这种情况下,需要先解压缩Body才能读取内容

image

修复后

ERRO[2024-08-14 01:16:07] http request [https://d.pcs.baidu.com/*] failure,status: 403 response:{"error_code":31329,"error_msg":"hit black userlist , hit illeage dlna","request_id":870*918065}

可正常读取body

Copy link

welcome bot commented Aug 13, 2024

Thanks for opening this pull request! Please check out our contributing guidelines.

@1-1-2 1-1-2 changed the title 修复错误信息乱码 fix:gzip body导致的乱码 Aug 13, 2024
@1-1-2
Copy link
Contributor Author

1-1-2 commented Aug 14, 2024

文档中提到

DisableCompression(如果为true)可防止传输在请求不包含现有Accept-Encoding值时使用“Accept-Encoding:gzip”请求标头请求压缩。如果Transport自己请求gzip并得到gzip压缩的响应,它将在response中透明地解码。身体。但是,如果用户明确请求gzip,它不会自动解压缩。

http.transport是能透明地对gzip body实行解压的,前提是未显式请求gzip。但排查了一下代码,没能找到病根。目前这个方法姑且算是治标而已。猜测是某个过程添加了空白的Accept-Encoding: 请求头,但能力有限未排查到。


附:GPT解读

在Go的http.Transport中,DisableCompression字段控制了HTTP请求是否自动包含Accept-Encoding: gzip请求头,并且影响响应是否自动解压缩。

关键点解释

  1. DisableCompression: false(默认值)

    • 请求行为:当客户端发送HTTP请求时,如果请求头中没有指定Accept-EncodingTransport会自动添加Accept-Encoding: gzip,表示客户端支持gzip压缩。
    • 响应行为:如果服务器返回了gzip压缩的响应(Content-Encoding: gzip),客户端会自动解压缩响应内容,即Response.Body中包含的是解压缩后的数据。
  2. DisableCompression: true

    • 请求行为:客户端不会自动添加Accept-Encoding: gzip请求头,除非你在请求中明确设置了这个头部。
    • 响应行为:如果你明确在请求中设置了Accept-Encoding: gzip,客户端不会自动解压缩响应内容,Response.Body会包含原始的压缩数据。

@xhofe xhofe merged commit 51c95ee into AlistGo:main Aug 15, 2024
3 checks passed
Copy link

welcome bot commented Aug 15, 2024

Congrats on merging your first pull request! We here at behavior bot are proud of you!

@foxxorcat foxxorcat mentioned this pull request Sep 7, 2024
Three-taile-dragon pushed a commit to loognsss/blist that referenced this pull request Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants