pub enum Filename<'a> {
Plain(Word<'a>),
Ext(ExtValue<'a>),
}Expand description
A file name from Content-Disposition or a Link title.
§Examples
use parse_net::{parse_content_disposition, Filename};
let cd = parse_content_disposition(b"attachment; filename=\"a b.txt\"")?;
assert!(matches!(cd.filename(), Some(Filename::Plain(w)) if w.eq_bytes(b"a b.txt")));
let cd = parse_content_disposition(b"attachment; filename*=UTF-8''a%20b.txt")?;
assert!(matches!(cd.filename(), Some(Filename::Ext(_))));Variants§
Plain(Word<'a>)
The filename (or title) parameter.
Ext(ExtValue<'a>)
The filename* (or title*) parameter.
Implementations§
Source§impl Filename<'_>
impl Filename<'_>
Sourcepub fn max_decoded_len(&self) -> usize
pub fn max_decoded_len(&self) -> usize
An upper bound on the length the decode functions write.
§Examples
let cd = parse_net::parse_content_disposition(b"attachment; filename*=ISO-8859-1''caf%E9")?;
let name = cd.filename().expect("present");
assert_eq!(name.max_decoded_len(), 8);
let mut buf = [0u8; 8];
assert_eq!(name.decode_into(&mut buf)?, "caf\u{e9}");Sourcepub fn decode_into<'b>(&self, out: &'b mut [u8]) -> Result<&'b str, ParseError>
pub fn decode_into<'b>(&self, out: &'b mut [u8]) -> Result<&'b str, ParseError>
Decodes into out: the unescaped filename as UTF-8, or the decoded
filename*. Separators (/, \) pass through; see
has_path_separator.
§Errors
ErrorKind::InvalidUtf8, ErrorKind::ControlInDecoded,
ErrorKind::BufferTooSmall. Nothing is written on error.
§Examples
use parse_net::{parse_content_disposition, ErrorKind};
let cd = parse_content_disposition(b"attachment; filename=\"q\\\"uote.txt\"")?;
let name = cd.filename().expect("present");
let mut buf = [0u8; 16];
assert_eq!(name.decode_into(&mut buf)?, "q\"uote.txt");
assert_eq!(name.decode_into(&mut [0u8; 2]).unwrap_err().kind(), ErrorKind::BufferTooSmall);Sourcepub fn decode_percent_into<'b>(
&self,
out: &'b mut [u8],
) -> Result<&'b str, ParseError>
pub fn decode_percent_into<'b>( &self, out: &'b mut [u8], ) -> Result<&'b str, ParseError>
As decode_into, and %XX in a plain
filename is decoded too (RFC 7578 section 2 percent-encoded file
names).
§Errors
As decode_into, and
ErrorKind::InvalidPercentEncoding.
§Examples
let cd = parse_net::parse_content_disposition(b"form-data; name=f; filename=\"caf%C3%A9.txt\"")?;
let mut buf = [0u8; 32];
assert_eq!(cd.filename().expect("present").decode_percent_into(&mut buf)?, "caf\u{e9}.txt");Sourcepub fn has_path_separator(&self) -> bool
pub fn has_path_separator(&self) -> bool
Whether the decoded name contains / or \ (in a plain name, also
after percent-decoding). The file-name policy belongs to the caller
(path-lib, customs-sec).
§Examples
use parse_net::parse_content_disposition;
let cd = parse_content_disposition(b"attachment; filename=\"..%2Fetc%2Fpasswd\"")?;
assert!(cd.filename().expect("present").has_path_separator());
let ok = parse_content_disposition(b"attachment; filename=notes.txt")?;
assert!(!ok.filename().expect("present").has_path_separator());Sourcepub fn has_bidi_control(&self) -> bool
pub fn has_bidi_control(&self) -> bool
Whether the decoded name contains a bidirectional control character
(U+202A to U+202E, U+2066 to U+2069), used to disguise an extension
(exe.txt shown for txt.exe). Reported, not refused.
§Examples
let cd = parse_net::parse_content_disposition(b"attachment; filename*=UTF-8''invoice%E2%80%AEfdp.exe")?;
assert!(cd.filename().expect("present").has_bidi_control());