pub struct FileName<'a> { /* private fields */ }Expand description
The client’s file name, with a view for each way a caller might want it.
form-net never uses a client file name as a path or as part of one.
Debug prints the length only: file names are user data.
§Examples
let body = b"--X\r\nContent-Disposition: form-data; name=\"doc\"; filename=\"C:\\Users\\me\\a b.pdf\"\r\n\r\nx\r\n--X--";
let mp = form_net::parse_multipart(b"multipart/form-data; boundary=X", body)?;
let name = mp.file("doc").unwrap().filename().unwrap();
assert_eq!(name.raw(), b"C:\\Users\\me\\a b.pdf");
assert_eq!(name.basename(), b"a b.pdf");Implementations§
Source§impl<'a> FileName<'a>
impl<'a> FileName<'a>
Sourcepub const fn raw(&self) -> &'a [u8] ⓘ
pub const fn raw(&self) -> &'a [u8] ⓘ
The bytes as sent between the quotes of filename, untouched; or the
decoded bytes of an accepted filename*.
§Examples
let name = mp.file("f").unwrap().filename().unwrap();
assert_eq!(name.raw(), b"a%22b.txt");Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Whether the name is empty: a file input left empty.
§Examples
assert!(mp.file("f").unwrap().filename().unwrap().is_empty());Sourcepub const fn source(&self) -> FileNameSource
pub const fn source(&self) -> FileNameSource
Plain for filename, Extended for filename*.
§Examples
use form_net::FileNameSource;
assert_eq!(mp.file("f").unwrap().filename().unwrap().source(), FileNameSource::Plain);Sourcepub fn basename(&self) -> &'a [u8] ⓘ
pub fn basename(&self) -> &'a [u8] ⓘ
The bytes after the last / or \ (some non-browser clients send
full client paths).
§Examples
assert_eq!(mp.file("f").unwrap().filename().unwrap().basename(), b"c.txt");Sourcepub fn html_unescape_into<'b>(
&self,
out: &'b mut [u8],
) -> Result<&'b str, FormError>
pub fn html_unescape_into<'b>( &self, out: &'b mut [u8], ) -> Result<&'b str, FormError>
Writes the name into out with the HTML escapes %0A, %0D, %22
decoded to LF, CR, ", and nothing else, and returns it as text.
Browsers do not escape % itself, so a file the user named a%22b
reads as a"b; that ambiguity is inherent in the HTML encoding.
§Errors
BufferTooSmall when out is shorter than raw;
Encoding { InvalidUtf8 }.
§Examples
let mut out = [0u8; 32];
let name = mp.file("f").unwrap().filename().unwrap();
assert_eq!(name.html_unescape_into(&mut out)?, "a\"b.txt");Sourcepub fn html_unescaped(&self) -> Result<Cow<'a, str>, FormError>
Available on crate feature alloc only.
pub fn html_unescaped(&self) -> Result<Cow<'a, str>, FormError>
alloc only.As html_unescape_into, borrowed when the
name has nothing to decode.
§Errors
Encoding { InvalidUtf8 }.
§Examples
assert_eq!(mp.file("f").unwrap().filename().unwrap().html_unescaped()?, "a\nb");