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
impl MultipartWriter
Sourcepub fn boundary(&self) -> &Boundary
pub fn boundary(&self) -> &Boundary
The boundary, for the body’s media type, such as from Boundary::form_data_media_type.
Sourcepub fn start_part<N, V>(
&mut self,
fields: impl IntoIterator<Item = (N, V)>,
output: &mut Vec<u8>,
) -> Result<(), MultipartWriteError>
pub fn start_part<N, V>( &mut self, fields: impl IntoIterator<Item = (N, V)>, output: &mut Vec<u8>, ) -> Result<(), MultipartWriteError>
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.
Sourcepub fn write_content(
&mut self,
content: &[u8],
output: &mut Vec<u8>,
) -> Result<(), MultipartWriteError>
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.