MCPcopy
hub / github.com/django/django / content_disposition_header

Function content_disposition_header

django/utils/http.py:378–405  ·  view source on GitHub ↗

Construct a Content-Disposition HTTP header value from the given filename as specified by RFC 6266.

(as_attachment, filename)

Source from the content-addressed store, hash-verified

376
377
378def content_disposition_header(as_attachment, filename):
379 """
380 Construct a Content-Disposition HTTP header value from the given filename
381 as specified by RFC 6266.
382 """
383 if filename:
384 disposition = "attachment" if as_attachment else "inline"
385 try:
386 filename.encode("ascii")
387 is_ascii = True
388 except UnicodeEncodeError:
389 is_ascii = False
390 # Quoted strings can contain horizontal tabs, space characters, and
391 # characters from 0x21 to 0x7e, except 0x22 (`"`) and 0x5C (`\`) which
392 # can still be expressed but must be escaped with their own `\`.
393 # https://datatracker.ietf.org/doc/html/rfc9110#name-quoted-strings
394 quotable_characters = r"^[\t \x21-\x7e]*$"
395 if is_ascii and re.match(quotable_characters, filename):
396 file_expr = 'filename="{}"'.format(
397 filename.replace("\\", "\\\\").replace('"', r"\"")
398 )
399 else:
400 file_expr = "filename*=utf-8''{}".format(quote(filename))
401 return f"{disposition}; {file_expr}"
402 elif as_attachment:
403 return "attachment"
404 else:
405 return None

Callers 2

set_headersMethod · 0.90
test_basicMethod · 0.90

Calls 4

quoteFunction · 0.85
encodeMethod · 0.45
matchMethod · 0.45
formatMethod · 0.45

Tested by 1

test_basicMethod · 0.72