From cbce427c17b66faec7ced0639346dc6905a498f9 Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Wed, 30 Nov 2022 15:09:49 -0500 Subject: Fixed #34194 -- Added django.utils.http.content_disposition_header(). --- django/utils/http.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'django/utils') diff --git a/django/utils/http.py b/django/utils/http.py index db4dee2f27..3e7acb5835 100644 --- a/django/utils/http.py +++ b/django/utils/http.py @@ -10,6 +10,7 @@ from urllib.parse import ( _coerce_args, _splitnetloc, _splitparams, + quote, scheme_chars, unquote, ) @@ -425,3 +426,24 @@ def parse_header_parameters(line): value = unquote(value, encoding=encoding) pdict[name] = value return key, pdict + + +def content_disposition_header(as_attachment, filename): + """ + Construct a Content-Disposition HTTP header value from the given filename + as specified by RFC 6266. + """ + if filename: + disposition = "attachment" if as_attachment else "inline" + try: + filename.encode("ascii") + file_expr = 'filename="{}"'.format( + filename.replace("\\", "\\\\").replace('"', r"\"") + ) + except UnicodeEncodeError: + file_expr = "filename*=utf-8''{}".format(quote(filename)) + return f"{disposition}; {file_expr}" + elif as_attachment: + return "attachment" + else: + return None -- cgit v1.3