dispo is a lightweight, high-performance Go package for building safe HTTP Content-Disposition headers.
The package correctly handles:
- ASCII and non-ASCII filenames
- Spaces and control characters
- ASCII
filenamefallback with RFC-oriented hardening for/,\, and%HH - Quoted-string escaping for
"infilename - UTF-8
filename*encoding according to RFC 5987 / RFC 6266
Behavior notes:
- leading and trailing whitespace is trimmed from filenames
- internal ASCII and Unicode whitespace is normalized to ASCII spaces
- ASCII
filenamepreserves most printable ASCII characters as-is, except for RFC-sensitive fallback rewrites of/,\, and%HHsequences - non-ASCII runes become
_infilename, whilefilename*carries the exact sanitized filename in UTF-8, with/and\rewritten to_ - invalid UTF-8 byte sequences become
_infilenameand U+FFFD infilename* filename*is also emitted when the ASCII fallback had to be rewritten- disposition types are trimmed;
inlineandattachmentare normalized to lowercase, and other valid HTTP tokens are passed through in lowercase
-
ContentDisposition(dispositionType, name string) stringGenerates a Content-Disposition header with a normalized disposition type and filename. -
Attachment(name string) stringShortcut forattachment. -
Inline(name string) stringShortcut forinline.
Run a local demo page with several Content-Disposition variants. It chooses
a free local port and opens the browser automatically:
go run ./cmd/dispo-labUse -addr to choose a fixed port, or -open=false to skip opening the
browser:
go run ./cmd/dispo-lab -addr 127.0.0.1:8090
go run ./cmd/dispo-lab -open=false