Compare commits

...
5 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
mdecimus b6c080ff45 Updaded keywords 2023-09-22 13:39:53 +02:00
mdecimus 56d2c3b8b0 v0.9.1 2023-09-22 13:38:55 +02:00
mdecimus 816d944f2e Updated Rust version 2023-09-16 17:28:58 +02:00
14 changed files with 420 additions and 98 deletions
+11
View File
@@ -1,3 +1,14 @@
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)
- Removed `content_type()` and `address()` functions that could `panic!`. Use `as_content_type()` and `as_address()` instead.
- Updated Rust edition to 2021.
mail-parser 0.9.0
================================
This version introduces multiple breaking changes. Please read the following notes carefully.
+3 -3
View File
@@ -1,13 +1,13 @@
[package]
name = "mail-parser"
description = "Fast and robust e-mail parsing library for Rust"
version = "0.9.0"
edition = "2018"
version = "0.9.2"
edition = "2021"
authors = [ "Stalwart Labs <hello@stalw.art>"]
license = "Apache-2.0 OR MIT"
repository = "https://github.com/stalwartlabs/mail-parser"
homepage = "https://github.com/stalwartlabs/mail-parser"
keywords = ["email", "mime", "mail", "parser", "e-mail"]
keywords = ["email", "mail", "parser", "parse", "mime"]
categories = ["email", "parser-implementations"]
readme = "README.md"
resolver = "2"
+3 -4
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
@@ -167,7 +167,6 @@ R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
// Integrates with Serde
println!("{}", serde_json::to_string_pretty(&message).unwrap());
println!("{}", serde_yaml::to_string(&message).unwrap());
```
More examples available under the [examples](examples) directory. Please note that this library does not support building e-mail messages as this functionality is provided separately by the [`mail-builder`](https://crates.io/crates/mail-builder) crate.
+147
View File
@@ -0,0 +1,147 @@
{
"html_body": [
1
],
"text_body": [
1
],
"attachments": [
1
],
"parts": [
{
"headers": [
{
"name": "subject",
"value": {
"Text": "Hello world"
},
"offset_field": 0,
"offset_start": 8,
"offset_end": 22
},
{
"name": "from",
"value": {
"Address": {
"List": [
{
"name": "Test User",
"address": "test@example.org"
}
]
}
},
"offset_field": 22,
"offset_start": 27,
"offset_end": 58
},
{
"name": "to",
"value": {
"Address": {
"List": [
{
"name": "Another User",
"address": "test2@example.org"
}
]
}
},
"offset_field": 58,
"offset_start": 61,
"offset_end": 96
},
{
"name": "content_type",
"value": {
"ContentType": {
"c_type": "multipart",
"c_subtype": "mixed",
"attributes": [
[
"boundary",
"boundary"
]
]
}
},
"offset_field": 96,
"offset_start": 109,
"offset_end": 149
}
],
"is_encoding_problem": false,
"body": {
"Multipart": [
1
]
},
"offset_header": 0,
"offset_body": 151,
"offset_end": 348
},
{
"headers": [
{
"name": "content_type",
"value": {
"ContentType": {
"c_type": "image",
"c_subtype": "png",
"attributes": null
}
},
"offset_field": 163,
"offset_start": 176,
"offset_end": 188
},
{
"name": "content_transfer_encoding",
"value": {
"Text": "base64"
},
"offset_field": 188,
"offset_start": 214,
"offset_end": 223
},
{
"name": "content_disposition",
"value": "Empty",
"offset_field": 223,
"offset_start": 243,
"offset_end": 280
},
{
"name": "content_id",
"value": {
"Text": "image001.png@[...]"
},
"offset_field": 280,
"offset_start": 291,
"offset_end": 314
}
],
"is_encoding_problem": false,
"body": {
"InlineBinary": [
104,
101,
108,
108,
111,
32,
119,
111,
114,
108,
100,
10
]
},
"offset_header": 163,
"offset_body": 316,
"offset_end": 332
}
]
}
+15
View File
@@ -0,0 +1,15 @@
Subject: Hello world
From: Test User <test@example.org>
To: Another User <test2@example.org>
Content-Type: multipart/mixed
boundary="boundary"
--boundary
Content-Type: image/png
Content-Transfer-Encoding: base64
Content-Disposition: ;
filename="image001.png"
Content-ID: <image001.png@[...]>
aGVsbG8gd29ybGQK
--boundary--
+147
View File
@@ -0,0 +1,147 @@
{
"html_body": [
1
],
"text_body": [
1
],
"attachments": [
1
],
"parts": [
{
"headers": [
{
"name": "subject",
"value": {
"Text": "Hello world"
},
"offset_field": 0,
"offset_start": 8,
"offset_end": 21
},
{
"name": "from",
"value": {
"Address": {
"List": [
{
"name": "Test User",
"address": "test@example.org"
}
]
}
},
"offset_field": 21,
"offset_start": 26,
"offset_end": 56
},
{
"name": "to",
"value": {
"Address": {
"List": [
{
"name": "Another User",
"address": "test2@example.org"
}
]
}
},
"offset_field": 56,
"offset_start": 59,
"offset_end": 93
},
{
"name": "content_type",
"value": {
"ContentType": {
"c_type": "multipart",
"c_subtype": "mixed",
"attributes": [
[
"boundary",
"boundary"
]
]
}
},
"offset_field": 93,
"offset_start": 106,
"offset_end": 144
}
],
"is_encoding_problem": false,
"body": {
"Multipart": [
1
]
},
"offset_header": 0,
"offset_body": 145,
"offset_end": 333
},
{
"headers": [
{
"name": "content_type",
"value": {
"ContentType": {
"c_type": "image",
"c_subtype": "png",
"attributes": null
}
},
"offset_field": 156,
"offset_start": 169,
"offset_end": 180
},
{
"name": "content_transfer_encoding",
"value": {
"Text": "base64"
},
"offset_field": 180,
"offset_start": 206,
"offset_end": 214
},
{
"name": "content_disposition",
"value": "Empty",
"offset_field": 214,
"offset_start": 234,
"offset_end": 269
},
{
"name": "content_id",
"value": {
"Text": "image001.png@[...]"
},
"offset_field": 269,
"offset_start": 280,
"offset_end": 302
}
],
"is_encoding_problem": false,
"body": {
"InlineBinary": [
104,
101,
108,
108,
111,
32,
119,
111,
114,
108,
100,
10
]
},
"offset_header": 156,
"offset_body": 303,
"offset_end": 319
}
]
}
+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,
-17
View File
@@ -164,13 +164,6 @@ impl<'x> HeaderValue<'x> {
}
}
pub fn address(&self) -> &Address<'x> {
match *self {
HeaderValue::Address(ref a) => a,
_ => panic!("HeaderValue::address called on non-Address: {:?}", self),
}
}
pub fn as_address(&self) -> Option<&Address<'x>> {
match *self {
HeaderValue::Address(ref a) => Some(a),
@@ -185,16 +178,6 @@ impl<'x> HeaderValue<'x> {
}
}
pub fn content_type(&self) -> &ContentType<'x> {
match *self {
HeaderValue::ContentType(ref ct) => ct,
_ => panic!(
"HeaderValue::content_type called on non-ContentType: {:?}",
self
),
}
}
pub fn as_content_type(&self) -> Option<&ContentType> {
match *self {
HeaderValue::ContentType(ref c) => Some(c),
+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>,
}
+9 -6
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(),
@@ -272,7 +272,10 @@ impl MessageParser {
let is_inline = is_inline
&& part_headers
.header_value(&HeaderName::ContentDisposition)
.map_or_else(|| true, |d| !d.content_type().is_attachment())
.map_or_else(
|| true,
|d| !d.as_content_type().map_or(false, |ct| ct.is_attachment()),
)
&& (state.parts == 1
|| (state.mime_type != MimeType::MultipartRelated
&& (mime_type == MimeType::Inline
@@ -280,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),
@@ -437,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
{