Content Disposition Filename Parse
Parse Content-Disposition headers for safe filename extraction with RFC 5987 support.
Code
from urllib.parse import unquote
import re
def parse_filename(header):
if not header:
return None
m = re.search(r"filename\*=([^;]+)", header)
if m:
parts = m.group(1).split("'")
if len(parts) == 3:
return unquote(parts[2])
return unquote(parts[-1])
m = re.search(r'filename="?([^";]+)"?', header)
if m:
return m.group(1)
return NoneReport a problem with this exampleOpens a pre-filled issue on GitHub.