Construct a Content-Disposition HTTP header value from the given filename as specified by RFC 6266.
(as_attachment, filename)
| 376 | |
| 377 | |
| 378 | def 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 |