summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorAlex Vandiver <alex@chmrr.net>2022-11-30 15:09:49 -0500
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-12-05 13:08:00 +0100
commitcbce427c17b66faec7ced0639346dc6905a498f9 (patch)
tree894b0990050dcf94c36010838717dd96351fbcc5 /django
parent3d3e955efaaeb4cc968e522592c5c1e47bdc72c4 (diff)
Fixed #34194 -- Added django.utils.http.content_disposition_header().
Diffstat (limited to 'django')
-rw-r--r--django/http/response.py22
-rw-r--r--django/utils/http.py22
2 files changed, 28 insertions, 16 deletions
diff --git a/django/http/response.py b/django/http/response.py
index bb94e81263..3c281f3dd0 100644
--- a/django/http/response.py
+++ b/django/http/response.py
@@ -8,7 +8,7 @@ import sys
import time
from email.header import Header
from http.client import responses
-from urllib.parse import quote, urlparse
+from urllib.parse import urlparse
from django.conf import settings
from django.core import signals, signing
@@ -18,7 +18,7 @@ from django.http.cookie import SimpleCookie
from django.utils import timezone
from django.utils.datastructures import CaseInsensitiveMapping
from django.utils.encoding import iri_to_uri
-from django.utils.http import http_date
+from django.utils.http import content_disposition_header, http_date
from django.utils.regex_helper import _lazy_re_compile
_charset_from_content_type_re = _lazy_re_compile(
@@ -569,20 +569,10 @@ class FileResponse(StreamingHttpResponse):
else:
self.headers["Content-Type"] = "application/octet-stream"
- if filename:
- disposition = "attachment" if self.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))
- self.headers["Content-Disposition"] = "{}; {}".format(
- disposition, file_expr
- )
- elif self.as_attachment:
- self.headers["Content-Disposition"] = "attachment"
+ if content_disposition := content_disposition_header(
+ self.as_attachment, filename
+ ):
+ self.headers["Content-Disposition"] = content_disposition
class HttpResponseRedirectBase(HttpResponse):
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