当从 HTTP 响应头的 Content-Disposition 字段获取文件名时,即使指定了 encoding 参数,程序也没有…#3
Open
werwerwerwerwerwerwerwerwer wants to merge 1 commit into
Open
当从 HTTP 响应头的 Content-Disposition 字段获取文件名时,即使指定了 encoding 参数,程序也没有…#3werwerwerwerwerwerwerwerwer wants to merge 1 commit into
Content-Disposition 字段获取文件名时,即使指定了 encoding 参数,程序也没有…#3werwerwerwerwerwerwerwerwer wants to merge 1 commit into
Conversation
…按照指定的编码格式正确解码文件名,导致文件名出现乱码。
**示例问题:**
- 服务器使用 GBK 编码文件名:`测试文件.txt`
- 响应头中的文件名(被当作 ISO-8859-1 读取):`Content-Disposition: attachment; filename="²âÊÔÎļþ.txt"`
- 期望文件名:`测试文件.txt`
- 实际结果:乱码字符 `²âÊÔÎļþ.txt`
**问题原因:**
HTTP 响应头默认使用 ISO-8859-1 编码传输。当服务器使用其他编码(如 GBK、CP932、UTF-8)编码文件名时,这些字节序列会被 HTTP 客户端当作 ISO-8859-1 字符串读取,导致出现乱码。
文件:`_funcs.py`
函数:`_get_file_name(response, encoding) -> str`
行数:226-276
当从 HTTP 响应头的 `Content-Disposition` 字段获取文件名时,如果文件名中包含空格,这些空格会被错误地移除,导致下载后的文件名与实际文件名不一致。
**示例问题:**
- 响应头:`Content-Disposition: attachment; filename="my file name.txt"`
- 期望文件名:`my file name.txt`
- 实际结果:`myfilename.txt`(空格被移除)
在 `_get_file_name` 函数中,代码在处理 `content-disposition` 响应头时,使用了 `.replace(' ', '')` 方法移除了所有空格,这导致:
1. 文件名中包含的空格被错误移除
2. 仅移除了响应头格式中的多余空格,但同时也移除了文件名内容中的有效空格
文件:`_funcs.py`
函数:`_get_file_name(response, encoding) -> str`
行数:214-239
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…按照指定的编码格式正确解码文件名,导致文件名出现乱码。
示例问题:
测试文件.txtContent-Disposition: attachment; filename="²âÊÔÎļþ.txt"测试文件.txt²âÊÔÎļþ.txt问题原因:
HTTP 响应头默认使用 ISO-8859-1 编码传输。当服务器使用其他编码(如 GBK、CP932、UTF-8)编码文件名时,这些字节序列会被 HTTP 客户端当作 ISO-8859-1 字符串读取,导致出现乱码。
文件:
_funcs.py函数:
_get_file_name(response, encoding) -> str行数:226-276
当从 HTTP 响应头的
Content-Disposition字段获取文件名时,如果文件名中包含空格,这些空格会被错误地移除,导致下载后的文件名与实际文件名不一致。示例问题:
Content-Disposition: attachment; filename="my file name.txt"my file name.txtmyfilename.txt(空格被移除)在
_get_file_name函数中,代码在处理content-disposition响应头时,使用了.replace(' ', '')方法移除了所有空格,这导致:文件:
_funcs.py函数:
_get_file_name(response, encoding) -> str行数:214-239