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

requests_toolbelt.downloadutils can not parse encoded file name in response header #379

Open
liang2307 opened this issue Aug 26, 2024 · 2 comments

Comments

@liang2307
Copy link

response headers like this:

Content-Disposition:attachment;filename*=charset'lang'value

In my script, temporarily use the custom method to hook function stream._get_filename.

from requests_toolbelt.downloadutils import stream
def _get_filename(content_disposition):

    _QUOTED_STRING_RE = r'"[^"\\]*(?:\\.[^"\\]*)*"'
    _OPTION_HEADER_PIECE_RE = re.compile(
        r';\s*(%s|[^\s;=]+)\s*(?:=\s*(%s|[^;]+))?\s*' % (_QUOTED_STRING_RE,
                                                         _QUOTED_STRING_RE)
    )
    for match in _OPTION_HEADER_PIECE_RE.finditer(content_disposition):
        k, v = match.groups()
        if k == 'filename':
            # ignore any directory paths in the filename
            return os.path.split(v)[1]
        if k == 'filename*':
            return unquote(v).split('\'\'')[1]
    return None
stream._get_filename = _get_filename

Expect to fix this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@liang2307 and others