summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKartikaya Gupta <kats@seldon.staktrace.com>2016-06-18 21:37:08 -0400
committerKartikaya Gupta <kats@seldon.staktrace.com>2016-06-22 10:10:31 -0400
commitaedfc081ba25ae3020ad4ddf08ec9af10eefabbe (patch)
tree1a681351b6c5a854ed8caaa9f52b28c3d68199c0 /src
parent93fe7484f8e7aecefd1850a234f0655040da1da3 (diff)
Allow quoted-printable header words to be wrapped in quotes
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs36
1 files changed, 31 insertions, 5 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 017f6a8..e42b041 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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]