fix multipart with unicode filename (#420)

This commit is contained in:
Shou Ya
2019-01-09 13:01:52 +08:00
committed by Sean McArthur
parent 5578c77597
commit b129ab0bb4

View File

@@ -397,7 +397,7 @@ impl PercentEncoding {
"Content-Disposition: form-data; {}{}{}", "Content-Disposition: form-data; {}{}{}",
self.format_parameter("name", name), self.format_parameter("name", name),
match field.file_name { match field.file_name {
Some(ref file_name) => format!("; {}", self.format_parameter("filename", file_name)), Some(ref file_name) => format!("; {}", self.format_filename(file_name)),
None => String::new(), None => String::new(),
}, },
match field.mime { match field.mime {
@@ -414,6 +414,16 @@ impl PercentEncoding {
}) })
} }
// According to RFC7578 Section 4.2, `filename*=` syntax is invalid.
// See https://github.com/seanmonstar/reqwest/issues/419.
fn format_filename(&self, filename: &str) -> String {
let legal_filename = filename.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\r", "\\\r")
.replace("\n", "\\\n");
format!("filename=\"{}\"", legal_filename)
}
fn format_parameter(&self, name: &str, value: &str) -> String { fn format_parameter(&self, name: &str, value: &str) -> String {
let legal_value = match *self { let legal_value = match *self {
PercentEncoding::PathSegment => { PercentEncoding::PathSegment => {