diff options
author | Kartikaya Gupta <kats@seldon.staktrace.com> | 2016-06-18 21:37:08 -0400 |
---|---|---|
committer | Kartikaya Gupta <kats@seldon.staktrace.com> | 2016-06-22 10:10:31 -0400 |
commit | aedfc081ba25ae3020ad4ddf08ec9af10eefabbe (patch) | |
tree | 1a681351b6c5a854ed8caaa9f52b28c3d68199c0 | |
parent | 93fe7484f8e7aecefd1850a234f0655040da1da3 (diff) |
Allow quoted-printable header words to be wrapped in quotes
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/lib.rs | 36 |
2 files changed, 32 insertions, 6 deletions
@@ -1,6 +1,6 @@ [package] name = "mailparse" -version = "0.1.0" +version = "0.1.1" authors = ["Kartikaya Gupta <kats@seldon.staktrace.com>"] description = "A simple parser for MIME e-mail messages" documentation = "https://staktrace.github.io/mailparse/target/doc/mailparse/" @@ -90,9 +90,32 @@ pub struct MailHeader<'a> { value: &'a [u8], } -fn is_boundary(line: &str, ix: Option<usize>) -> bool { - ix.map_or_else(|| true, - |v| v >= line.len() || line.chars().nth(v).unwrap().is_whitespace()) +fn is_out_of_bounds(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; + } + false } fn find_from(line: &str, ix_start: usize, key: &str) -> Option<usize> { @@ -196,7 +219,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)) { + if !is_boundary(line, ix_begin.checked_sub(3), ix_begin.checked_sub(4)) { result.push_str(&line[ix_search..ix_begin]); ix_search = ix_begin; continue; @@ -206,7 +229,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)) { + if !is_boundary(line, ix_end.checked_add(2), ix_end.checked_add(3)) { ix_end_search = ix_end + 2; continue; } @@ -753,6 +776,9 @@ mod tests { let (parsed, _) = parse_header(b"NotSeparateWord2: =?utf-8?Q?hello?=world").unwrap(); assert_eq!(parsed.get_value().unwrap(), "=?utf-8?Q?hello?=world"); + + let (parsed, _) = parse_header(b"Key: \"=?utf-8?Q?value?=\"").unwrap(); + assert_eq!(parsed.get_value().unwrap(), "\"value\""); } #[test] |