Skip to main content

MultipartWriter

Struct MultipartWriter 

Source
pub struct MultipartWriter { /* private fields */ }
Expand description

A sans-I/O writer for a multipart body, which appends the body’s bytes to a caller’s buffer, to be sent as the caller chooses.

It refuses whatever would change the body’s structure for a reader: a field name or value that is not valid, which could inject header lines, and a header line or content that contains the boundary after a line break, which would end the part early. That includes the boundary after a bare CR or LF, which some readers also take for a delimiter. A generated boundary makes content containing it practically impossible, but the check also covers boundaries chosen by the caller.

The writer does not apply a reader’s limits: a body with more parts or header fields than a reader allows, such as MultipartLimits’s defaults, is written, and that reader refuses it.

use base_web_multipart::{Boundary, ContentDisposition, MultipartWriter};

let mut writer = MultipartWriter::new(Boundary::new("b")?);
let mut body = Vec::new();
let disposition = ContentDisposition::form_data("note")?.to_string();
writer.start_part([("Content-Disposition", disposition.as_bytes())], &mut body)?;
writer.write_content(b"hello", &mut body)?;
writer.finish(&mut body);
assert_eq!(body, b"--b\r\nContent-Disposition: form-data; name=\"note\"\r\n\r\nhello\r\n--b--\r\n");

Implementations§

Source§

impl MultipartWriter

Source

pub fn new(boundary: Boundary) -> Self

A writer for a body with this boundary.

Source

pub fn boundary(&self) -> &Boundary

The boundary, for the body’s media type, such as from Boundary::form_data_media_type.

Source

pub fn start_part<N, V>( &mut self, fields: impl IntoIterator<Item = (N, V)>, output: &mut Vec<u8>, ) -> Result<(), MultipartWriteError>
where N: AsRef<str>, V: AsRef<[u8]>,

End any current part and begin another, with these header fields, in order.

§Errors

When a field name or value is invalid, or a header line begins with the boundary delimiter. Nothing is written, and the writer is unchanged.

Source

pub fn write_content( &mut self, content: &[u8], output: &mut Vec<u8>, ) -> Result<(), MultipartWriteError>

Append some of the current part’s content.

§Errors

When no part has started, or the content, with the content before it, contains the boundary after a line break. Nothing is written, and the writer is unchanged.

Source

pub fn finish(self, output: &mut Vec<u8>)

End the body with its closing boundary. A body with no parts is the closing boundary alone.

Trait Implementations§

Source§

impl Clone for MultipartWriter

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 MultipartWriter

Source§

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

Formats the value using the given formatter. 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> 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.