diff options
author | Kartikaya Gupta <kgupta@mozilla.com> | 2016-06-18 11:04:00 +0100 |
---|---|---|
committer | Kartikaya Gupta <kgupta@mozilla.com> | 2016-06-18 11:04:00 +0100 |
commit | 5595a17a39949253118b8d9f11ab68091892a0c7 (patch) | |
tree | d46ab3b4dd40a217a560e9944c4a18294fe9090d | |
parent | ee4e959e10316ccd59400f69327760e9e977d339 (diff) |
Strip out whitespace before base64-decoding the mail body
-rw-r--r-- | src/lib.rs | 14 |
1 files changed, 13 insertions, 1 deletions
@@ -405,7 +405,15 @@ impl<'a> ParsedMail<'a> { let transfer_coding = try!(self.headers.get_first_value("Content-Transfer-Encoding")) .map(|s| s.to_lowercase()); let decoded = match transfer_coding.unwrap_or(String::new()).as_ref() { - "base64" => try!(base64::u8de(self.body)), + "base64" => { + let cleaned = self.body.iter().filter_map( + |&c| match c { + b' ' | b'\t' | b'\r' | b'\n' => None, + v => Some(v), + } + ).collect::<Vec<u8>>(); + try!(base64::u8de(&cleaned)) + } "quoted-printable" => try!(quoted_printable::decode(self.body, quoted_printable::ParseMode::Robust)), _ => Vec::<u8>::from(self.body), }; @@ -653,6 +661,7 @@ mod tests { assert_eq!(mail.ctype.charset, "us-ascii"); assert_eq!(mail.ctype.boundary, None); assert_eq!(mail.body, b"Some body stuffs"); + assert_eq!(mail.get_body().unwrap(), "Some body stuffs"); assert_eq!(mail.subparts.len(), 0); let mail = parse_mail(b"Content-Type: MULTIpart/alternative; bounDAry=myboundary\r\n\r\n \ @@ -675,5 +684,8 @@ mod tests { assert_eq!(mail.subparts[1].ctype.mimetype, "text/html"); assert_eq!(mail.subparts[1].ctype.charset, "utf-8"); assert_eq!(mail.subparts[1].ctype.boundary, None); + + let mail = parse_mail(b"Content-Transfer-Encoding: base64\r\n\r\naGVsbG 8gd\r\n29ybGQ=").unwrap(); + assert_eq!(mail.get_body().unwrap(), "hello world"); } } |