diff options
author | Kartikaya Gupta <kats@seldon.staktrace.com> | 2016-06-22 09:15:43 -0400 |
---|---|---|
committer | Kartikaya Gupta <kats@seldon.staktrace.com> | 2016-06-22 10:10:55 -0400 |
commit | eb089b0655de60544810637b8a801b43c04bc9c0 (patch) | |
tree | 3b29af0a3b34e6ab76566309ef9b6f6ffd476755 /src | |
parent | fa2719efb0d164740d49d37a154212ad485dc7b1 (diff) |
Recognize encoded words in more places
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 39 |
1 files changed, 14 insertions, 25 deletions
@@ -90,32 +90,17 @@ pub struct MailHeader<'a> { value: &'a [u8], } -fn is_out_of_bounds(line: &str, ix: Option<usize>) -> bool { +fn is_boundary(line: &str, ix: Option<usize>) -> bool { match ix { None => true, - Some(v) => v >= line.len(), - } -} - -fn is_boundary(line: &str, ix: Option<usize>, ix_next: Option<usize>) -> bool { - if is_out_of_bounds(line, ix) { - return true; - } - let c = line.chars().nth(ix.unwrap()).unwrap(); - if c.is_whitespace() { - return true; - } - if c != '"' { - return false; - } - if is_out_of_bounds(line, ix_next) { - return true; - } - let c = line.chars().nth(ix_next.unwrap()).unwrap(); - if c.is_whitespace() { - return true; + Some(v) => { + if v >= line.len() { + return true; + } + let c = line.chars().nth(v).unwrap(); + return c.is_whitespace() || c == '"' || c == '(' || c == ')' || c == '<' || c == '>'; + } } - false } fn find_from(line: &str, ix_start: usize, key: &str) -> Option<usize> { @@ -219,7 +204,7 @@ impl<'a> MailHeader<'a> { match find_from(line, ix_search, "=?") { Some(v) => { let ix_begin = v + 2; - if !is_boundary(line, ix_begin.checked_sub(3), ix_begin.checked_sub(4)) { + if !is_boundary(line, ix_begin.checked_sub(3)) { result.push_str(&line[ix_search..ix_begin]); ix_search = ix_begin; continue; @@ -229,7 +214,7 @@ impl<'a> MailHeader<'a> { loop { match find_from(line, ix_end_search, "?=") { Some(ix_end) => { - if !is_boundary(line, ix_end.checked_add(2), ix_end.checked_add(3)) { + if !is_boundary(line, ix_end.checked_add(2)) { ix_end_search = ix_end + 2; continue; } @@ -785,6 +770,10 @@ mod tests { .unwrap(); assert_eq!(parsed.get_key().unwrap(), "Subject"); assert_eq!(parsed.get_value().unwrap(), "[Ontario Builder] Understanding home shopping \u{2013} a q uick survey"); + + let (parsed, _) = parse_header(b"Content-Type: image/jpeg; name=\"=?UTF-8?B?MDY2MTM5ODEuanBn?=\"").unwrap(); + assert_eq!(parsed.get_key().unwrap(), "Content-Type"); + assert_eq!(parsed.get_value().unwrap(), "image/jpeg; name=\"06613981.jpg\""); } #[test] |