Skip to main content

ContentDisposition

Struct ContentDisposition 

Source
pub struct ContentDisposition<'a> { /* private fields */ }
Expand description

A Content-Disposition field, as RFC 2183 defines it for MIME parts and RFC 7578 uses for form data: a disposition type, such as form-data, and parameters, such as name and filename.

Parsing borrows from the field value, copying only a quoted string with escapes. Names and filenames are returned as they were sent: RFC 7578 lets them be UTF-8, and browsers percent-encode the quotes and line breaks in them, which this type does not decode. In a quoted string, a backslash escapes only a quote or another backslash; any other backslash is kept, since browsers send Windows paths unescaped. Extended parameters, such as filename*, are kept as they were sent by parameter; name and filename apply ParameterRules to them, since forms and other MIME parts treat them differently. They are written unquoted.

A value that is not UTF-8, as a form on a page in a legacy character set may send, is invalid, as are an empty parameter, such as a trailing ;, and a value outside quotes that is not a token.

use base_web_multipart::{ContentDisposition, ParameterRules};
use base_message::FieldType;

let disposition = ContentDisposition::parse(b"form-data; name=\"avatar\"; filename=\"caf\xc3\xa9.png\"")?;
assert_eq!(disposition.disposition_type(), "form-data");
assert_eq!(disposition.name(ParameterRules::FormData)?.as_deref(), Some("avatar"));
assert_eq!(disposition.filename(ParameterRules::FormData)?.as_deref(), Some("café.png"));

let written = ContentDisposition::form_data("note")?.with_parameter("filename", "a \"quoted\" name")?;
assert_eq!(written.to_string(), "form-data; name=\"note\"; filename=\"a \\\"quoted\\\" name\"");

Implementations§

Source§

impl ContentDisposition<'static>

Source

pub fn new(disposition_type: &str) -> Result<Self, FieldValueError>

A disposition without parameters, such as attachment. The type is lowercased.

§Errors

When the type is not a token.

Source

pub fn form_data(name: &str) -> Result<Self, FieldValueError>

A form-data disposition for a form field with this name, as RFC 7578 section 4.2 requires of each part.

§Errors

When the name contains a control character, such as CR or LF, which a quoted string cannot carry.

Source§

impl<'a> ContentDisposition<'a>

Source

pub fn with_parameter( self, name: &str, value: &str, ) -> Result<ContentDisposition<'a>, FieldValueError>

The disposition with a parameter added. The name is lowercased.

§Errors

When the name is not a token or is already present, the value contains a control character other than horizontal tab, the name ends with *, marking an RFC 8187 extended value, and the value is not a token, or the disposition already has the most parameters it may have.

Source

pub fn disposition_type(&self) -> &str

The lowercase disposition type, such as form-data, attachment, or inline.

Source

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

The value of a parameter. Names are compared case-insensitively.

Source

pub fn parameters(&self) -> impl ExactSizeIterator<Item = (&str, &str)>

The lowercase parameter names and their values, in order.

Source

pub fn name( &self, rules: ParameterRules, ) -> Result<Option<Cow<'_, str>>, FieldValueError>

The name parameter, a form field’s name, following these rules for extended parameters.

§Errors

When the rules do not allow the disposition’s extended parameters, or an extended name* cannot be decoded.

Source

pub fn filename( &self, rules: ParameterRules, ) -> Result<Option<Cow<'_, str>>, FieldValueError>

The filename parameter, a file’s name as the sender gave it, following these rules for extended parameters. It must not be used as a local path without checking it.

§Errors

When the rules do not allow the disposition’s extended parameters, or an extended filename* cannot be decoded.

Source

pub fn into_owned(self) -> ContentDisposition<'static>

The disposition, owning its text.

Trait Implementations§

Source§

impl<'a> Clone for ContentDisposition<'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 Debug for ContentDisposition<'_>

Shows the disposition type and parameter names, and the lengths of parameter values, since a form field’s name or a filename can identify a person.

Source§

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

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

impl Display for ContentDisposition<'_>

Writes the disposition as a field value, with every parameter value quoted except extended values.

Source§

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

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

impl<'a> Eq for ContentDisposition<'a>

Source§

impl<'a> FieldType<'a> for ContentDisposition<'a>

Source§

type Combination = Singleton

One of the markers in combination: Singleton, List, or Separate.
Source§

fn parse(value: &'a [u8]) -> Result<Self, FieldValueError>

Parse one instance, or one element of a list, with surrounding spaces and tabs already removed. Read more
Source§

const INVALID: InvalidValue = InvalidValue::Error

What an invalid value means, unless a declaration overrides it with invalid = "...". A required declaration always treats an invalid value as an error.
Source§

impl<'a> Hash for ContentDisposition<'a>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> PartialEq for ContentDisposition<'a>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<'a> StructuralPartialEq for ContentDisposition<'a>

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.