summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
authorKartikaya Gupta <kgupta@mozilla.com>2016-06-11 21:33:20 -0400
committerKartikaya Gupta <kgupta@mozilla.com>2016-06-11 21:33:20 -0400
commit8b90afb5342083337d592030850e4cb1a16cab0a (patch)
tree6e098ed5c65526349d047a0796680659f0799f0c /src/lib.rs
parent1ea38e332aa7d52aec2181ea4ebb9cc2933cc30b (diff)
Update header-parsing to skip past empty newline at the end of the headers, and also to allow CRLF instead of just LF
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 34617d8..bc1deb8 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -351,8 +351,18 @@ pub fn parse_headers(raw_data: &[u8]) -> Result<(Vec<MailHeader>, usize), MailPa
}));
headers.push(header);
ix = ix + ix_next;
- if ix >= raw_data.len() || raw_data[ix] == b'\n' {
+ if ix >= raw_data.len() {
break;
+ } else if raw_data[ix] == b'\n' {
+ ix = ix + 1;
+ break;
+ } else if raw_data[ix] == b'\r' {
+ if ix + 1 < raw_data.len() && raw_data[ix+1] == b'\n' {
+ ix = ix + 2;
+ break;
+ } else {
+ return Err(MailParseError::Generic("Headers were followed by an unexpected lone CR character!".to_string(), 0));
+ }
}
}
Ok((headers, ix))
@@ -555,6 +565,12 @@ mod tests {
assert_eq!(parsed.get_all_values("NoKey").unwrap(),
Vec::<String>::new());
+ let (parsed, _) =
+ parse_headers(b"Key: value\r\nWith: CRLF\r\n\r\nBody").unwrap();
+ assert_eq!(parsed.len(), 2);
+ assert_eq!(parsed.get_first_value("Key").unwrap(), Some("value".to_string()));
+ assert_eq!(parsed.get_first_value("With").unwrap(), Some("CRLF".to_string()));
+
assert_match!(parse_headers(b"Bad\nKey").unwrap_err(), MailParseError::Generic(_, 3));
assert_match!(parse_headers(b"K:V\nBad\nKey").unwrap_err(), MailParseError::Generic(_, 7));
}