def _rename_with_content_disposition(self, response: HTTPResponse): '''Rename using the Content-Disposition header.''' if not self._filename: return if response.request.url_info.scheme not in ('http', 'https'): return header_value = response.fields.get('Content-Disposition') if not header_value: return filename = parse_content_disposition(header_value) if filename: dir_path = os.path.dirname(self._filename) new_filename = self._path_namer.safe_filename(filename) self._filename = os.path.join(dir_path, new_filename)
def _rename_with_content_disposition(self, response): '''Rename using the Content-Disposition header.''' if not self._filename: return if response.request.url_info.scheme not in ('http', 'https'): return header_value = response.fields.get('Content-Disposition') if not header_value: return filename = parse_content_disposition(header_value) if filename: parts = list(self._filename.split(os.sep)) if parts: del parts[-1] new_filename = self._path_namer.safe_filename(filename) parts.append(new_filename) self._filename = os.path.join(*parts)
def test_parse_content_disposition(self): self.assertEqual("hello.txt", parse_content_disposition("attachment; filename=hello.txt")) self.assertEqual("hello.txt", parse_content_disposition("attachment; filename=hello.txt; filename*=blahblah")) self.assertEqual("hello.txt", parse_content_disposition("attachment; filename=hello.txt ;filename*=blahblah")) self.assertEqual("hello.txt", parse_content_disposition('attachment; filename="hello.txt"')) self.assertEqual("hello.txt", parse_content_disposition('attachment; filename="hello.txt" ;')) self.assertEqual("hello world", parse_content_disposition('attachment; filename="hello world"')) self.assertEqual("hello world", parse_content_disposition('attachment; filename="hello world"')) self.assertEqual("hello world", parse_content_disposition("attachment; filename='hello world'")) self.assertEqual('hello"world', parse_content_disposition('attachment; filename="hello\\"world"')) self.assertEqual("'hello\"world'", parse_content_disposition('attachment; filename="\'hello\\"world\'"')) self.assertEqual("'hello\"world'", parse_content_disposition('attachment; filename="\'hello\\"world\'";')) self.assertFalse(parse_content_disposition("attachment; filename=")) self.assertFalse(parse_content_disposition('attachment; filename=""')) self.assertFalse(parse_content_disposition('attachment; filename=";')) self.assertFalse(parse_content_disposition("attachment; filename='aaa")) self.assertFalse(parse_content_disposition('attachment; filename="aaa'))
def test_parse_content_disposition(self): self.assertEqual( 'hello.txt', parse_content_disposition('attachment; filename=hello.txt')) self.assertEqual( 'hello.txt', parse_content_disposition( 'attachment; filename=hello.txt; filename*=blahblah')) self.assertEqual( 'hello.txt', parse_content_disposition( 'attachment; filename=hello.txt ;filename*=blahblah')) self.assertEqual( 'hello.txt', parse_content_disposition('attachment; filename="hello.txt"')) self.assertEqual( 'hello.txt', parse_content_disposition('attachment; filename="hello.txt" ;')) self.assertEqual( 'hello world', parse_content_disposition('attachment; filename="hello world"')) self.assertEqual( 'hello world', parse_content_disposition('attachment; filename="hello world"')) self.assertEqual( 'hello world', parse_content_disposition("attachment; filename='hello world'")) self.assertEqual( 'hello"world', parse_content_disposition('attachment; filename="hello\\"world"')) self.assertEqual( '\'hello"world\'', parse_content_disposition( 'attachment; filename="\'hello\\"world\'"')) self.assertEqual( '\'hello"world\'', parse_content_disposition( 'attachment; filename="\'hello\\"world\'";')) self.assertFalse(parse_content_disposition('attachment; filename=')) self.assertFalse(parse_content_disposition('attachment; filename=""')) self.assertFalse(parse_content_disposition('attachment; filename=";')) self.assertFalse( parse_content_disposition('attachment; filename=\'aaa')) self.assertFalse( parse_content_disposition('attachment; filename="aaa'))