Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API for Headers on Parts of Multipart Form Data #331

Merged
merged 1 commit into from
Sep 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 44 additions & 2 deletions src/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::path::Path;
use mime_guess::{self, Mime};
use url::percent_encoding;
use uuid::Uuid;
use http::HeaderMap;

use {Body};

Expand Down Expand Up @@ -131,6 +132,7 @@ pub struct Part {
value: Body,
mime: Option<Mime>,
file_name: Option<Cow<'static, str>>,
headers: HeaderMap<String>,
}

impl Part {
Expand Down Expand Up @@ -185,6 +187,7 @@ impl Part {
value: value,
mime: None,
file_name: None,
headers: HeaderMap::default()
}
}

Expand All @@ -199,6 +202,16 @@ impl Part {
self.file_name = Some(filename.into());
self
}

/// Returns a reference to the map with additional header fields
pub fn headers(&self) -> &HeaderMap<String> {
&self.headers
}

/// Returns a reference to the map with additional header fields
pub fn headers_mut(&mut self) -> &mut HeaderMap<String> {
&mut self.headers
}
}

impl fmt::Debug for Part {
Expand All @@ -207,6 +220,7 @@ impl fmt::Debug for Part {
.field("value", &self.value)
.field("mime", &self.mime)
.field("file_name", &self.file_name)
.field("headers", &self.headers)
.finish()
}
}
Expand Down Expand Up @@ -289,7 +303,7 @@ impl Read for Reader {

fn header(name: &str, field: &Part) -> String {
format!(
"Content-Disposition: form-data; {}{}{}",
"Content-Disposition: form-data; {}{}{}{}",
format_parameter("name", name),
match field.file_name {
Some(ref file_name) => format!("; {}", format_parameter("filename", file_name)),
Expand All @@ -298,7 +312,11 @@ fn header(name: &str, field: &Part) -> String {
match field.mime {
Some(ref mime) => format!("\r\nContent-Type: {}", mime),
None => "".to_string(),
}
},
field.headers.iter().fold(
"".to_string(),
|header, (k,v)| header + "\r\n" + k.as_str() + ": " + v
)
)
}

Expand Down Expand Up @@ -409,6 +427,30 @@ mod tests {
assert_eq!(length.unwrap(), expected.len() as u64);
}

#[test]
fn read_to_end_with_header() {
let mut output = Vec::new();
let mut part = Part::text("value2").mime(::mime::IMAGE_BMP);
part.headers_mut().insert("Hdr3", "/a/b/c".to_string());
let mut form = Form::new().part("key2", part);
form.boundary = "boundary".to_string();
let expected = "--boundary\r\n\
Content-Disposition: form-data; name=\"key2\"\r\n\
Content-Type: image/bmp\r\n\
hdr3: /a/b/c\r\n\
\r\n\
value2\r\n\
--boundary--\r\n";
form.reader().read_to_end(&mut output).unwrap();
// These prints are for debug purposes in case the test fails
println!(
"START REAL\n{}\nEND REAL",
::std::str::from_utf8(&output).unwrap()
);
println!("START EXPECTED\n{}\nEND EXPECTED", expected);
assert_eq!(::std::str::from_utf8(&output).unwrap(), expected);
}

#[test]
fn header_percent_encoding() {
let name = "start%'\"\r\nßend";
Expand Down