diff options
author | Kartikaya Gupta <kgupta@mozilla.com> | 2016-06-08 17:00:57 -0400 |
---|---|---|
committer | Kartikaya Gupta <kgupta@mozilla.com> | 2016-06-08 17:00:57 -0400 |
commit | ef0987598ba0a983d1309d8a6c00079ad2225e3b (patch) | |
tree | b7f4e4032c1a607dfe11c1f8514d9526afb77350 | |
parent | 9bd405a328f9119ab6d2a1feed79643316e6f254 (diff) |
When converting header keys and values from bytes to Strings, assume ISO-8859-1 instead of UTF-8
-rw-r--r-- | src/lib.rs | 16 |
1 files changed, 8 insertions, 8 deletions
@@ -6,6 +6,8 @@ use std::error; use std::fmt; use std::str; +use encoding::Encoding; + #[derive(Debug)] pub enum MailParseError { QuotedPrintableDecodeError(quoted_printable::QuotedPrintableError), @@ -53,10 +55,9 @@ impl From<base64::Base64Error> for MailParseError { } } -impl From<str::Utf8Error> for MailParseError { - fn from(err: str::Utf8Error) -> MailParseError { - use std::error::Error; - MailParseError::Generic(err.description().to_string(), 0) +impl From<std::borrow::Cow<'static, str>> for MailParseError { + fn from(err: std::borrow::Cow<'static, str>) -> MailParseError { + MailParseError::Generic(err.into_owned().to_string(), 0) } } @@ -77,8 +78,7 @@ fn find_from(line: &str, ix_start: usize, key: &str) -> Option<usize> { impl<'a> MailHeader<'a> { pub fn get_key(&self) -> Result<String, MailParseError> { - let utf = try!(str::from_utf8(self.key)); - Ok(utf.trim().to_string()) + Ok(try!(encoding::all::ISO_8859_1.decode(self.key, encoding::DecoderTrap::Strict)).trim().to_string()) } fn decode_word(&self, encoded: &str) -> Result<String, MailParseError> { @@ -124,8 +124,8 @@ impl<'a> MailHeader<'a> { pub fn get_value(&self) -> Result<String, MailParseError> { let mut result = String::new(); - let utf = try!(str::from_utf8(self.value)); - let mut lines = utf.lines(); + let chars = try!(encoding::all::ISO_8859_1.decode(self.value, encoding::DecoderTrap::Strict)); + let mut lines = chars.lines(); let mut add_space = false; loop { let line = match lines.next() { |