Coverage for src/lilbee/server/content_disposition.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-09-28 17:20 +0000

1"""The ``Content-Disposition`` header that names a downloaded file.""" 

2 

3from __future__ import annotations 

4 

5import re 

6from urllib.parse import quote 

7 

8CONTENT_DISPOSITION = "Content-Disposition" 

9# Printable ASCII except the quote and backslash a quoted filename cannot hold bare. 

10_FILENAME_UNSAFE_RE = re.compile(r"[^ !#-\[\]-~]") 

11_FILENAME_PLACEHOLDER = "_" 

12 

13 

14def attachment_disposition(filename: str) -> str: 

15 """An RFC 6266 attachment header: an ASCII *filename* fallback plus the exact UTF-8 name.""" 

16 fallback = _FILENAME_UNSAFE_RE.sub(_FILENAME_PLACEHOLDER, filename) 

17 return f"attachment; filename=\"{fallback}\"; filename*=UTF-8''{quote(filename, safe='')}"