From eb089b0655de60544810637b8a801b43c04bc9c0 Mon Sep 17 00:00:00 2001 From: Kartikaya Gupta Date: Wed, 22 Jun 2016 09:15:43 -0400 Subject: Recognize encoded words in more places --- src/lib.rs | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 5331159..660477e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -90,32 +90,17 @@ pub struct MailHeader<'a> { value: &'a [u8], } -fn is_out_of_bounds(line: &str, ix: Option) -> bool { +fn is_boundary(line: &str, ix: Option) -> bool { match ix { None => true, - Some(v) => v >= line.len(), - } -} - -fn is_boundary(line: &str, ix: Option, ix_next: Option) -> 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 { @@ -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] -- cgit v1.2.3