Skip to main content

FileName

Struct FileName 

Source
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>

Source

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");
Source

pub fn as_str(&self) -> Result<&'a str, FormError>

The raw bytes as UTF-8.

§Errors

Encoding { InvalidUtf8 }.

§Examples
assert_eq!(mp.file("f").unwrap().filename().unwrap().as_str()?, "café.txt");
Source

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());
Source

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);
Source

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");
Source

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");
Source

pub fn html_unescaped(&self) -> Result<Cow<'a, str>, FormError>

Available on crate feature 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");

Trait Implementations§

Source§

impl<'a> Clone for FileName<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for FileName<'a>

Source§

impl Debug for FileName<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for FileName<'a>

§

impl<'a> RefUnwindSafe for FileName<'a>

§

impl<'a> Send for FileName<'a>

§

impl<'a> Sync for FileName<'a>

§

impl<'a> Unpin for FileName<'a>

§

impl<'a> UnsafeUnpin for FileName<'a>

§

impl<'a> UnwindSafe for FileName<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.