Skip to content

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

@liang2307

Description

@liang2307

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions