def format_content_disposition(value, disposition_type='attachment'): """Format a Content-Disposition header given a filename.""" # NOTE(vytas): RFC 6266, Appendix D. # Include a "filename" parameter when US-ASCII ([US-ASCII]) is # sufficiently expressive. if isascii(value): return '%s; filename="%s"' % (disposition_type, value) # NOTE(vytas): RFC 6266, Appendix D. # * Include a "filename*" parameter where the desired filename cannot be # expressed faithfully using the "filename" form. Note that legacy # user agents will not process this, and will fall back to using the # "filename" parameter's content. # * When a "filename*" parameter is sent, to also generate a "filename" # parameter as a fallback for user agents that do not support the # "filename*" form, if possible. # * When a "filename" parameter is included as a fallback (as per above), # "filename" should occur first, due to parsing problems in some # existing implementations. return "%s; filename=%s; filename*=UTF-8''%s" % ( disposition_type, secure_filename(value), uri.encode_value(value), )
def test_secure_filename_empty_value(self): with pytest.raises(ValueError): misc.secure_filename('')
def test_secure_filename(self, filename, expected): assert misc.secure_filename(filename) == expected
def secure_filename(self): try: return misc.secure_filename(self.filename) except ValueError as ex: raise MultipartParseError(description=str(ex)) from ex