Compare commits

..
2 Commits
Author SHA1 Message Date
mdecimus 65533d3faf v0.9.2 2023-11-27 09:34:59 +01:00
mdecimus 28a9b67b9a Fix Received struct serialization for bincode compatibility 2023-11-09 17:40:47 +01:00
10 changed files with 99 additions and 77 deletions
+5
View File
@@ -1,3 +1,8 @@
mail-parser 0.9.2
================================
- Fixed `quoted_printable_decode` external function (not used by mail-parser directly).
- Fix `Received` header serialization for bincode compatibility.
mail-parser 0.9.1
================================
- Fixed panic when Content-Disposition is empty (#63)
+1 -1
View File
@@ -1,7 +1,7 @@
[package]
name = "mail-parser"
description = "Fast and robust e-mail parsing library for Rust"
version = "0.9.1"
version = "0.9.2"
edition = "2021"
authors = [ "Stalwart Labs <hello@stalw.art>"]
license = "Apache-2.0 OR MIT"
+3 -3
View File
@@ -27,9 +27,9 @@ Performance and memory safety were two important factors while designing _mail-p
- **High performance Base64 decoding** based on Chromium's decoder ([the fastest non-SIMD decoder](https://github.com/lemire/fastbase64)).
- **Fast parsing** of message header fields, character set names and HTML entities using [perfect hashing](https://en.wikipedia.org/wiki/Perfect_hash_function).
- Written in **100% safe** Rust with no external dependencies.
- Every function in the library has been [fuzzed](#testing-fuzzing--benchmarking) and
thoroughly [tested with MIRI](#testing-fuzzing--benchmarking).
- **Battle-tested** with millions of real-world e-mail messages dating from 1995 until today.
- Every function in the library has been [fuzzed](#testing-fuzzing--benchmarking) and thoroughly [tested with MIRI](#testing-fuzzing--benchmarking).
- **Battle-tested** with millions of real-world e-mail messages dating from 1995 until today.
- Used in production environments worldwide by [Stalwart Mail Server](https://github.com/stalwartlabs/mail-server).
## Usage Example
+14
View File
@@ -192,12 +192,19 @@
"Name": "mailhost.whitehouse.gov"
},
"from_ip": "192.168.51.200",
"from_iprev": null,
"by": {
"Name": "heartbeat.whitehouse.gov"
},
"for_": "vice-president@heartbeat.whitehouse.gov",
"with": "ESMTP",
"tls_version": null,
"tls_cipher": null,
"id": "SAA22453",
"ident": null,
"helo": null,
"helo_cmd": null,
"via": null,
"date": {
"year": 1998,
"month": 8,
@@ -223,12 +230,19 @@
"Name": "the_big_box.whitehouse.gov"
},
"from_ip": "192.168.51.50",
"from_iprev": null,
"by": {
"Name": "mailhost.whitehouse.gov"
},
"for_": "vice-president@whitehouse.gov",
"with": "ESMTP",
"tls_version": null,
"tls_cipher": null,
"id": "RAA20366",
"ident": null,
"helo": null,
"helo_cmd": null,
"via": null,
"date": {
"year": 1998,
"month": 8,
+14
View File
@@ -192,12 +192,19 @@
"Name": "mailhost.whitehouse.gov"
},
"from_ip": "192.168.51.200",
"from_iprev": null,
"by": {
"Name": "heartbeat.whitehouse.gov"
},
"for_": "vice-president@heartbeat.whitehouse.gov",
"with": "ESMTP",
"tls_version": null,
"tls_cipher": null,
"id": "SAA22453\n",
"ident": null,
"helo": null,
"helo_cmd": null,
"via": null,
"date": {
"year": 1998,
"month": 8,
@@ -223,12 +230,19 @@
"Name": "the_big_box.whitehouse.gov"
},
"from_ip": "192.168.51.50",
"from_iprev": null,
"by": {
"Name": "mailhost.whitehouse.gov"
},
"for_": "vice-president@whitehouse.gov",
"with": "ESMTP",
"tls_version": null,
"tls_cipher": null,
"id": "RAA20366\n",
"ident": null,
"helo": null,
"helo_cmd": null,
"via": null,
"date": {
"year": 1998,
"month": 8,
+8
View File
@@ -39,10 +39,18 @@
"IpAddr": "10.1.1.1"
},
"from_ip": "10.1.1.1",
"from_iprev": null,
"by": {
"Name": "xyz-webserver.abcd-gestion.com"
},
"for_": null,
"with": "ESMTPA",
"tls_version": null,
"tls_cipher": null,
"ident": null,
"helo": null,
"helo_cmd": null,
"via": null,
"date": {
"year": 2022,
"month": 7,
+8
View File
@@ -39,10 +39,18 @@
"IpAddr": "10.1.1.1"
},
"from_ip": "10.1.1.1",
"from_iprev": null,
"by": {
"Name": "xyz-webserver.abcd-gestion.com"
},
"for_": null,
"with": "ESMTPA",
"tls_version": null,
"tls_cipher": null,
"ident": null,
"helo": null,
"helo_cmd": null,
"via": null,
"date": {
"year": 2022,
"month": 7,
+27 -6
View File
@@ -25,6 +25,8 @@ pub fn quoted_printable_decode(bytes: &[u8]) -> Option<Vec<u8>> {
let mut state = QuotedPrintableState::None;
let mut hex1 = 0;
let mut ws_count = 0;
let mut crlf = b"\n".as_ref();
for &ch in bytes {
match ch {
@@ -39,12 +41,23 @@ pub fn quoted_printable_decode(bytes: &[u8]) -> Option<Vec<u8>> {
if QuotedPrintableState::Eq == state {
state = QuotedPrintableState::None;
} else {
buf.push(b'\n');
if ws_count > 0 {
buf.truncate(buf.len() - ws_count);
}
buf.extend_from_slice(crlf);
}
ws_count = 0;
}
b'\r' => {
crlf = b"\r\n".as_ref();
}
b'\r' => (),
_ => match state {
QuotedPrintableState::None => {
if ch.is_ascii_whitespace() {
ws_count += 1;
} else {
ws_count = 0;
}
buf.push(ch);
}
QuotedPrintableState::Eq => {
@@ -59,7 +72,7 @@ pub fn quoted_printable_decode(bytes: &[u8]) -> Option<Vec<u8>> {
if hex1 != -1 {
state = QuotedPrintableState::Hex1;
} else {
} else if !ch.is_ascii_whitespace() {
return None;
}
}
@@ -72,6 +85,7 @@ pub fn quoted_printable_decode(bytes: &[u8]) -> Option<Vec<u8>> {
state = QuotedPrintableState::None;
if hex2 != -1 {
buf.push(((hex1 as u8) << 4) | hex2 as u8);
ws_count = 0;
} else {
return None;
}
@@ -337,14 +351,21 @@ mod tests {
concat!(
"Die Hasen klagten einst uber ihre Lage; \"wir leben\", ",
"sprach ein Redner, \"in steter Furcht vor Menschen und ",
"Tieren, eine Beute der Hunde, der\n"
"Tieren, eine Beute der Hunde, der\r\n"
),
),
(
concat!(
"hello \r\nbar=\r\n\r\nfoo\t=\r\nbar\r\nfoo\t \t= \r\n=62\r\nfoo = ",
"\t\r\nbar\r\nfoo =\r\n=62\r\nfoo \r\nbar=\r\n\r\nfoo_bar\r\n"
),
"hello\r\nbar\r\nfoo\tbar\r\nfoo\t \tb\r\nfoo bar\r\nfoo b\r\nfoo\r\nbar\r\nfoo_bar\r\n",
),
("\n\n", "\n\n"),
] {
assert_eq!(
super::quoted_printable_decode(encoded_str.as_bytes()).unwrap_or_default(),
expected_result.as_bytes(),
String::from_utf8(super::quoted_printable_decode(encoded_str.as_bytes()).unwrap_or_default()).unwrap(),
expected_result,
"Failed for {encoded_str:?}",
);
}
+14 -62
View File
@@ -33,9 +33,9 @@
//! - **High performance Base64 decoding** based on Chromium's decoder ([the fastest non-SIMD decoder](https://github.com/lemire/fastbase64)).
//! - **Fast parsing** of message header fields, character set names and HTML entities using [perfect hashing](https://en.wikipedia.org/wiki/Perfect_hash_function).
//! - Written in **100% safe** Rust with no external dependencies.
//! - Every function in the library has been [fuzzed](#testing-fuzzing--benchmarking) and
//! thoroughly [tested with MIRI](#testing-fuzzing--benchmarking).
//! - Every function in the library has been [fuzzed](#testing-fuzzing--benchmarking) and thoroughly [tested with MIRI](#testing-fuzzing--benchmarking).
//! - **Battle-tested** with millions of real-world e-mail messages dating from 1995 until today.
//! - Used in production environments worldwide by [Stalwart Mail Server](https://github.com/stalwartlabs/mail-server).
//!
//! Jump to the [example](#usage-example).
//!
@@ -521,82 +521,34 @@ pub struct DateTime {
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Received<'x> {
#[cfg_attr(
feature = "serde_support",
serde(skip_serializing_if = "Option::is_none"),
serde(default)
)]
#[cfg_attr(feature = "serde_support", serde(default))]
pub from: Option<Host<'x>>,
#[cfg_attr(
feature = "serde_support",
serde(skip_serializing_if = "Option::is_none"),
serde(default)
)]
#[cfg_attr(feature = "serde_support", serde(default))]
pub from_ip: Option<IpAddr>,
#[cfg_attr(
feature = "serde_support",
serde(skip_serializing_if = "Option::is_none"),
serde(default)
)]
#[cfg_attr(feature = "serde_support", serde(default))]
pub from_iprev: Option<Cow<'x, str>>,
#[cfg_attr(
feature = "serde_support",
serde(skip_serializing_if = "Option::is_none"),
serde(default)
)]
#[cfg_attr(feature = "serde_support", serde(default))]
pub by: Option<Host<'x>>,
#[cfg_attr(
feature = "serde_support",
serde(skip_serializing_if = "Option::is_none"),
serde(default)
)]
#[cfg_attr(feature = "serde_support", serde(default))]
pub for_: Option<Cow<'x, str>>,
#[cfg_attr(
feature = "serde_support",
serde(skip_serializing_if = "Option::is_none"),
serde(default)
)]
#[cfg_attr(feature = "serde_support", serde(default))]
pub with: Option<Protocol>,
#[cfg_attr(
feature = "serde_support",
serde(skip_serializing_if = "Option::is_none"),
serde(default)
)]
#[cfg_attr(feature = "serde_support", serde(default))]
pub tls_version: Option<TlsVersion>,
#[cfg_attr(
feature = "serde_support",
serde(skip_serializing_if = "Option::is_none"),
serde(default)
)]
#[cfg_attr(feature = "serde_support", serde(default))]
pub tls_cipher: Option<Cow<'x, str>>,
#[cfg_attr(
feature = "serde_support",
serde(skip_serializing_if = "Option::is_none")
)]
pub id: Option<Cow<'x, str>>,
#[cfg_attr(
feature = "serde_support",
serde(skip_serializing_if = "Option::is_none"),
serde(default)
)]
#[cfg_attr(feature = "serde_support", serde(default))]
pub ident: Option<Cow<'x, str>>,
#[cfg_attr(
feature = "serde_support",
serde(skip_serializing_if = "Option::is_none"),
serde(default)
)]
#[cfg_attr(feature = "serde_support", serde(default))]
pub helo: Option<Host<'x>>,
#[cfg_attr(
feature = "serde_support",
serde(skip_serializing_if = "Option::is_none"),
serde(default)
)]
#[cfg_attr(feature = "serde_support", serde(default))]
pub helo_cmd: Option<Greeting>,
#[cfg_attr(
feature = "serde_support",
serde(skip_serializing_if = "Option::is_none"),
serde(default)
)]
#[cfg_attr(feature = "serde_support", serde(default))]
pub via: Option<Cow<'x, str>>,
pub date: Option<DateTime>,
}
+5 -5
View File
@@ -24,7 +24,7 @@ const MAX_NESTED_ENCODED: usize = 3;
#[derive(Debug, PartialEq, Default)]
enum MimeType {
MultipartMixed,
MultipartAlernative,
MultipartAlternative,
MultipartRelated,
MultipartDigest,
TextPlain,
@@ -49,7 +49,7 @@ fn mime_type(
false,
match content_type.subtype() {
Some("mixed") => MimeType::MultipartMixed,
Some("alternative") => MimeType::MultipartAlernative,
Some("alternative") => MimeType::MultipartAlternative,
Some("related") => MimeType::MultipartRelated,
Some("digest") => MimeType::MultipartDigest,
_ => MimeType::Other,
@@ -170,7 +170,7 @@ impl MessageParser {
let part_id = message.parts.len();
let new_state = MessageParserState {
in_alternative: state.in_alternative
|| mime_type == MimeType::MultipartAlernative,
|| mime_type == MimeType::MultipartAlternative,
mime_type,
mime_boundary: mime_boundary.as_bytes().to_vec().into(),
html_parts: message.html_body.len(),
@@ -283,7 +283,7 @@ impl MessageParser {
.map_or_else(|| true, |c| !c.has_attribute("name")))));
let (add_to_html, add_to_text) =
if let MimeType::MultipartAlernative = state.mime_type {
if let MimeType::MultipartAlternative = state.mime_type {
match mime_type {
MimeType::TextHtml => (true, false),
MimeType::TextPlain => (false, true),
@@ -440,7 +440,7 @@ impl MessageParser {
if stream.is_multipart_end() {
// End of MIME part reached
if MimeType::MultipartAlernative == state.mime_type
if MimeType::MultipartAlternative == state.mime_type
&& state.need_html_body
&& state.need_text_body
{