Skip to main content

Upload

Struct Upload 

Source
pub struct Upload { /* private fields */ }
Available on crate feature fs only.
Expand description

The result of a completed multipart parse with storage: text fields, owned and wiped on drop, and files.

§Examples

let body = b"--X\r\nContent-Disposition: form-data; name=title\r\n\r\nHoliday\r\n--X\r\nContent-Disposition: form-data; name=photo; filename=a.jpg\r\n\r\nJPEG\r\n--X--";
let mut upload = form_net::read_multipart(b"multipart/form-data; boundary=X", &body[..])?;
assert_eq!(upload.field("title"), Some("Holiday"));
let photo = upload.take_file("photo").unwrap();
assert_eq!(photo.bytes(), Some(&b"JPEG"[..]));
assert_eq!(upload.file_count(), 0);

Implementations§

Source§

impl Upload

Source

pub fn field(&self, name: &str) -> Option<&str>

The first text field named name.

Source

pub fn field_one(&self, name: &str) -> Result<Option<&str>, FormError>

The text field named name, refusing a name given twice.

§Errors

Extract { Duplicate }.

Source

pub fn fields<'u>(&'u self, name: &'u str) -> impl Iterator<Item = &'u str> + 'u

Every text field named name, in body order.

Source

pub fn iter(&self) -> impl Iterator<Item = (&str, &str)>

Every text field (name, value), in body order.

Source

pub fn file(&self, name: &str) -> Option<&StoredFile>

The first file named name.

Source

pub fn files<'u>( &'u self, name: &'u str, ) -> impl Iterator<Item = &'u StoredFile> + 'u

Every file named name, in body order.

Source

pub fn take_file(&mut self, name: &str) -> Option<StoredFile>

Removes and returns the first file named name.

Source

pub fn into_files(self) -> Vec<StoredFile>

Every file, in body order.

Source

pub fn captured_token(&self) -> Option<&str>

The anti-forgery token field’s value, when a token field was set.

Source

pub fn text_len(&self) -> u64

The bytes of all text values together.

Source

pub fn file_count(&self) -> usize

The number of files.

Source

pub fn parse_with<R: Read, I: Inspector>( content_type: &[u8], reader: R, limits: &Limits, policy: &Policy, store: &Store, inspector: &mut I, ) -> Result<Upload, FormError>

Reads a whole multipart body from reader in 64 KiB chunks, with explicit limits, policy, store, and inspector.

§Errors

Every multipart error; Refused; Io { kind }; Store { .. }; Limit { Disk }.

§Examples
use form_net::{Limits, NoInspector, Policy, Store, TokenField, Upload};

let store = Store::temp()?;
let policy = Policy::DEFAULT.with_token_field(TokenField::new("csrf_token"));
let body = b"--X\r\nContent-Disposition: form-data; name=csrf_token\r\n\r\ntok\r\n--X--";
let upload = Upload::parse_with(b"multipart/form-data; boundary=X", &body[..], &Limits::DEFAULT, &policy, &store, &mut NoInspector)?;
assert_eq!(upload.captured_token(), Some("tok"));

Trait Implementations§

Source§

impl Debug for Upload

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Upload

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for Upload

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

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> 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, 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.