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>
impl ContentDisposition<'static>
Source§impl<'a> ContentDisposition<'a>
impl<'a> ContentDisposition<'a>
Sourcepub fn with_parameter(
self,
name: &str,
value: &str,
) -> Result<ContentDisposition<'a>, FieldValueError>
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.
Sourcepub fn disposition_type(&self) -> &str
pub fn disposition_type(&self) -> &str
The lowercase disposition type, such as form-data, attachment, or inline.
Sourcepub fn parameter(&self, name: &str) -> Option<&str>
pub fn parameter(&self, name: &str) -> Option<&str>
The value of a parameter. Names are compared case-insensitively.
Sourcepub fn parameters(&self) -> impl ExactSizeIterator<Item = (&str, &str)>
pub fn parameters(&self) -> impl ExactSizeIterator<Item = (&str, &str)>
The lowercase parameter names and their values, in order.
Sourcepub fn name(
&self,
rules: ParameterRules,
) -> Result<Option<Cow<'_, str>>, FieldValueError>
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.
Sourcepub fn filename(
&self,
rules: ParameterRules,
) -> Result<Option<Cow<'_, str>>, FieldValueError>
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.
Sourcepub fn into_owned(self) -> ContentDisposition<'static>
pub fn into_owned(self) -> ContentDisposition<'static>
The disposition, owning its text.
Trait Implementations§
Source§impl<'a> Clone for ContentDisposition<'a>
impl<'a> Clone for ContentDisposition<'a>
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.
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§impl Display for ContentDisposition<'_>
Writes the disposition as a field value, with every parameter value quoted except extended values.
impl Display for ContentDisposition<'_>
Writes the disposition as a field value, with every parameter value quoted except extended values.
impl<'a> Eq for ContentDisposition<'a>
Source§impl<'a> FieldType<'a> for ContentDisposition<'a>
impl<'a> FieldType<'a> for ContentDisposition<'a>
Source§type Combination = Singleton
type Combination = Singleton
Source§fn parse(value: &'a [u8]) -> Result<Self, FieldValueError>
fn parse(value: &'a [u8]) -> Result<Self, FieldValueError>
Source§const INVALID: InvalidValue = InvalidValue::Error
const INVALID: InvalidValue = InvalidValue::Error
invalid = "...". A required declaration always treats an invalid value as an error.