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

Update encoding for Content-Disposition header #363

Merged
merged 1 commit into from
Oct 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::path::Path;

use mime_guess::{self, Mime};
use url::percent_encoding;
use url::percent_encoding::EncodeSet;
use uuid::Uuid;
use http::HeaderMap;

Expand Down Expand Up @@ -311,6 +312,32 @@ impl Read for Reader {
}
}

#[derive(Debug, Clone)]
struct AttrCharEncodeSet;

impl EncodeSet for AttrCharEncodeSet {
fn contains(&self, ch: u8) -> bool {
match ch as char {
'!' => false,
'#' => false,
'$' => false,
'&' => false,
'+' => false,
'-' => false,
'.' => false,
'^' => false,
'_' => false,
'`' => false,
'|' => false,
'~' => false,
_ => {
let is_alpha_numeric = ch >= 0x41 && ch <= 0x5a || ch >= 0x61 && ch <= 0x7a || ch >= 0x30 && ch <= 0x39;
!is_alpha_numeric
}
}
}

}

fn header(name: &str, field: &Part) -> Vec<u8> {
let s = format!(
Expand All @@ -325,7 +352,6 @@ fn header(name: &str, field: &Part) -> Vec<u8> {
None => "".to_string(),
},
);

field.headers.iter().fold(s.into_bytes(), |mut header, (k,v)| {
header.extend_from_slice(b"\r\n");
header.extend_from_slice(k.as_str().as_bytes());
Expand All @@ -337,7 +363,7 @@ fn header(name: &str, field: &Part) -> Vec<u8> {

fn format_parameter(name: &str, value: &str) -> String {
let legal_value =
percent_encoding::utf8_percent_encode(value, percent_encoding::PATH_SEGMENT_ENCODE_SET)
percent_encoding::utf8_percent_encode(value, AttrCharEncodeSet)
.to_string();
if value.len() == legal_value.len() {
// nothing has been percent encoded
Expand Down Expand Up @@ -470,7 +496,7 @@ mod tests {
fn header_percent_encoding() {
let name = "start%'\"\r\nßend";
let field = Part::text("");
let expected = "Content-Disposition: form-data; name*=utf-8''start%25\'%22%0D%0A%C3%9Fend";
let expected = "Content-Disposition: form-data; name*=utf-8''start%25%27%22%0D%0A%C3%9Fend";

assert_eq!(header(name, &field), expected.as_bytes());
}
Expand Down