flask: send_file fails when filename contains unicode symbols

Hi.

I’ve detected an issue with supporting unicode filenames in send_file. If send_file attempts to respond with utf-8 in http headers, the answer is empty, the log contains something like “http-headers should containbe latin-1”. I know that browser support IS A MESS, but it seems, that sending two filenames (filename= and filename*=) separated by semicolon should work.

I’d like this to be handled by flask or werkzeug. Will you accept such pull request?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 2
  • Comments: 38 (22 by maintainers)

Commits related to this issue

Most upvoted comments

Similar discussion has already happened regarding this observation. See the issue from requests to read more about the details, but the short of it is that HTTP headers are supposed to only accept latin-1 characters. Changing this behavior in flask or werkzeug would only cause issues and break running code by allowing requests to be sent in an unsupported encoding to webservers which expect to get latin-1.

Someone else can chime in if I am mistaken, but I think this can be closed.

Similar discussion: https://github.com/kennethreitz/requests/issues/1926 https://github.com/jakubroztocil/httpie/issues/212

BTW, I’ve solved the problem by sending filename*=utf-8’blah’ header field. Works everywhere except for the IE <= 8.

Here is the snippet:

response = flask.make_response(flask.send_file(pdf_full_path))
response.headers["Content-Disposition"] = \
    "attachment; " \
    "filenane={ascii_filename};" \
    "filename*=UTF-8''{utf_filename}".format(
    ascii_filename="book.pdf",
    utf_filename=urlparse.quote(os.path.basename(pdf_full_path))
)

Looks like a more correct way of fixing this.