mirror of
https://github.com/stalwartlabs/mail-parser.git
synced 2026-11-29 00:01:49 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c177ffaca3 | ||
|
|
fa34e10927 | ||
|
|
2b37017cf8 | ||
|
|
5f25e1291f | ||
|
|
8b2ad20bf3 | ||
|
|
bc6a5b1e94 | ||
|
|
20fa3dbd46 | ||
|
|
e97d14ad89 | ||
|
|
ed16429bcc | ||
|
|
c733600b75 | ||
|
|
76669a32c6 | ||
|
|
c8bf355494 | ||
|
|
9bf766dcf1 | ||
|
|
0eba00b4e9 | ||
|
|
0baffddb28 | ||
|
|
54ca7df6eb | ||
|
|
c3f28cb4d1 | ||
|
|
e512a66bb0 | ||
|
|
6a48ed69b6 | ||
|
|
dfda00c939 | ||
|
|
a96497d7a3 | ||
|
|
ad4dff69c3 | ||
|
|
cf87d3556c | ||
|
|
4ff4c57e2c | ||
|
|
598cba3b87 | ||
|
|
716c0d747e |
@@ -1,13 +0,0 @@
|
||||
# These are supported funding model platforms
|
||||
|
||||
github: stalwartlabs
|
||||
patreon: # Replace with a single Patreon username
|
||||
open_collective: # Replace with a single Open Collective username
|
||||
ko_fi: # Replace with a single Ko-fi username
|
||||
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
||||
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
||||
liberapay: # Replace with a single Liberapay username
|
||||
issuehunt: # Replace with a single IssueHunt username
|
||||
otechie: # Replace with a single Otechie username
|
||||
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
|
||||
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
||||
@@ -1,3 +1,24 @@
|
||||
mail-parser 0.10.0
|
||||
================================
|
||||
- Perfect hashing using `hashify` crate rather than static `gperf` generated code.
|
||||
- Added `DkimSignature`, `ArcAuthenticationResults`, `ArcMessageSignature` and `ArcSeal` headers.
|
||||
- `HeaderName` is non-exhaustive.
|
||||
- Parse obsolete timezones (#95).
|
||||
- Fix: Folding ws between "Content-Type:" and "plain/text" leads to empty header (#96).
|
||||
- Fix: Multiline quoted continuations (closes #92).
|
||||
- Fix: Deserialize (#93).
|
||||
- Retain mbox IO errors (#91).
|
||||
- Hide concrete type behind impl type (#94).
|
||||
- Removed `ludicrous` feature, the Rust compiler is smart enough to optimize array lookups.
|
||||
|
||||
mail-parser 0.9.4
|
||||
================================
|
||||
- Flexible parsing of charset names (#85).
|
||||
|
||||
mail-parser 0.9.3
|
||||
================================
|
||||
- Fixed parsing of address names containing @ (#80)
|
||||
|
||||
mail-parser 0.9.2
|
||||
================================
|
||||
- Fixed `quoted_printable_decode` external function (not used by mail-parser directly).
|
||||
|
||||
+7
-4
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "mail-parser"
|
||||
description = "Fast and robust e-mail parsing library for Rust"
|
||||
version = "0.9.2"
|
||||
version = "0.10.0"
|
||||
edition = "2021"
|
||||
authors = [ "Stalwart Labs <hello@stalw.art>"]
|
||||
license = "Apache-2.0 OR MIT"
|
||||
@@ -13,8 +13,9 @@ readme = "README.md"
|
||||
resolver = "2"
|
||||
|
||||
[dependencies]
|
||||
encoding_rs = { version="0.8", optional=true }
|
||||
serde = { version = "1.0", features = ["derive"], optional=true }
|
||||
hashify = { version = "0.2.1" }
|
||||
encoding_rs = { version = "0.8", optional = true }
|
||||
serde = { version = "1.0", features = ["derive"], optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
mail-parser = { path = ".", features = ["serde_support", "full_encoding"] }
|
||||
@@ -27,7 +28,9 @@ chrono = "0.4"
|
||||
default = ["full_encoding"]
|
||||
full_encoding = ["encoding_rs"]
|
||||
serde_support = ["serde"]
|
||||
ludicrous_mode = []
|
||||
|
||||
[profile.bench]
|
||||
debug = true
|
||||
|
||||
[lib]
|
||||
doctest = false
|
||||
|
||||
@@ -277,4 +277,4 @@ at your option.
|
||||
|
||||
## Copyright
|
||||
|
||||
Copyright (C) 2020-2022, Stalwart Labs Ltd.
|
||||
Copyright (C) 2020, Stalwart Labs LLC
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use mail_parser::{HeaderName, MessageParser};
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use mail_parser::{mailbox::mbox::MessageIterator, MessageParser};
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use mail_parser::*;
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use mail_parser::*;
|
||||
|
||||
+34
-1
@@ -744,5 +744,38 @@
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"header": "\"phil@example.com\" <phil@example.com>\n",
|
||||
"expected": {
|
||||
"List": [
|
||||
{
|
||||
"name": "phil@example.com",
|
||||
"address": "phil@example.com"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"header": "\"<phil@example.com>\" <phil@example.com>\n",
|
||||
"expected": {
|
||||
"List": [
|
||||
{
|
||||
"name": "<phil@example.com>",
|
||||
"address": "phil@example.com"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"header": "phil@example.com <phil@example.com>\n",
|
||||
"expected": {
|
||||
"List": [
|
||||
{
|
||||
"name": "phil@example.com",
|
||||
"address": "phil@example.com"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
@@ -1204,5 +1204,25 @@
|
||||
]
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"header": "attachment;\r\n\tfilename=\"=?utf-8?Q?Auftragsbest=C3=A4tigung_Xxxxx_Xxxxxx_XXXXXXXXXX=2EPD?=\r\n =?utf-8?Q?F?=\"\r\n",
|
||||
"expected": {
|
||||
"c_type": "attachment",
|
||||
"c_subtype": null,
|
||||
"attributes": [
|
||||
[
|
||||
"filename",
|
||||
"Auftragsbestätigung Xxxxx Xxxxxx XXXXXXXXXX.PDF"
|
||||
]
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"header": "\n text/plain\r\n",
|
||||
"expected": {
|
||||
"c_type": "text",
|
||||
"c_subtype": "plain"
|
||||
}
|
||||
}
|
||||
]
|
||||
+57
-1
@@ -170,7 +170,7 @@
|
||||
{
|
||||
"header": " 4 8 15 16 23 42, 4 8 15 16 23 42, 4 8 15 16 23 42, ",
|
||||
"expected": {
|
||||
"year": 1915,
|
||||
"year": 2015,
|
||||
"month": 8,
|
||||
"day": 4,
|
||||
"hour": 16,
|
||||
@@ -384,5 +384,61 @@
|
||||
"tz_hour": 7,
|
||||
"tz_minute": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"header": "Sun, 27 Oct 2002 23:57:07 EST",
|
||||
"expected": {
|
||||
"year": 2002,
|
||||
"month": 10,
|
||||
"day": 27,
|
||||
"hour": 23,
|
||||
"minute": 57,
|
||||
"second": 7,
|
||||
"tz_before_gmt": true,
|
||||
"tz_hour": 5,
|
||||
"tz_minute": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"header": "Thu, 10 Jul 1997 14:53:31 EST5EDT",
|
||||
"expected": {
|
||||
"year": 1997,
|
||||
"month": 7,
|
||||
"day": 10,
|
||||
"hour": 14,
|
||||
"minute": 53,
|
||||
"second": 31,
|
||||
"tz_before_gmt": true,
|
||||
"tz_hour": 5,
|
||||
"tz_minute": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"header": "Thu, 10 Jul 1997 14:53:31 A",
|
||||
"expected": {
|
||||
"year": 1997,
|
||||
"month": 7,
|
||||
"day": 10,
|
||||
"hour": 14,
|
||||
"minute": 53,
|
||||
"second": 31,
|
||||
"tz_before_gmt": false,
|
||||
"tz_hour": 0,
|
||||
"tz_minute": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"header": "Thu, 10 Jul 1997 14:53:31 ZZZZ",
|
||||
"expected": {
|
||||
"year": 1997,
|
||||
"month": 7,
|
||||
"day": 10,
|
||||
"hour": 14,
|
||||
"minute": 53,
|
||||
"second": 31,
|
||||
"tz_before_gmt": false,
|
||||
"tz_hour": 0,
|
||||
"tz_minute": 0
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -146,11 +146,13 @@
|
||||
"is_encoding_problem": false,
|
||||
"body": {
|
||||
"Message": {
|
||||
"html_body": [],
|
||||
"text_body": [],
|
||||
"attachments": [
|
||||
"html_body": [
|
||||
0
|
||||
],
|
||||
"text_body": [
|
||||
0
|
||||
],
|
||||
"attachments": [],
|
||||
"parts": [
|
||||
{
|
||||
"headers": [
|
||||
|
||||
@@ -146,11 +146,13 @@
|
||||
"is_encoding_problem": false,
|
||||
"body": {
|
||||
"Message": {
|
||||
"html_body": [],
|
||||
"text_body": [],
|
||||
"attachments": [
|
||||
"html_body": [
|
||||
0
|
||||
],
|
||||
"text_body": [
|
||||
0
|
||||
],
|
||||
"attachments": [],
|
||||
"parts": [
|
||||
{
|
||||
"headers": [
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
{
|
||||
"html_body": [
|
||||
0
|
||||
],
|
||||
"text_body": [
|
||||
0
|
||||
],
|
||||
"attachments": [],
|
||||
"parts": [
|
||||
{
|
||||
"headers": [
|
||||
{
|
||||
"name": "content_type",
|
||||
"value": {
|
||||
"ContentType": {
|
||||
"c_type": "text",
|
||||
"c_subtype": "plain",
|
||||
"attributes": [
|
||||
[
|
||||
"charset",
|
||||
"Windows-1252"
|
||||
]
|
||||
]
|
||||
}
|
||||
},
|
||||
"offset_field": 0,
|
||||
"offset_start": 13,
|
||||
"offset_end": 52
|
||||
},
|
||||
{
|
||||
"name": "content_transfer_encoding",
|
||||
"value": {
|
||||
"Text": "quoted-printable"
|
||||
},
|
||||
"offset_field": 52,
|
||||
"offset_start": 78,
|
||||
"offset_end": 97
|
||||
},
|
||||
{
|
||||
"name": "mime_version",
|
||||
"value": {
|
||||
"Text": "1.0"
|
||||
},
|
||||
"offset_field": 97,
|
||||
"offset_start": 110,
|
||||
"offset_end": 116
|
||||
}
|
||||
],
|
||||
"is_encoding_problem": true,
|
||||
"body": {
|
||||
"Text": "Best\r\nEnviado desde mi BlackBerry=AE de Vodafone=\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\r\n<plist version=\"1.0\">\r\n<dict>\r\n</dict>\r\n</plist>"
|
||||
},
|
||||
"offset_header": 0,
|
||||
"offset_body": 118,
|
||||
"offset_end": 361
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
Content-Type: text/plain;
|
||||
charset="Windows-1252"
|
||||
Content-Transfer-Encoding: quoted-printable
|
||||
MIME-Version: 1.0
|
||||
|
||||
Best
|
||||
Enviado desde mi BlackBerry=AE de Vodafone=
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,58 @@
|
||||
{
|
||||
"html_body": [
|
||||
0
|
||||
],
|
||||
"text_body": [
|
||||
0
|
||||
],
|
||||
"attachments": [],
|
||||
"parts": [
|
||||
{
|
||||
"headers": [
|
||||
{
|
||||
"name": "content_type",
|
||||
"value": {
|
||||
"ContentType": {
|
||||
"c_type": "text",
|
||||
"c_subtype": "plain",
|
||||
"attributes": [
|
||||
[
|
||||
"charset",
|
||||
"Windows-1252"
|
||||
]
|
||||
]
|
||||
}
|
||||
},
|
||||
"offset_field": 0,
|
||||
"offset_start": 13,
|
||||
"offset_end": 52
|
||||
},
|
||||
{
|
||||
"name": "content_transfer_encoding",
|
||||
"value": {
|
||||
"Text": "quoted-printable"
|
||||
},
|
||||
"offset_field": 52,
|
||||
"offset_start": 78,
|
||||
"offset_end": 97
|
||||
},
|
||||
{
|
||||
"name": "mime_version",
|
||||
"value": {
|
||||
"Text": "1.0"
|
||||
},
|
||||
"offset_field": 97,
|
||||
"offset_start": 110,
|
||||
"offset_end": 116
|
||||
}
|
||||
],
|
||||
"is_encoding_problem": true,
|
||||
"body": {
|
||||
"Text": "Best\nEnviado desde mi BlackBerry=AE de Vodafone=\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n</dict>\n</plist>"
|
||||
},
|
||||
"offset_header": 0,
|
||||
"offset_body": 118,
|
||||
"offset_end": 354
|
||||
}
|
||||
]
|
||||
}
|
||||
+5
-10
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use crate::{Addr, Address, Group};
|
||||
@@ -73,7 +68,7 @@ impl<'x> Address<'x> {
|
||||
}
|
||||
|
||||
/// Returns an iterator over the addresses in the list, or the addresses in the groups.
|
||||
pub fn iter<'y: 'x>(&'y self) -> Box<dyn DoubleEndedIterator<Item = &Addr<'x>> + 'x> {
|
||||
pub fn iter(&self) -> Box<dyn DoubleEndedIterator<Item = &Addr<'x>> + '_ + Sync + Send> {
|
||||
match self {
|
||||
Address::List(list) => Box::new(list.iter()),
|
||||
Address::Group(group) => {
|
||||
@@ -88,13 +83,13 @@ impl<'x> Address<'x> {
|
||||
Address::List(list) => list.iter().any(|a| {
|
||||
a.address
|
||||
.as_ref()
|
||||
.map_or(false, |a| a.eq_ignore_ascii_case(addr))
|
||||
.is_some_and(|a| a.eq_ignore_ascii_case(addr))
|
||||
}),
|
||||
Address::Group(group) => group.iter().any(|group| {
|
||||
group.addresses.iter().any(|a| {
|
||||
a.address
|
||||
.as_ref()
|
||||
.map_or(false, |a| a.eq_ignore_ascii_case(addr))
|
||||
.is_some_and(|a| a.eq_ignore_ascii_case(addr))
|
||||
})
|
||||
}),
|
||||
}
|
||||
|
||||
+3
-8
@@ -1,17 +1,12 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use crate::{AttachmentIterator, BodyPartIterator, Message, MessagePart, MessagePartId, PartType};
|
||||
|
||||
impl<'x> PartType<'x> {
|
||||
impl PartType<'_> {
|
||||
#[allow(clippy::len_without_is_empty)]
|
||||
pub fn len(&self) -> usize {
|
||||
match self {
|
||||
|
||||
+2
-7
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use crate::{HeaderName, HeaderValue, MessageParser};
|
||||
|
||||
+62
-43
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use core::fmt;
|
||||
@@ -26,7 +21,7 @@ impl<'x> Header<'x> {
|
||||
}
|
||||
|
||||
/// Returns the parsed header value
|
||||
pub fn value(&self) -> &HeaderValue {
|
||||
pub fn value(&self) -> &HeaderValue<'x> {
|
||||
&self.value
|
||||
}
|
||||
|
||||
@@ -156,10 +151,10 @@ impl<'x> HeaderValue<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_text_list(&self) -> Option<Vec<&str>> {
|
||||
pub fn as_text_list(&self) -> Option<&[Cow<'x, str>]> {
|
||||
match *self {
|
||||
HeaderValue::Text(ref s) => Some(vec![s.as_ref()]),
|
||||
HeaderValue::TextList(ref l) => Some(l.iter().map(|l| l.as_ref()).collect()),
|
||||
HeaderValue::Text(ref s) => Some(std::slice::from_ref(s)),
|
||||
HeaderValue::TextList(ref l) => Some(l.as_slice()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -171,14 +166,14 @@ impl<'x> HeaderValue<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_received(&self) -> Option<&Received> {
|
||||
pub fn as_received(&self) -> Option<&Received<'x>> {
|
||||
match *self {
|
||||
HeaderValue::Received(ref r) => Some(r),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_content_type(&self) -> Option<&ContentType> {
|
||||
pub fn as_content_type(&self) -> Option<&ContentType<'x>> {
|
||||
match *self {
|
||||
HeaderValue::ContentType(ref c) => Some(c),
|
||||
_ => None,
|
||||
@@ -259,7 +254,7 @@ impl PartialEq for HeaderName<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> Hash for HeaderName<'x> {
|
||||
impl Hash for HeaderName<'_> {
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
match self {
|
||||
HeaderName::Other(value) => {
|
||||
@@ -280,7 +275,7 @@ impl<'x> From<HeaderName<'x>> for u8 {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> HeaderName<'x> {
|
||||
impl HeaderName<'_> {
|
||||
pub fn to_owned(&self) -> HeaderName<'static> {
|
||||
match self {
|
||||
HeaderName::Other(name) => HeaderName::Other(name.to_string().into()),
|
||||
@@ -321,6 +316,10 @@ impl<'x> HeaderName<'x> {
|
||||
HeaderName::ListPost => HeaderName::ListPost,
|
||||
HeaderName::ListSubscribe => HeaderName::ListSubscribe,
|
||||
HeaderName::ListUnsubscribe => HeaderName::ListUnsubscribe,
|
||||
HeaderName::ArcAuthenticationResults => HeaderName::ArcAuthenticationResults,
|
||||
HeaderName::ArcMessageSignature => HeaderName::ArcMessageSignature,
|
||||
HeaderName::ArcSeal => HeaderName::ArcSeal,
|
||||
HeaderName::DkimSignature => HeaderName::DkimSignature,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -364,6 +363,10 @@ impl<'x> HeaderName<'x> {
|
||||
HeaderName::ListPost => HeaderName::ListPost,
|
||||
HeaderName::ListSubscribe => HeaderName::ListSubscribe,
|
||||
HeaderName::ListUnsubscribe => HeaderName::ListUnsubscribe,
|
||||
HeaderName::ArcAuthenticationResults => HeaderName::ArcAuthenticationResults,
|
||||
HeaderName::ArcMessageSignature => HeaderName::ArcMessageSignature,
|
||||
HeaderName::ArcSeal => HeaderName::ArcSeal,
|
||||
HeaderName::DkimSignature => HeaderName::DkimSignature,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -374,7 +377,7 @@ impl<'x> HeaderName<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_str<'y: 'x>(&'y self) -> &'x str {
|
||||
pub fn as_str(&self) -> &str {
|
||||
match self {
|
||||
HeaderName::Other(other) => other.as_ref(),
|
||||
_ => self.as_static_str(),
|
||||
@@ -420,6 +423,10 @@ impl<'x> HeaderName<'x> {
|
||||
HeaderName::ListPost => "List-Post",
|
||||
HeaderName::ListSubscribe => "List-Subscribe",
|
||||
HeaderName::ListUnsubscribe => "List-Unsubscribe",
|
||||
HeaderName::ArcAuthenticationResults => "ARC-Authentication-Results",
|
||||
HeaderName::ArcMessageSignature => "ARC-Message-Signature",
|
||||
HeaderName::ArcSeal => "ARC-Seal",
|
||||
HeaderName::DkimSignature => "DKIM-Signature",
|
||||
HeaderName::Other(_) => "",
|
||||
}
|
||||
}
|
||||
@@ -463,6 +470,10 @@ impl<'x> HeaderName<'x> {
|
||||
HeaderName::ListPost => "List-Post".len(),
|
||||
HeaderName::ListSubscribe => "List-Subscribe".len(),
|
||||
HeaderName::ListUnsubscribe => "List-Unsubscribe".len(),
|
||||
HeaderName::ArcAuthenticationResults => "ARC-Authentication-Results".len(),
|
||||
HeaderName::ArcMessageSignature => "ARC-Message-Signature".len(),
|
||||
HeaderName::ArcSeal => "ARC-Seal".len(),
|
||||
HeaderName::DkimSignature => "DKIM-Signature".len(),
|
||||
HeaderName::Other(other) => other.len(),
|
||||
}
|
||||
}
|
||||
@@ -530,6 +541,10 @@ impl<'x> HeaderName<'x> {
|
||||
HeaderName::ListSubscribe => 35,
|
||||
HeaderName::ListUnsubscribe => 36,
|
||||
HeaderName::Other(_) => 37,
|
||||
HeaderName::ArcAuthenticationResults => 38,
|
||||
HeaderName::ArcMessageSignature => 39,
|
||||
HeaderName::ArcSeal => 40,
|
||||
HeaderName::DkimSignature => 41,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -542,7 +557,7 @@ impl<'x> MimeHeaders<'x> for Message<'x> {
|
||||
.and_then(|header| header.as_text())
|
||||
}
|
||||
|
||||
fn content_disposition(&self) -> Option<&ContentType> {
|
||||
fn content_disposition(&self) -> Option<&ContentType<'x>> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ContentDisposition)
|
||||
@@ -563,14 +578,14 @@ impl<'x> MimeHeaders<'x> for Message<'x> {
|
||||
.and_then(|header| header.as_text())
|
||||
}
|
||||
|
||||
fn content_type(&self) -> Option<&ContentType> {
|
||||
fn content_type(&self) -> Option<&ContentType<'x>> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ContentType)
|
||||
.and_then(|header| header.as_content_type())
|
||||
}
|
||||
|
||||
fn content_language(&self) -> &HeaderValue {
|
||||
fn content_language(&self) -> &HeaderValue<'x> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ContentLanguage)
|
||||
@@ -587,29 +602,29 @@ impl<'x> MimeHeaders<'x> for Message<'x> {
|
||||
|
||||
impl<'x> MessagePart<'x> {
|
||||
/// Returns the body part's contents as a `u8` slice
|
||||
pub fn contents(&'x self) -> &'x [u8] {
|
||||
pub fn contents(&self) -> &[u8] {
|
||||
match &self.body {
|
||||
PartType::Text(text) | PartType::Html(text) => text.as_bytes(),
|
||||
PartType::Binary(bin) | PartType::InlineBinary(bin) => bin.as_ref(),
|
||||
PartType::Message(message) => message.raw_message.as_ref(),
|
||||
PartType::Message(message) => message.raw_message(),
|
||||
PartType::Multipart(_) => b"",
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the body part's contents as a `str`
|
||||
pub fn text_contents(&'x self) -> Option<&'x str> {
|
||||
pub fn text_contents(&self) -> Option<&str> {
|
||||
match &self.body {
|
||||
PartType::Text(text) | PartType::Html(text) => text.as_ref().into(),
|
||||
PartType::Binary(bin) | PartType::InlineBinary(bin) => {
|
||||
std::str::from_utf8(bin.as_ref()).ok()
|
||||
}
|
||||
PartType::Message(message) => std::str::from_utf8(message.raw_message.as_ref()).ok(),
|
||||
PartType::Message(message) => std::str::from_utf8(message.raw_message()).ok(),
|
||||
PartType::Multipart(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the nested message
|
||||
pub fn message(&'x self) -> Option<&Message<'x>> {
|
||||
pub fn message(&self) -> Option<&Message<'x>> {
|
||||
if let PartType::Message(message) = &self.body {
|
||||
Some(message)
|
||||
} else {
|
||||
@@ -618,7 +633,7 @@ impl<'x> MessagePart<'x> {
|
||||
}
|
||||
|
||||
/// Returns the sub parts ids of a MIME part
|
||||
pub fn sub_parts(&'x self) -> Option<&[MessagePartId]> {
|
||||
pub fn sub_parts(&self) -> Option<&[MessagePartId]> {
|
||||
if let PartType::Multipart(parts) = &self.body {
|
||||
Some(parts.as_ref())
|
||||
} else {
|
||||
@@ -631,7 +646,7 @@ impl<'x> MessagePart<'x> {
|
||||
match &self.body {
|
||||
PartType::Text(text) | PartType::Html(text) => text.len(),
|
||||
PartType::Binary(bin) | PartType::InlineBinary(bin) => bin.len(),
|
||||
PartType::Message(message) => message.raw_message.len(),
|
||||
PartType::Message(message) => message.raw_message().len(),
|
||||
PartType::Multipart(_) => 0,
|
||||
}
|
||||
}
|
||||
@@ -667,7 +682,7 @@ impl<'x> MessagePart<'x> {
|
||||
}
|
||||
|
||||
/// Get the message headers
|
||||
pub fn headers(&self) -> &[Header] {
|
||||
pub fn headers(&self) -> &[Header<'x>] {
|
||||
&self.headers
|
||||
}
|
||||
|
||||
@@ -712,8 +727,8 @@ impl<'x> MessagePart<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> fmt::Display for MessagePart<'x> {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
impl fmt::Display for MessagePart<'_> {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt.write_str(self.text_contents().unwrap_or("[no contents]"))
|
||||
}
|
||||
}
|
||||
@@ -725,7 +740,7 @@ impl<'x> MimeHeaders<'x> for MessagePart<'x> {
|
||||
.and_then(|header| header.as_text())
|
||||
}
|
||||
|
||||
fn content_disposition(&self) -> Option<&ContentType> {
|
||||
fn content_disposition(&self) -> Option<&ContentType<'x>> {
|
||||
self.headers
|
||||
.header_value(&HeaderName::ContentDisposition)
|
||||
.and_then(|header| header.as_content_type())
|
||||
@@ -743,13 +758,13 @@ impl<'x> MimeHeaders<'x> for MessagePart<'x> {
|
||||
.and_then(|header| header.as_text())
|
||||
}
|
||||
|
||||
fn content_type(&self) -> Option<&ContentType> {
|
||||
fn content_type(&self) -> Option<&ContentType<'x>> {
|
||||
self.headers
|
||||
.header_value(&HeaderName::ContentType)
|
||||
.and_then(|header| header.as_content_type())
|
||||
}
|
||||
|
||||
fn content_language(&self) -> &HeaderValue {
|
||||
fn content_language(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.header_value(&HeaderName::ContentLanguage)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
@@ -786,7 +801,7 @@ impl<'x> ContentType<'x> {
|
||||
}
|
||||
|
||||
/// Removes an attribute by name
|
||||
pub fn remove_attribute(&mut self, name: &str) -> Option<Cow<str>> {
|
||||
pub fn remove_attribute(&mut self, name: &str) -> Option<Cow<'x, str>> {
|
||||
let attributes = self.attributes.as_mut()?;
|
||||
|
||||
attributes
|
||||
@@ -796,7 +811,7 @@ impl<'x> ContentType<'x> {
|
||||
}
|
||||
|
||||
/// Returns all attributes
|
||||
pub fn attributes(&self) -> Option<&[(Cow<str>, Cow<str>)]> {
|
||||
pub fn attributes(&self) -> Option<&[(Cow<'x, str>, Cow<'x, str>)]> {
|
||||
self.attributes.as_deref()
|
||||
}
|
||||
|
||||
@@ -804,7 +819,7 @@ impl<'x> ContentType<'x> {
|
||||
pub fn has_attribute(&self, name: &str) -> bool {
|
||||
self.attributes
|
||||
.as_ref()
|
||||
.map_or_else(|| false, |attr| attr.iter().any(|(key, _)| key == name))
|
||||
.is_some_and(|attr| attr.iter().any(|(key, _)| key == name))
|
||||
}
|
||||
|
||||
/// Returns ```true``` if the Content-Disposition type is "attachment"
|
||||
@@ -840,7 +855,7 @@ impl<'x> Received<'x> {
|
||||
}
|
||||
|
||||
/// Returns the hostname or IP address of the machine that originated the message
|
||||
pub fn from(&self) -> Option<&Host> {
|
||||
pub fn from(&self) -> Option<&Host<'x>> {
|
||||
self.from.as_ref()
|
||||
}
|
||||
|
||||
@@ -855,7 +870,7 @@ impl<'x> Received<'x> {
|
||||
}
|
||||
|
||||
/// Returns the hostname or IP address of the machine that received the message
|
||||
pub fn by(&self) -> Option<&Host> {
|
||||
pub fn by(&self) -> Option<&Host<'x>> {
|
||||
self.by.as_ref()
|
||||
}
|
||||
|
||||
@@ -890,7 +905,7 @@ impl<'x> Received<'x> {
|
||||
}
|
||||
|
||||
/// Returns the EHLO/LHLO/HELO hostname or IP address of the machine that sent the message
|
||||
pub fn helo(&self) -> Option<&Host> {
|
||||
pub fn helo(&self) -> Option<&Host<'x>> {
|
||||
self.helo.as_ref()
|
||||
}
|
||||
|
||||
@@ -911,7 +926,7 @@ impl<'x> Received<'x> {
|
||||
}
|
||||
|
||||
/// A hostname or IP address.
|
||||
impl<'x> Host<'x> {
|
||||
impl Host<'_> {
|
||||
pub fn into_owned(self) -> Host<'static> {
|
||||
match self {
|
||||
Host::Name(name) => Host::Name(name.into_owned().into()),
|
||||
@@ -921,14 +936,14 @@ impl<'x> Host<'x> {
|
||||
}
|
||||
|
||||
impl<'x> GetHeader<'x> for Vec<Header<'x>> {
|
||||
fn header_value(&self, name: &HeaderName) -> Option<&HeaderValue<'x>> {
|
||||
fn header_value(&self, name: &HeaderName<'_>) -> Option<&HeaderValue<'x>> {
|
||||
self.iter()
|
||||
.rev()
|
||||
.find(|header| &header.name == name)
|
||||
.map(|header| &header.value)
|
||||
}
|
||||
|
||||
fn header(&self, name: impl Into<HeaderName<'x>>) -> Option<&Header> {
|
||||
fn header(&self, name: impl Into<HeaderName<'x>>) -> Option<&Header<'x>> {
|
||||
let name = name.into();
|
||||
self.iter().rev().find(|header| header.name == name)
|
||||
}
|
||||
@@ -946,14 +961,14 @@ impl<'x> From<Cow<'x, str>> for HeaderName<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> From<String> for HeaderName<'x> {
|
||||
impl From<String> for HeaderName<'_> {
|
||||
fn from(value: String) -> Self {
|
||||
HeaderName::parse(value).unwrap_or(HeaderName::Other("".into()))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<HeaderName<'_>> for String {
|
||||
fn from(header: HeaderName) -> Self {
|
||||
fn from(header: HeaderName<'_>) -> Self {
|
||||
header.to_string()
|
||||
}
|
||||
}
|
||||
@@ -1007,6 +1022,10 @@ impl From<u8> for HeaderName<'_> {
|
||||
34 => HeaderName::ListPost,
|
||||
35 => HeaderName::ListSubscribe,
|
||||
36 => HeaderName::ListUnsubscribe,
|
||||
38 => HeaderName::ArcAuthenticationResults,
|
||||
39 => HeaderName::ArcMessageSignature,
|
||||
40 => HeaderName::ArcSeal,
|
||||
41 => HeaderName::DkimSignature,
|
||||
_ => HeaderName::Other("".into()),
|
||||
}
|
||||
}
|
||||
|
||||
+43
-48
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::{borrow::Cow, convert::TryInto};
|
||||
@@ -29,12 +24,12 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns a parsed header.
|
||||
pub fn header(&self, header: impl Into<HeaderName<'x>>) -> Option<&HeaderValue> {
|
||||
pub fn header(&self, header: impl Into<HeaderName<'x>>) -> Option<&HeaderValue<'x>> {
|
||||
self.parts[0].headers.header(header).map(|h| &h.value)
|
||||
}
|
||||
|
||||
/// Removed a parsed header and returns its value.
|
||||
pub fn remove_header(&mut self, header: impl Into<HeaderName<'x>>) -> Option<HeaderValue> {
|
||||
pub fn remove_header(&mut self, header: impl Into<HeaderName<'x>>) -> Option<HeaderValue<'x>> {
|
||||
let header = header.into();
|
||||
let headers = &mut self.parts[0].headers;
|
||||
headers
|
||||
@@ -56,7 +51,7 @@ impl<'x> Message<'x> {
|
||||
&self,
|
||||
header: impl Into<HeaderName<'x>>,
|
||||
form: HeaderForm,
|
||||
) -> Vec<HeaderValue> {
|
||||
) -> Vec<HeaderValue<'_>> {
|
||||
let header = header.into();
|
||||
let mut results = Vec::new();
|
||||
for header_ in &self.parts[0].headers {
|
||||
@@ -85,15 +80,15 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns an iterator over the RFC headers of this message.
|
||||
pub fn headers(&self) -> &[Header] {
|
||||
pub fn headers(&self) -> &[Header<'x>] {
|
||||
&self.parts[0].headers
|
||||
}
|
||||
|
||||
/// Returns an iterator over the matching RFC headers of this message.
|
||||
pub fn header_values<'y: 'x>(
|
||||
&'y self,
|
||||
pub fn header_values(
|
||||
&self,
|
||||
name: impl Into<HeaderName<'x>>,
|
||||
) -> impl Iterator<Item = &HeaderValue<'x>> {
|
||||
) -> impl Iterator<Item = &HeaderValue<'x>> + Sync + Send {
|
||||
let name = name.into();
|
||||
self.parts[0].headers.iter().filter_map(move |header| {
|
||||
if header.name == name {
|
||||
@@ -105,7 +100,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns all headers in raw format
|
||||
pub fn headers_raw(&self) -> impl Iterator<Item = (&str, &str)> {
|
||||
pub fn headers_raw(&self) -> impl Iterator<Item = (&str, &str)> + Sync + Send {
|
||||
self.parts[0].headers.iter().filter_map(move |header| {
|
||||
Some((
|
||||
header.name.as_str(),
|
||||
@@ -124,7 +119,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the BCC header field
|
||||
pub fn bcc<'y: 'x>(&'y self) -> Option<&Address<'x>> {
|
||||
pub fn bcc(&self) -> Option<&Address<'x>> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::Bcc)
|
||||
@@ -132,7 +127,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the CC header field
|
||||
pub fn cc<'y: 'x>(&'y self) -> Option<&Address<'x>> {
|
||||
pub fn cc(&self) -> Option<&Address<'x>> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::Cc)
|
||||
@@ -140,7 +135,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns all Comments header fields
|
||||
pub fn comments(&self) -> &HeaderValue {
|
||||
pub fn comments(&self) -> &HeaderValue<'x> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::Comments)
|
||||
@@ -156,7 +151,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the From header field
|
||||
pub fn from<'y: 'x>(&'y self) -> Option<&Address<'x>> {
|
||||
pub fn from(&self) -> Option<&Address<'x>> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::From)
|
||||
@@ -164,7 +159,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns all In-Reply-To header fields
|
||||
pub fn in_reply_to(&self) -> &HeaderValue {
|
||||
pub fn in_reply_to(&self) -> &HeaderValue<'x> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::InReplyTo)
|
||||
@@ -172,7 +167,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns all Keywords header fields
|
||||
pub fn keywords(&self) -> &HeaderValue {
|
||||
pub fn keywords(&self) -> &HeaderValue<'x> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::Keywords)
|
||||
@@ -180,7 +175,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the List-Archive header field
|
||||
pub fn list_archive(&self) -> &HeaderValue {
|
||||
pub fn list_archive(&self) -> &HeaderValue<'x> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ListArchive)
|
||||
@@ -188,7 +183,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the List-Help header field
|
||||
pub fn list_help(&self) -> &HeaderValue {
|
||||
pub fn list_help(&self) -> &HeaderValue<'x> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ListHelp)
|
||||
@@ -196,7 +191,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the List-ID header field
|
||||
pub fn list_id(&self) -> &HeaderValue {
|
||||
pub fn list_id(&self) -> &HeaderValue<'x> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ListId)
|
||||
@@ -204,7 +199,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the List-Owner header field
|
||||
pub fn list_owner(&self) -> &HeaderValue {
|
||||
pub fn list_owner(&self) -> &HeaderValue<'x> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ListOwner)
|
||||
@@ -212,7 +207,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the List-Post header field
|
||||
pub fn list_post(&self) -> &HeaderValue {
|
||||
pub fn list_post(&self) -> &HeaderValue<'x> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ListPost)
|
||||
@@ -220,7 +215,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the List-Subscribe header field
|
||||
pub fn list_subscribe(&self) -> &HeaderValue {
|
||||
pub fn list_subscribe(&self) -> &HeaderValue<'x> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ListSubscribe)
|
||||
@@ -228,7 +223,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the List-Unsubscribe header field
|
||||
pub fn list_unsubscribe(&self) -> &HeaderValue {
|
||||
pub fn list_unsubscribe(&self) -> &HeaderValue<'x> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ListUnsubscribe)
|
||||
@@ -244,7 +239,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the MIME-Version header field
|
||||
pub fn mime_version(&self) -> &HeaderValue {
|
||||
pub fn mime_version(&self) -> &HeaderValue<'x> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::MimeVersion)
|
||||
@@ -252,7 +247,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the first Received header field
|
||||
pub fn received(&self) -> Option<&Received> {
|
||||
pub fn received(&self) -> Option<&Received<'x>> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::Received)
|
||||
@@ -260,7 +255,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns all References header fields
|
||||
pub fn references(&self) -> &HeaderValue {
|
||||
pub fn references(&self) -> &HeaderValue<'x> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::References)
|
||||
@@ -268,7 +263,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the Reply-To header field
|
||||
pub fn reply_to<'y: 'x>(&'y self) -> Option<&Address<'x>> {
|
||||
pub fn reply_to(&self) -> Option<&Address<'x>> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ReplyTo)
|
||||
@@ -276,7 +271,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the Resent-BCC header field
|
||||
pub fn resent_bcc<'y: 'x>(&'y self) -> Option<&Address<'x>> {
|
||||
pub fn resent_bcc(&self) -> Option<&Address<'x>> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ResentBcc)
|
||||
@@ -284,7 +279,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the Resent-CC header field
|
||||
pub fn resent_cc<'y: 'x>(&'y self) -> Option<&Address<'x>> {
|
||||
pub fn resent_cc(&self) -> Option<&Address<'x>> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ResentTo)
|
||||
@@ -292,7 +287,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns all Resent-Date header fields
|
||||
pub fn resent_date(&self) -> &HeaderValue {
|
||||
pub fn resent_date(&self) -> &HeaderValue<'x> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ResentDate)
|
||||
@@ -300,7 +295,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the Resent-From header field
|
||||
pub fn resent_from<'y: 'x>(&'y self) -> Option<&Address<'x>> {
|
||||
pub fn resent_from(&self) -> Option<&Address<'x>> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ResentFrom)
|
||||
@@ -308,7 +303,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns all Resent-Message-ID header fields
|
||||
pub fn resent_message_id(&self) -> &HeaderValue {
|
||||
pub fn resent_message_id(&self) -> &HeaderValue<'x> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ResentMessageId)
|
||||
@@ -316,7 +311,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the Sender header field
|
||||
pub fn resent_sender<'y: 'x>(&'y self) -> Option<&Address<'x>> {
|
||||
pub fn resent_sender(&self) -> Option<&Address<'x>> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ResentSender)
|
||||
@@ -324,7 +319,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the Resent-To header field
|
||||
pub fn resent_to<'y: 'x>(&'y self) -> Option<&Address<'x>> {
|
||||
pub fn resent_to(&self) -> Option<&Address<'x>> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ResentTo)
|
||||
@@ -332,7 +327,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns all Return-Path header fields
|
||||
pub fn return_path(&self) -> &HeaderValue {
|
||||
pub fn return_path(&self) -> &HeaderValue<'x> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::ReturnPath)
|
||||
@@ -353,7 +348,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the Sender header field
|
||||
pub fn sender<'y: 'x>(&'y self) -> Option<&Address<'x>> {
|
||||
pub fn sender(&self) -> Option<&Address<'x>> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::Sender)
|
||||
@@ -375,7 +370,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns the To header field
|
||||
pub fn to<'y: 'x>(&'y self) -> Option<&Address<'x>> {
|
||||
pub fn to(&self) -> Option<&Address<'x>> {
|
||||
self.parts[0]
|
||||
.headers
|
||||
.header_value(&HeaderName::To)
|
||||
@@ -414,17 +409,17 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns a message part by position
|
||||
pub fn part(&self, pos: usize) -> Option<&MessagePart> {
|
||||
pub fn part(&self, pos: usize) -> Option<&MessagePart<'x>> {
|
||||
self.parts.get(pos)
|
||||
}
|
||||
|
||||
/// Returns an inline HTML body part by position
|
||||
pub fn html_part(&self, pos: usize) -> Option<&MessagePart> {
|
||||
pub fn html_part(&self, pos: usize) -> Option<&MessagePart<'x>> {
|
||||
self.parts.get(*self.html_body.get(pos)?)
|
||||
}
|
||||
|
||||
/// Returns an inline text body part by position
|
||||
pub fn text_part(&self, pos: usize) -> Option<&MessagePart> {
|
||||
pub fn text_part(&self, pos: usize) -> Option<&MessagePart<'x>> {
|
||||
self.parts.get(*self.text_body.get(pos)?)
|
||||
}
|
||||
|
||||
@@ -449,17 +444,17 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns an Interator over the text body parts
|
||||
pub fn text_bodies(&'x self) -> BodyPartIterator<'x> {
|
||||
pub fn text_bodies(&self) -> impl Iterator<Item = &'_ MessagePart<'_>> + Sync + Send {
|
||||
BodyPartIterator::new(self, &self.text_body)
|
||||
}
|
||||
|
||||
/// Returns an Interator over the HTML body parts
|
||||
pub fn html_bodies(&'x self) -> BodyPartIterator<'x> {
|
||||
pub fn html_bodies(&self) -> impl Iterator<Item = &'_ MessagePart<'_>> + Sync + Send {
|
||||
BodyPartIterator::new(self, &self.html_body)
|
||||
}
|
||||
|
||||
/// Returns an Interator over the attachments
|
||||
pub fn attachments(&'x self) -> AttachmentIterator<'x> {
|
||||
pub fn attachments(&self) -> impl Iterator<Item = &'_ MessagePart<'_>> + Sync + Send {
|
||||
AttachmentIterator::new(self)
|
||||
}
|
||||
|
||||
|
||||
+2
-7
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
pub mod address;
|
||||
|
||||
+92
-163
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::borrow::Cow;
|
||||
@@ -29,13 +24,6 @@ pub fn base64_decode_stream<'x>(
|
||||
let mut buf = Vec::with_capacity(stream_len / 4 * 3);
|
||||
|
||||
for &ch in stream {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
let val = unsafe {
|
||||
*BASE64_MAP
|
||||
.get_unchecked(byte_count as usize)
|
||||
.get_unchecked(ch as usize)
|
||||
};
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
let val = BASE64_MAP[byte_count as usize][ch as usize];
|
||||
|
||||
if val < 0x01ffffff {
|
||||
@@ -47,11 +35,6 @@ pub fn base64_decode_stream<'x>(
|
||||
chunk |= val;
|
||||
|
||||
if byte_count == 0 {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
buf.extend_from_slice(chunk.to_le_bytes().get_unchecked(0..3));
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
buf.extend_from_slice(&chunk.to_le_bytes()[0..3]);
|
||||
}
|
||||
}
|
||||
@@ -59,20 +42,10 @@ pub fn base64_decode_stream<'x>(
|
||||
match ch {
|
||||
b'=' => match byte_count {
|
||||
1 | 2 => {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
buf.push(*chunk.to_le_bytes().get_unchecked(0));
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
buf.push(chunk.to_le_bytes()[0]);
|
||||
byte_count = 0;
|
||||
}
|
||||
3 => {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
buf.extend_from_slice(chunk.to_le_bytes().get_unchecked(0..2));
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
buf.extend_from_slice(&chunk.to_le_bytes()[0..2]);
|
||||
byte_count = 0;
|
||||
}
|
||||
@@ -103,13 +76,6 @@ impl<'x> MessageStream<'x> {
|
||||
self.checkpoint();
|
||||
|
||||
while let Some(&ch) = self.next() {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
let val = unsafe {
|
||||
*BASE64_MAP
|
||||
.get_unchecked(byte_count as usize)
|
||||
.get_unchecked(ch as usize)
|
||||
};
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
let val = BASE64_MAP[byte_count as usize][ch as usize];
|
||||
|
||||
if val < 0x01ffffff {
|
||||
@@ -121,11 +87,6 @@ impl<'x> MessageStream<'x> {
|
||||
chunk |= val;
|
||||
|
||||
if byte_count == 0 {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
buf.extend_from_slice(chunk.to_le_bytes().get_unchecked(0..3));
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
buf.extend_from_slice(&chunk.to_le_bytes()[0..3]);
|
||||
}
|
||||
}
|
||||
@@ -133,20 +94,10 @@ impl<'x> MessageStream<'x> {
|
||||
match ch {
|
||||
b'=' => match byte_count {
|
||||
1 | 2 => {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
buf.push(*chunk.to_le_bytes().get_unchecked(0));
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
buf.push(chunk.to_le_bytes()[0]);
|
||||
byte_count = 0;
|
||||
}
|
||||
3 => {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
buf.extend_from_slice(chunk.to_le_bytes().get_unchecked(0..2));
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
buf.extend_from_slice(&chunk.to_le_bytes()[0..2]);
|
||||
byte_count = 0;
|
||||
}
|
||||
@@ -215,20 +166,10 @@ impl<'x> MessageStream<'x> {
|
||||
b'=' => {
|
||||
match byte_count {
|
||||
1 | 2 => {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
buf.push(*chunk.to_le_bytes().get_unchecked(0));
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
buf.push(chunk.to_le_bytes()[0]);
|
||||
byte_count = 0;
|
||||
}
|
||||
3 => {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
buf.extend_from_slice(chunk.to_le_bytes().get_unchecked(0..2));
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
buf.extend_from_slice(&chunk.to_le_bytes()[0..2]);
|
||||
byte_count = 0;
|
||||
}
|
||||
@@ -253,13 +194,6 @@ impl<'x> MessageStream<'x> {
|
||||
}
|
||||
b' ' | b'\t' | b'\r' => (),
|
||||
_ => {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
let val = unsafe {
|
||||
*BASE64_MAP
|
||||
.get_unchecked(byte_count as usize)
|
||||
.get_unchecked(ch as usize)
|
||||
};
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
let val = BASE64_MAP[byte_count as usize][ch as usize];
|
||||
|
||||
if val < 0x01ffffff {
|
||||
@@ -271,11 +205,6 @@ impl<'x> MessageStream<'x> {
|
||||
chunk |= val;
|
||||
|
||||
if byte_count == 0 {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
buf.extend_from_slice(chunk.to_le_bytes().get_unchecked(0..3));
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
buf.extend_from_slice(&chunk.to_le_bytes()[0..3]);
|
||||
}
|
||||
}
|
||||
@@ -290,96 +219,6 @@ impl<'x> MessageStream<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::parsers::MessageStream;
|
||||
|
||||
#[test]
|
||||
fn decode_base64() {
|
||||
for (encoded_str, expected_result) in [
|
||||
("VGVzdA==", "Test"),
|
||||
("WWU=", "Ye"),
|
||||
("QQ==", "A"),
|
||||
("cm8=", "ro"),
|
||||
(
|
||||
"QXJlIHlvdSBhIFNoaW1hbm8gb3IgQ2FtcGFnbm9sbyBwZXJzb24/",
|
||||
"Are you a Shimano or Campagnolo person?",
|
||||
),
|
||||
(
|
||||
"PCFET0NUWVBFIGh0bWw+CjxodG1sPgo8Ym9keT4KPC9ib2R5Pgo8L2h0bWw+Cg==",
|
||||
"<!DOCTYPE html>\n<html>\n<body>\n</body>\n</html>\n",
|
||||
),
|
||||
(
|
||||
"PCFET0NUWVBFIGh0bWw+CjxodG1sPg\no8Ym9ke\nT4KPC 9ib2R5Pg\n o8L2h0bWw+Cg==",
|
||||
"<!DOCTYPE html>\n<html>\n<body>\n</body>\n</html>\n",
|
||||
),
|
||||
("w6HDqcOtw7PDug==", "áéíóú"),
|
||||
("====", ""),
|
||||
("w6HDq!cOtw7PDug=", ""),
|
||||
("w6 HD qcOt", "áéí"),
|
||||
("cmáé", ""),
|
||||
("áé", ""),
|
||||
("w\n6\nH\nD\nq\nc\nO\nt\nw\n7\n P\tD u g\n==", "áéíóú"),
|
||||
("w6HDqcOtw7PDug==", "áéíóú"),
|
||||
] {
|
||||
assert_eq!(
|
||||
super::base64_decode(encoded_str.as_bytes()).unwrap_or_default(),
|
||||
expected_result.as_bytes(),
|
||||
"Failed for {encoded_str:?}",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_base64_mime() {
|
||||
for (encoded_str, expected_result) in [
|
||||
("VGVzdA==\r\n--boundary\n", "Test"),
|
||||
(
|
||||
"PCFET0NUWVBFIGh0bWw+CjxodG1sPg\no8Ym9ke\nT4KPC 9ib2R5Pg\n o8L2h0bWw+Cg==\r\n--boundary--\r\n",
|
||||
"<!DOCTYPE html>\n<html>\n<body>\n</body>\n</html>\n",
|
||||
),
|
||||
("w6HDqcOtw7PDug==\r\n--boundary \n", "áéíóú"),
|
||||
("w\n6\nH\nD\nq\nc\nO\nt\nw\n7\n P\tD u g\n==\r\n--boundary\n", "áéíóú"),
|
||||
("w6HDqcOtw7PDug==--boundary", "áéíóú"),
|
||||
(
|
||||
"w6HDqcOtw7PDug==\n--boundary--",
|
||||
"áéíóú",
|
||||
),
|
||||
(
|
||||
"w\n6\nH\nD\nq\nc\nO\nt\nw\n7\n P\tD u g\n==\n--boundary",
|
||||
"áéíóú",
|
||||
),
|
||||
] {
|
||||
let mut s = MessageStream::new(encoded_str.as_bytes());
|
||||
let (_, result) = s.decode_base64_mime(b"boundary");
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
expected_result.as_bytes(),
|
||||
"Failed for {encoded_str:?}",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_base64_word() {
|
||||
for (encoded_str, expected_result) in [
|
||||
("w 6 H D q c O t w 7 P D u g== ?=", "áéíóú"),
|
||||
("w6HDqcOtw7PDug==?=", "áéíóú"),
|
||||
("w6HDqc\n Otw7PDug==?=", "áéíóú"),
|
||||
("w6HDqcOtw7PDug================?=", "áéíóú"),
|
||||
("?=", ""),
|
||||
] {
|
||||
let mut s = MessageStream::new(encoded_str.as_bytes());
|
||||
assert_eq!(
|
||||
s.decode_base64_word().unwrap(),
|
||||
expected_result.as_bytes(),
|
||||
"Failed for {encoded_str:?}",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Table adapted from Nick Galbreath's "High performance base64 encoder / decoder"
|
||||
*
|
||||
@@ -550,3 +389,93 @@ pub static BASE64_MAP: &[&[u32]] = &[
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
],
|
||||
];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::parsers::MessageStream;
|
||||
|
||||
#[test]
|
||||
fn decode_base64() {
|
||||
for (encoded_str, expected_result) in [
|
||||
("VGVzdA==", "Test"),
|
||||
("WWU=", "Ye"),
|
||||
("QQ==", "A"),
|
||||
("cm8=", "ro"),
|
||||
(
|
||||
"QXJlIHlvdSBhIFNoaW1hbm8gb3IgQ2FtcGFnbm9sbyBwZXJzb24/",
|
||||
"Are you a Shimano or Campagnolo person?",
|
||||
),
|
||||
(
|
||||
"PCFET0NUWVBFIGh0bWw+CjxodG1sPgo8Ym9keT4KPC9ib2R5Pgo8L2h0bWw+Cg==",
|
||||
"<!DOCTYPE html>\n<html>\n<body>\n</body>\n</html>\n",
|
||||
),
|
||||
(
|
||||
"PCFET0NUWVBFIGh0bWw+CjxodG1sPg\no8Ym9ke\nT4KPC 9ib2R5Pg\n o8L2h0bWw+Cg==",
|
||||
"<!DOCTYPE html>\n<html>\n<body>\n</body>\n</html>\n",
|
||||
),
|
||||
("w6HDqcOtw7PDug==", "áéíóú"),
|
||||
("====", ""),
|
||||
("w6HDq!cOtw7PDug=", ""),
|
||||
("w6 HD qcOt", "áéí"),
|
||||
("cmáé", ""),
|
||||
("áé", ""),
|
||||
("w\n6\nH\nD\nq\nc\nO\nt\nw\n7\n P\tD u g\n==", "áéíóú"),
|
||||
("w6HDqcOtw7PDug==", "áéíóú"),
|
||||
] {
|
||||
assert_eq!(
|
||||
super::base64_decode(encoded_str.as_bytes()).unwrap_or_default(),
|
||||
expected_result.as_bytes(),
|
||||
"Failed for {encoded_str:?}",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_base64_mime() {
|
||||
for (encoded_str, expected_result) in [
|
||||
("VGVzdA==\r\n--boundary\n", "Test"),
|
||||
(
|
||||
"PCFET0NUWVBFIGh0bWw+CjxodG1sPg\no8Ym9ke\nT4KPC 9ib2R5Pg\n o8L2h0bWw+Cg==\r\n--boundary--\r\n",
|
||||
"<!DOCTYPE html>\n<html>\n<body>\n</body>\n</html>\n",
|
||||
),
|
||||
("w6HDqcOtw7PDug==\r\n--boundary \n", "áéíóú"),
|
||||
("w\n6\nH\nD\nq\nc\nO\nt\nw\n7\n P\tD u g\n==\r\n--boundary\n", "áéíóú"),
|
||||
("w6HDqcOtw7PDug==--boundary", "áéíóú"),
|
||||
(
|
||||
"w6HDqcOtw7PDug==\n--boundary--",
|
||||
"áéíóú",
|
||||
),
|
||||
(
|
||||
"w\n6\nH\nD\nq\nc\nO\nt\nw\n7\n P\tD u g\n==\n--boundary",
|
||||
"áéíóú",
|
||||
),
|
||||
] {
|
||||
let mut s = MessageStream::new(encoded_str.as_bytes());
|
||||
let (_, result) = s.decode_base64_mime(b"boundary");
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
expected_result.as_bytes(),
|
||||
"Failed for {encoded_str:?}",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_base64_word() {
|
||||
for (encoded_str, expected_result) in [
|
||||
("w 6 H D q c O t w 7 P D u g== ?=", "áéíóú"),
|
||||
("w6HDqcOtw7PDug==?=", "áéíóú"),
|
||||
("w6HDqc\n Otw7PDug==?=", "áéíóú"),
|
||||
("w6HDqcOtw7PDug================?=", "áéíóú"),
|
||||
("?=", ""),
|
||||
] {
|
||||
let mut s = MessageStream::new(encoded_str.as_bytes());
|
||||
assert_eq!(
|
||||
s.decode_base64_word().unwrap(),
|
||||
expected_result.as_bytes(),
|
||||
"Failed for {encoded_str:?}",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+168
-1625
File diff suppressed because it is too large
Load Diff
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
pub mod map;
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
#[cfg(feature = "full_encoding")]
|
||||
@@ -14,7 +9,7 @@ use encoding_rs::*;
|
||||
|
||||
#[cfg(feature = "full_encoding")]
|
||||
fn multi_byte_decoder(mut decoder: Decoder, bytes: &[u8]) -> String {
|
||||
let mut result = String::with_capacity(bytes.len() * 3);
|
||||
let mut result = String::with_capacity(bytes.len() * 4);
|
||||
|
||||
if let (CoderResult::OutputFull, _, _) = decoder.decode_to_string(bytes, &mut result, true) {
|
||||
debug_assert!(false, "String full while decoding.")
|
||||
@@ -24,7 +19,8 @@ fn multi_byte_decoder(mut decoder: Decoder, bytes: &[u8]) -> String {
|
||||
result
|
||||
}
|
||||
|
||||
pub fn decoder_shift_jis(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_shift_jis(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(SHIFT_JIS.new_decoder(), bytes)
|
||||
@@ -36,7 +32,8 @@ pub fn decoder_shift_jis(bytes: &[u8]) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_big5(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_big5(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(BIG5.new_decoder(), bytes)
|
||||
@@ -48,7 +45,8 @@ pub fn decoder_big5(bytes: &[u8]) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_euc_jp(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_euc_jp(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(EUC_JP.new_decoder(), bytes)
|
||||
@@ -60,7 +58,8 @@ pub fn decoder_euc_jp(bytes: &[u8]) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_euc_kr(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_euc_kr(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(EUC_KR.new_decoder(), bytes)
|
||||
@@ -72,7 +71,8 @@ pub fn decoder_euc_kr(bytes: &[u8]) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_gb18030(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_gb18030(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(GB18030.new_decoder(), bytes)
|
||||
@@ -84,7 +84,8 @@ pub fn decoder_gb18030(bytes: &[u8]) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_gbk(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_gbk(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(GBK.new_decoder(), bytes)
|
||||
@@ -96,7 +97,8 @@ pub fn decoder_gbk(bytes: &[u8]) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_iso2022_jp(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_iso2022_jp(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(ISO_2022_JP.new_decoder(), bytes)
|
||||
@@ -108,7 +110,8 @@ pub fn decoder_iso2022_jp(bytes: &[u8]) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_windows874(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_windows874(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(WINDOWS_874.new_decoder(), bytes)
|
||||
@@ -120,7 +123,8 @@ pub fn decoder_windows874(bytes: &[u8]) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_ibm866(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_ibm866(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(IBM866.new_decoder(), bytes)
|
||||
|
||||
@@ -1,23 +1,13 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
fn single_byte_decoder(table: &[char], bytes: &[u8]) -> String {
|
||||
let mut result = String::with_capacity(bytes.len() * 2);
|
||||
|
||||
for byte in bytes {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
result.push(*table.get_unchecked(*byte as usize));
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
result.push(table[*byte as usize]);
|
||||
}
|
||||
|
||||
@@ -25,115 +15,143 @@ fn single_byte_decoder(table: &[char], bytes: &[u8]) -> String {
|
||||
result
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_1(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_iso_8859_1(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_1, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_2(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_iso_8859_2(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_2, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_3(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_iso_8859_3(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_3, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_4(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_iso_8859_4(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_4, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_5(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_iso_8859_5(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_5, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_6(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_iso_8859_6(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_6, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_7(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_iso_8859_7(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_7, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_8(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_iso_8859_8(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_8, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_9(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_iso_8859_9(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_9, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_10(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_iso_8859_10(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_10, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_13(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_iso_8859_13(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_13, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_14(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_iso_8859_14(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_14, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_15(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_iso_8859_15(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_15, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_16(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_iso_8859_16(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_16, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1250(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_cp1250(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1250, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1251(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_cp1251(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1251, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1252(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_cp1252(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1252, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1253(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_cp1253(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1253, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1254(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_cp1254(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1254, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1255(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_cp1255(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1255, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1256(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_cp1256(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1256, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1257(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_cp1257(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1257, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1258(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_cp1258(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1258, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_koi8_r(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_koi8_r(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(KOI8_R, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_koi8_u(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_koi8_u(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(KOI8_U, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_macintosh(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_macintosh(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(MACINTOSH, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_ibm_850(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_ibm_850(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(IBM850, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_tis_620(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_tis_620(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(TIS_620, bytes)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::char::{decode_utf16, REPLACEMENT_CHARACTER};
|
||||
@@ -34,7 +29,7 @@ fn add_utf16_bytes(state: &mut Utf7DecoderState, n_bytes: usize) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_utf7(bytes: &[u8]) -> String {
|
||||
pub(crate) fn decoder_utf7(bytes: &[u8]) -> String {
|
||||
let mut result = String::with_capacity(bytes.len());
|
||||
let mut byte_count: u8 = 0;
|
||||
let mut in_b64 = false;
|
||||
@@ -111,16 +106,18 @@ fn decoder_utf16_(bytes: &[u8], fnc: fn([u8; 2]) -> u16) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_utf16_le(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_utf16_le(bytes: &[u8]) -> String {
|
||||
decoder_utf16_(bytes, u16::from_le_bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_utf16_be(bytes: &[u8]) -> String {
|
||||
#[inline(always)]
|
||||
pub(crate) fn decoder_utf16_be(bytes: &[u8]) -> String {
|
||||
decoder_utf16_(bytes, u16::from_be_bytes)
|
||||
}
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub fn decoder_utf16(bytes: &[u8]) -> String {
|
||||
pub(crate) fn decoder_utf16(bytes: &[u8]) -> String {
|
||||
// Read BOM
|
||||
let (bytes, fnc): (&[u8], fn([u8; 2]) -> u16) = match bytes.get(0..2) {
|
||||
Some([0xfe, 0xff]) => (bytes.get(2..).unwrap_or(&[]), u16::from_be_bytes),
|
||||
@@ -131,11 +128,6 @@ pub fn decoder_utf16(bytes: &[u8]) -> String {
|
||||
decoder_utf16_(bytes, fnc)
|
||||
}
|
||||
|
||||
// Not currently used at the moment
|
||||
pub fn decoder_utf8(bytes: &[u8]) -> String {
|
||||
String::from_utf8_lossy(bytes).into_owned()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::decoders::charsets::utf::decoder_utf7;
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use crate::{decoders::charsets::map::charset_decoder, parsers::MessageStream};
|
||||
@@ -20,13 +15,13 @@ enum Rfc2047State {
|
||||
Data,
|
||||
}
|
||||
|
||||
impl<'x> MessageStream<'x> {
|
||||
impl MessageStream<'_> {
|
||||
pub fn decode_rfc2047(&mut self) -> Option<String> {
|
||||
let mut state = Rfc2047State::Init;
|
||||
|
||||
let mut charset_start = 0;
|
||||
let mut charset_end = 0;
|
||||
let mut decode_fnc: Option<DecodeWordFnc> = None;
|
||||
let mut decode_fnc: Option<DecodeWordFnc<'_>> = None;
|
||||
|
||||
while let Some(ch) = self.next() {
|
||||
match state {
|
||||
|
||||
+2
-7
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use super::quoted_printable::HEX_MAP;
|
||||
|
||||
+2142
-806
File diff suppressed because it is too large
Load Diff
+2
-7
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::borrow::Cow;
|
||||
@@ -61,14 +56,7 @@ pub fn quoted_printable_decode(bytes: &[u8]) -> Option<Vec<u8>> {
|
||||
buf.push(ch);
|
||||
}
|
||||
QuotedPrintableState::Eq => {
|
||||
hex1 = {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
*HEX_MAP.get_unchecked(ch as usize)
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
HEX_MAP[ch as usize]
|
||||
};
|
||||
hex1 = HEX_MAP[ch as usize];
|
||||
|
||||
if hex1 != -1 {
|
||||
state = QuotedPrintableState::Hex1;
|
||||
@@ -77,9 +65,6 @@ pub fn quoted_printable_decode(bytes: &[u8]) -> Option<Vec<u8>> {
|
||||
}
|
||||
}
|
||||
QuotedPrintableState::Hex1 => {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
let hex2 = unsafe { *HEX_MAP.get_unchecked(ch as usize) };
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
let hex2 = HEX_MAP[ch as usize];
|
||||
|
||||
state = QuotedPrintableState::None;
|
||||
@@ -99,26 +84,10 @@ pub fn quoted_printable_decode(bytes: &[u8]) -> Option<Vec<u8>> {
|
||||
|
||||
#[inline(always)]
|
||||
pub fn quoted_printable_decode_char(hex1: u8, hex2: u8) -> Option<u8> {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
{
|
||||
let hex1 = unsafe { *HEX_MAP.get_unchecked(hex1 as usize) };
|
||||
let hex2 = unsafe { *HEX_MAP.get_unchecked(hex2 as usize) };
|
||||
if hex1 != -1 && hex2 != -1 {
|
||||
(((hex1 as u8) << 4) | hex2 as u8).into()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
{
|
||||
let hex1 = HEX_MAP[hex1 as usize];
|
||||
let hex2 = HEX_MAP[hex2 as usize];
|
||||
if hex1 != -1 && hex2 != -1 {
|
||||
(((hex1 as u8) << 4) | hex2 as u8).into()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
let hex1 = HEX_MAP[hex1 as usize];
|
||||
let hex2 = HEX_MAP[hex2 as usize];
|
||||
|
||||
(hex1 != -1 && hex2 != -1).then_some(((hex1 as u8) << 4) | hex2 as u8)
|
||||
}
|
||||
|
||||
impl<'x> MessageStream<'x> {
|
||||
@@ -184,14 +153,7 @@ impl<'x> MessageStream<'x> {
|
||||
buf.push(ch);
|
||||
}
|
||||
QuotedPrintableState::Eq => {
|
||||
hex1 = {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
*HEX_MAP.get_unchecked(ch as usize)
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
HEX_MAP[ch as usize]
|
||||
};
|
||||
hex1 = HEX_MAP[ch as usize];
|
||||
if hex1 != -1 {
|
||||
state = QuotedPrintableState::Hex1;
|
||||
} else if !ch.is_ascii_whitespace() {
|
||||
@@ -200,9 +162,6 @@ impl<'x> MessageStream<'x> {
|
||||
}
|
||||
}
|
||||
QuotedPrintableState::Hex1 => {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
let hex2 = unsafe { *HEX_MAP.get_unchecked(ch as usize) };
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
let hex2 = HEX_MAP[ch as usize];
|
||||
|
||||
state = QuotedPrintableState::None;
|
||||
@@ -276,14 +235,7 @@ impl<'x> MessageStream<'x> {
|
||||
buf.push(ch);
|
||||
}
|
||||
QuotedPrintableState::Eq => {
|
||||
hex1 = {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
*HEX_MAP.get_unchecked(ch as usize)
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
HEX_MAP[ch as usize]
|
||||
};
|
||||
hex1 = HEX_MAP[ch as usize];
|
||||
if hex1 != -1 {
|
||||
state = QuotedPrintableState::Hex1;
|
||||
} else {
|
||||
@@ -292,11 +244,7 @@ impl<'x> MessageStream<'x> {
|
||||
}
|
||||
}
|
||||
QuotedPrintableState::Hex1 => {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
let hex2 = unsafe { *HEX_MAP.get_unchecked(ch as usize) };
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
let hex2 = HEX_MAP[ch as usize];
|
||||
|
||||
state = QuotedPrintableState::None;
|
||||
if hex2 != -1 {
|
||||
buf.push(((hex1 as u8) << 4) | hex2 as u8);
|
||||
@@ -312,6 +260,27 @@ impl<'x> MessageStream<'x> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Adapted from Daniel Lemire's source:
|
||||
* https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/blob/master/2019/04/17/hexparse.cpp
|
||||
*
|
||||
*/
|
||||
|
||||
pub static HEX_MAP: &[i8] = &[
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10,
|
||||
11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::parsers::MessageStream;
|
||||
@@ -468,23 +437,3 @@ mod tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Adapted from Daniel Lemire's source:
|
||||
* https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/blob/master/2019/04/17/hexparse.cpp
|
||||
*
|
||||
*/
|
||||
|
||||
pub static HEX_MAP: &[i8] = &[
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10,
|
||||
11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
];
|
||||
|
||||
+21
-263
@@ -1,252 +1,11 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
//! # mail-parser
|
||||
//!
|
||||
//! _mail-parser_ is an **e-mail parsing library** written in Rust that fully conforms to the Internet Message Format standard (_RFC 5322_), the
|
||||
//! Multipurpose Internet Mail Extensions (MIME; _RFC 2045 - 2049_) as well as many other [internet messaging RFCs](#conformed-rfcs).
|
||||
//!
|
||||
//! It also supports decoding messages in [41 different character sets](#supported-character-sets) including obsolete formats such as UTF-7.
|
||||
//! All Unicode (UTF-*) and single-byte character sets are handled internally by the library while support for legacy multi-byte encodings of Chinese
|
||||
//! and Japanese languages such as BIG5 or ISO-2022-JP is provided by the optional dependency [encoding_rs](https://crates.io/crates/encoding_rs).
|
||||
//!
|
||||
//! In general, this library abides by the Postel's law or [Robustness Principle](https://en.wikipedia.org/wiki/Robustness_principle) which
|
||||
//! states that an implementation must be conservative in its sending behavior and liberal in its receiving behavior. This means that
|
||||
//! _mail-parser_ will make a best effort to parse non-conformant e-mail messages as long as these do not deviate too much from the standard.
|
||||
//!
|
||||
//! Unlike other e-mail parsing libraries that return nested representations of the different MIME parts in a message, this library
|
||||
//! conforms to [RFC 8621, Section 4.1.4](https://datatracker.ietf.org/doc/html/rfc8621#section-4.1.4) and provides a more human-friendly
|
||||
//! representation of the message contents consisting of just text body parts, html body parts and attachments. Additionally, conversion to/from
|
||||
//! HTML and plain text inline body parts is done automatically when the _alternative_ version is missing.
|
||||
//!
|
||||
//! Performance and memory safety were two important factors while designing _mail-parser_:
|
||||
//!
|
||||
//! - **Zero-copy**: Practically all strings returned by this library are `Cow<str>` references to the input raw message.
|
||||
//! - **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.
|
||||
//! - Used in production environments worldwide by [Stalwart Mail Server](https://github.com/stalwartlabs/mail-server).
|
||||
//!
|
||||
//! Jump to the [example](#usage-example).
|
||||
//!
|
||||
//! ## Conformed RFCs
|
||||
//!
|
||||
//! - [RFC 822 - Standard for ARPA Internet Text Messages](https://datatracker.ietf.org/doc/html/rfc822)
|
||||
//! - [RFC 5322 - Internet Message Format](https://datatracker.ietf.org/doc/html/rfc5322)
|
||||
//! - [RFC 2045 - Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies](https://datatracker.ietf.org/doc/html/rfc2045)
|
||||
//! - [RFC 2046 - Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types](https://datatracker.ietf.org/doc/html/rfc2046)
|
||||
//! - [RFC 2047 - MIME (Multipurpose Internet Mail Extensions) Part Three: Message Header Extensions for Non-ASCII Text](https://datatracker.ietf.org/doc/html/rfc2047)
|
||||
//! - [RFC 2048 - Multipurpose Internet Mail Extensions (MIME) Part Four: Registration Procedures](https://datatracker.ietf.org/doc/html/rfc2048)
|
||||
//! - [RFC 2049 - Multipurpose Internet Mail Extensions (MIME) Part Five: Conformance Criteria and Examples](https://datatracker.ietf.org/doc/html/rfc2049)
|
||||
//! - [RFC 2231 - MIME Parameter Value and Encoded Word Extensions: Character Sets, Languages, and Continuations](https://datatracker.ietf.org/doc/html/rfc2231)
|
||||
//! - [RFC 2557 - MIME Encapsulation of Aggregate Documents, such as HTML (MHTML)](https://datatracker.ietf.org/doc/html/rfc2557)
|
||||
//! - [RFC 2183 - Communicating Presentation Information in Internet Messages: The Content-Disposition Header Field](https://datatracker.ietf.org/doc/html/rfc2183)
|
||||
//! - [RFC 2392 - Content-ID and Message-ID Uniform Resource Locators](https://datatracker.ietf.org/doc/html/rfc2392)
|
||||
//! - [RFC 3282 - Content Language Headers](https://datatracker.ietf.org/doc/html/rfc3282)
|
||||
//! - [RFC 6532 - Internationalized Email Headers](https://datatracker.ietf.org/doc/html/rfc6532)
|
||||
//! - [RFC 2152 - UTF-7 - A Mail-Safe Transformation Format of Unicode](https://datatracker.ietf.org/doc/html/rfc2152)
|
||||
//! - [RFC 2369 - The Use of URLs as Meta-Syntax for Core Mail List Commands and their Transport through Message Header Fields](https://datatracker.ietf.org/doc/html/rfc2369)
|
||||
//! - [RFC 2919 - List-Id: A Structured Field and Namespace for the Identification of Mailing Lists](https://datatracker.ietf.org/doc/html/rfc2919)
|
||||
//! - [RFC 3339 - Date and Time on the Internet: Timestamps](https://datatracker.ietf.org/doc/html/rfc3339)
|
||||
//! - [RFC 8621 - The JSON Meta Application Protocol (JMAP) for Mail (Section 4.1.4)](https://datatracker.ietf.org/doc/html/rfc8621#section-4.1.4)
|
||||
//! - [RFC 5957 - Internet Message Access Protocol - SORT and THREAD Extensions (Section 2.1)](https://datatracker.ietf.org/doc/html/rfc5256#section-2.1)
|
||||
//!
|
||||
//! ## Supported Character Sets
|
||||
//!
|
||||
//! - UTF-8
|
||||
//! - UTF-16, UTF-16BE, UTF-16LE
|
||||
//! - UTF-7
|
||||
//! - US-ASCII
|
||||
//! - ISO-8859-1
|
||||
//! - ISO-8859-2
|
||||
//! - ISO-8859-3
|
||||
//! - ISO-8859-4
|
||||
//! - ISO-8859-5
|
||||
//! - ISO-8859-6
|
||||
//! - ISO-8859-7
|
||||
//! - ISO-8859-8
|
||||
//! - ISO-8859-9
|
||||
//! - ISO-8859-10
|
||||
//! - ISO-8859-13
|
||||
//! - ISO-8859-14
|
||||
//! - ISO-8859-15
|
||||
//! - ISO-8859-16
|
||||
//! - CP1250
|
||||
//! - CP1251
|
||||
//! - CP1252
|
||||
//! - CP1253
|
||||
//! - CP1254
|
||||
//! - CP1255
|
||||
//! - CP1256
|
||||
//! - CP1257
|
||||
//! - CP1258
|
||||
//! - KOI8-R
|
||||
//! - KOI8_U
|
||||
//! - MACINTOSH
|
||||
//! - IBM850
|
||||
//! - TIS-620
|
||||
//!
|
||||
//! Supported character sets via the optional dependency [encoding_rs](https://crates.io/crates/encoding_rs):
|
||||
//!
|
||||
//! - SHIFT_JIS
|
||||
//! - BIG5
|
||||
//! - EUC-JP
|
||||
//! - EUC-KR
|
||||
//! - GB18030
|
||||
//! - GBK
|
||||
//! - ISO-2022-JP
|
||||
//! - WINDOWS-874
|
||||
//! - IBM-866
|
||||
//!
|
||||
//! ## Usage Example
|
||||
//!
|
||||
//! ```
|
||||
//! use mail_parser::*;
|
||||
//!
|
||||
//! let input = br#"From: Art Vandelay <art@vandelay.com> (Vandelay Industries)
|
||||
//!To: "Colleagues": "James Smythe" <james@vandelay.com>; Friends:
|
||||
//! jane@example.com, =?UTF-8?Q?John_Sm=C3=AEth?= <john@example.com>;
|
||||
//!Date: Sat, 20 Nov 2021 14:22:01 -0800
|
||||
//!Subject: Why not both importing AND exporting? =?utf-8?b?4pi6?=
|
||||
//!Content-Type: multipart/mixed; boundary="festivus";
|
||||
//!
|
||||
//!--festivus
|
||||
//!Content-Type: text/html; charset="us-ascii"
|
||||
//!Content-Transfer-Encoding: base64
|
||||
//!
|
||||
//!PGh0bWw+PHA+SSB3YXMgdGhpbmtpbmcgYWJvdXQgcXVpdHRpbmcgdGhlICZsZHF1bztle
|
||||
//!HBvcnRpbmcmcmRxdW87IHRvIGZvY3VzIGp1c3Qgb24gdGhlICZsZHF1bztpbXBvcnRpbm
|
||||
//!cmcmRxdW87LDwvcD48cD5idXQgdGhlbiBJIHRob3VnaHQsIHdoeSBub3QgZG8gYm90aD8
|
||||
//!gJiN4MjYzQTs8L3A+PC9odG1sPg==
|
||||
//!--festivus
|
||||
//!Content-Type: message/rfc822
|
||||
//!
|
||||
//!From: "Cosmo Kramer" <kramer@kramerica.com>
|
||||
//!Subject: Exporting my book about coffee tables
|
||||
//!Content-Type: multipart/mixed; boundary="giddyup";
|
||||
//!
|
||||
//!--giddyup
|
||||
//!Content-Type: text/plain; charset="utf-16"
|
||||
//!Content-Transfer-Encoding: quoted-printable
|
||||
//!
|
||||
//!=FF=FE=0C!5=D8"=DD5=D8)=DD5=D8-=DD =005=D8*=DD5=D8"=DD =005=D8"=
|
||||
//!=DD5=D85=DD5=D8-=DD5=D8,=DD5=D8/=DD5=D81=DD =005=D8*=DD5=D86=DD =
|
||||
//!=005=D8=1F=DD5=D8,=DD5=D8,=DD5=D8(=DD =005=D8-=DD5=D8)=DD5=D8"=
|
||||
//!=DD5=D8=1E=DD5=D80=DD5=D8"=DD!=00
|
||||
//!--giddyup
|
||||
//!Content-Type: image/gif; name*1="about "; name*0="Book ";
|
||||
//! name*2*=utf-8''%e2%98%95 tables.gif
|
||||
//!Content-Transfer-Encoding: Base64
|
||||
//!Content-Disposition: attachment
|
||||
//!
|
||||
//!R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
|
||||
//!--giddyup--
|
||||
//!--festivus--
|
||||
//!"#;
|
||||
//!
|
||||
//! let message = MessageParser::default().parse(input).unwrap();
|
||||
//!
|
||||
//! // Parses addresses (including comments), lists and groups
|
||||
//! assert_eq!(
|
||||
//! message.from().unwrap().first().unwrap(),
|
||||
//! &Addr::new(
|
||||
//! "Art Vandelay (Vandelay Industries)".into(),
|
||||
//! "art@vandelay.com"
|
||||
//! )
|
||||
//! );
|
||||
//!
|
||||
//! assert_eq!(
|
||||
//! message.to().unwrap().as_group().unwrap(),
|
||||
//! &[
|
||||
//! Group::new(
|
||||
//! "Colleagues",
|
||||
//! vec![Addr::new("James Smythe".into(), "james@vandelay.com")]
|
||||
//! ),
|
||||
//! Group::new(
|
||||
//! "Friends",
|
||||
//! vec![
|
||||
//! Addr::new(None, "jane@example.com"),
|
||||
//! Addr::new("John Smîth".into(), "john@example.com"),
|
||||
//! ]
|
||||
//! )
|
||||
//! ]
|
||||
//! );
|
||||
//!
|
||||
//! assert_eq!(
|
||||
//! message.date().unwrap().to_rfc3339(),
|
||||
//! "2021-11-20T14:22:01-08:00"
|
||||
//! );
|
||||
//!
|
||||
//! // RFC2047 support for encoded text in message readers
|
||||
//! assert_eq!(
|
||||
//! message.subject().unwrap(),
|
||||
//! "Why not both importing AND exporting? ☺"
|
||||
//! );
|
||||
//!
|
||||
//! // HTML and text body parts are returned conforming to RFC8621, Section 4.1.4
|
||||
//! assert_eq!(
|
||||
//! message.body_html(0).unwrap(),
|
||||
//! concat!(
|
||||
//! "<html><p>I was thinking about quitting the “exporting” to ",
|
||||
//! "focus just on the “importing”,</p><p>but then I thought,",
|
||||
//! " why not do both? ☺</p></html>"
|
||||
//! )
|
||||
//! );
|
||||
//!
|
||||
//! // HTML parts are converted to plain text (and viceversa) when missing
|
||||
//! assert_eq!(
|
||||
//! message.body_text(0).unwrap(),
|
||||
//! concat!(
|
||||
//! "I was thinking about quitting the “exporting” to focus just on the",
|
||||
//! " “importing”,\nbut then I thought, why not do both? ☺\n"
|
||||
//! )
|
||||
//! );
|
||||
//!
|
||||
//! // Supports nested messages as well as multipart/digest
|
||||
//! let nested_message = message
|
||||
//! .attachment(0)
|
||||
//! .unwrap()
|
||||
//! .message()
|
||||
//! .unwrap();
|
||||
//!
|
||||
//! assert_eq!(
|
||||
//! nested_message.subject().unwrap(),
|
||||
//! "Exporting my book about coffee tables"
|
||||
//! );
|
||||
//!
|
||||
//! // Handles UTF-* as well as many legacy encodings
|
||||
//! assert_eq!(
|
||||
//! nested_message.body_text(0).unwrap(),
|
||||
//! "ℌ𝔢𝔩𝔭 𝔪𝔢 𝔢𝔵𝔭𝔬𝔯𝔱 𝔪𝔶 𝔟𝔬𝔬𝔨 𝔭𝔩𝔢𝔞𝔰𝔢!"
|
||||
//! );
|
||||
//! assert_eq!(
|
||||
//! nested_message.body_html(0).unwrap(),
|
||||
//! "<html><body>ℌ𝔢𝔩𝔭 𝔪𝔢 𝔢𝔵𝔭𝔬𝔯𝔱 𝔪𝔶 𝔟𝔬𝔬𝔨 𝔭𝔩𝔢𝔞𝔰𝔢!</body></html>"
|
||||
//! );
|
||||
//!
|
||||
//! let nested_attachment = nested_message.attachment(0).unwrap();
|
||||
//!
|
||||
//! assert_eq!(nested_attachment.len(), 42);
|
||||
//!
|
||||
//! // Full RFC2231 support for continuations and character sets
|
||||
//! assert_eq!(
|
||||
//! nested_attachment.attachment_name().unwrap(),
|
||||
//! "Book about ☕ tables.gif"
|
||||
//! );
|
||||
//!
|
||||
//! // Integrates with Serde
|
||||
//! println!("{}", serde_json::to_string_pretty(&message).unwrap());
|
||||
//!```
|
||||
#![doc = include_str!("../README.md")]
|
||||
#![deny(rust_2018_idioms)]
|
||||
#[forbid(unsafe_code)]
|
||||
pub mod core;
|
||||
pub mod decoders;
|
||||
pub mod mailbox;
|
||||
@@ -279,7 +38,6 @@ pub struct Message<'x> {
|
||||
pub attachments: Vec<MessagePartId>,
|
||||
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
#[cfg_attr(feature = "serde_support", serde(borrow))]
|
||||
pub parts: Vec<MessagePart<'x>>,
|
||||
|
||||
#[cfg_attr(feature = "serde_support", serde(skip))]
|
||||
@@ -294,7 +52,6 @@ pub struct MessagePart<'x> {
|
||||
pub headers: Vec<Header<'x>>,
|
||||
pub is_encoding_problem: bool,
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
#[cfg_attr(feature = "serde_support", serde(borrow))]
|
||||
pub body: PartType<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip))]
|
||||
pub encoding: Encoding,
|
||||
@@ -344,11 +101,9 @@ pub enum PartType<'x> {
|
||||
Html(Cow<'x, str>),
|
||||
|
||||
/// Any other part type that is not text.
|
||||
#[cfg_attr(feature = "serde_support", serde(borrow))]
|
||||
Binary(Cow<'x, [u8]>),
|
||||
|
||||
/// Any inline binary data that.
|
||||
#[cfg_attr(feature = "serde_support", serde(borrow))]
|
||||
InlineBinary(Cow<'x, [u8]>),
|
||||
|
||||
/// Nested RFC5322 message.
|
||||
@@ -358,7 +113,7 @@ pub enum PartType<'x> {
|
||||
Multipart(Vec<MessagePartId>),
|
||||
}
|
||||
|
||||
impl<'x> Default for PartType<'x> {
|
||||
impl Default for PartType<'_> {
|
||||
fn default() -> Self {
|
||||
PartType::Multipart(Vec::with_capacity(0))
|
||||
}
|
||||
@@ -405,7 +160,11 @@ pub struct Header<'x> {
|
||||
#[derive(Debug, Clone, PartialOrd, Ord)]
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "serde_support", serde(rename_all = "snake_case"))]
|
||||
#[non_exhaustive]
|
||||
pub enum HeaderName<'x> {
|
||||
ArcAuthenticationResults,
|
||||
ArcMessageSignature,
|
||||
ArcSeal,
|
||||
Subject,
|
||||
From,
|
||||
To,
|
||||
@@ -429,6 +188,7 @@ pub enum HeaderName<'x> {
|
||||
ContentTransferEncoding,
|
||||
ContentType,
|
||||
ContentDisposition,
|
||||
DkimSignature,
|
||||
ResentTo,
|
||||
ResentFrom,
|
||||
ResentBcc,
|
||||
@@ -504,7 +264,7 @@ pub struct ContentType<'x> {
|
||||
}
|
||||
|
||||
/// An RFC5322 datetime.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
pub struct DateTime {
|
||||
pub year: u16,
|
||||
@@ -619,15 +379,15 @@ pub trait MimeHeaders<'x> {
|
||||
/// Returns the Content-Description field
|
||||
fn content_description(&self) -> Option<&str>;
|
||||
/// Returns the Content-Disposition field
|
||||
fn content_disposition(&self) -> Option<&ContentType>;
|
||||
fn content_disposition(&self) -> Option<&ContentType<'_>>;
|
||||
/// Returns the Content-ID field
|
||||
fn content_id(&self) -> Option<&str>;
|
||||
/// Returns the Content-Encoding field
|
||||
fn content_transfer_encoding(&self) -> Option<&str>;
|
||||
/// Returns the Content-Type field
|
||||
fn content_type(&self) -> Option<&ContentType>;
|
||||
fn content_type(&self) -> Option<&ContentType<'_>>;
|
||||
/// Returns the Content-Language field
|
||||
fn content_language(&self) -> &HeaderValue;
|
||||
fn content_language(&self) -> &HeaderValue<'_>;
|
||||
/// Returns the Content-Location field
|
||||
fn content_location(&self) -> Option<&str>;
|
||||
/// Returns the attachment name, if any.
|
||||
@@ -638,30 +398,28 @@ pub trait MimeHeaders<'x> {
|
||||
}
|
||||
// Returns true is the content type matches
|
||||
fn is_content_type(&self, type_: &str, subtype: &str) -> bool {
|
||||
self.content_type().map_or(false, |ct| {
|
||||
self.content_type().is_some_and(|ct| {
|
||||
ct.c_type.eq_ignore_ascii_case(type_)
|
||||
&& ct
|
||||
.c_subtype
|
||||
.as_ref()
|
||||
.map_or(false, |st| st.eq_ignore_ascii_case(subtype))
|
||||
.is_some_and(|st| st.eq_ignore_ascii_case(subtype))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GetHeader<'x> {
|
||||
fn header_value(&self, name: &HeaderName) -> Option<&HeaderValue>;
|
||||
fn header(&self, name: impl Into<HeaderName<'x>>) -> Option<&Header>;
|
||||
fn header_value(&self, name: &HeaderName<'_>) -> Option<&HeaderValue<'x>>;
|
||||
fn header(&self, name: impl Into<HeaderName<'x>>) -> Option<&Header<'x>>;
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct BodyPartIterator<'x> {
|
||||
struct BodyPartIterator<'x> {
|
||||
message: &'x Message<'x>,
|
||||
list: &'x [MessagePartId],
|
||||
pos: isize,
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct AttachmentIterator<'x> {
|
||||
struct AttachmentIterator<'x> {
|
||||
message: &'x Message<'x>,
|
||||
pos: isize,
|
||||
}
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::{
|
||||
@@ -56,7 +51,7 @@ impl FolderIterator<'_> {
|
||||
pub fn new(
|
||||
path: impl Into<PathBuf>,
|
||||
sub_folder_prefix: Option<&str>,
|
||||
) -> io::Result<FolderIterator> {
|
||||
) -> io::Result<FolderIterator<'_>> {
|
||||
let path = path.into();
|
||||
|
||||
Ok(FolderIterator {
|
||||
|
||||
+34
-52
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use crate::DateTime;
|
||||
@@ -14,7 +9,8 @@ use std::io::{BufRead, BufReader, Read};
|
||||
|
||||
/// Parses an Mbox mailbox from a `Read` stream, returning each message as a
|
||||
/// `Vec<u8>`.
|
||||
/// supports >From quoting as defined in the [QMail mbox specification](http://qmail.org/qmail-manual-html/man5/mbox.html).
|
||||
///
|
||||
/// Supports >From quoting as defined in the [QMail mbox specification](http://qmail.org/qmail-manual-html/man5/mbox.html).
|
||||
pub struct MessageIterator<T: Read> {
|
||||
reader: BufReader<T>,
|
||||
message: Option<Message>,
|
||||
@@ -28,9 +24,6 @@ pub struct Message {
|
||||
contents: Vec<u8>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ParseError {}
|
||||
|
||||
impl<T> MessageIterator<T>
|
||||
where
|
||||
T: Read,
|
||||
@@ -47,61 +40,50 @@ impl<T> Iterator for MessageIterator<T>
|
||||
where
|
||||
T: Read,
|
||||
{
|
||||
type Item = Result<Message, ParseError>;
|
||||
type Item = std::io::Result<Message>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let mut message_line = Vec::with_capacity(80);
|
||||
|
||||
loop {
|
||||
match self.reader.read_until(b'\n', &mut message_line) {
|
||||
Ok(bytes_read) => {
|
||||
if bytes_read == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
return Some(Err(ParseError {}));
|
||||
}
|
||||
Ok(0) => return self.message.take().map(Ok),
|
||||
Ok(_) => {}
|
||||
Err(e) => return Some(Err(e)),
|
||||
}
|
||||
|
||||
let is_from = message_line
|
||||
.get(..5)
|
||||
.map(|line| line == b"From ")
|
||||
.unwrap_or(false);
|
||||
let is_from = message_line.starts_with(b"From ");
|
||||
|
||||
if let Some(message) = &mut self.message {
|
||||
if !is_from {
|
||||
if message_line[0] != b'>' {
|
||||
message.contents.append(&mut message_line);
|
||||
} else if message_line
|
||||
.iter()
|
||||
.skip_while(|&&ch| ch == b'>')
|
||||
.take(5)
|
||||
.copied()
|
||||
.collect::<Vec<u8>>()
|
||||
== b"From "
|
||||
{
|
||||
message.contents.extend_from_slice(&message_line[1..]);
|
||||
message_line.clear();
|
||||
} else {
|
||||
message.contents.append(&mut message_line);
|
||||
}
|
||||
} else {
|
||||
let message = self.message.take().map(Ok);
|
||||
self.message =
|
||||
Message::new(std::str::from_utf8(&message_line).unwrap_or("")).into();
|
||||
if is_from {
|
||||
let message = self.message.take().map(Ok);
|
||||
self.message =
|
||||
Message::new(std::str::from_utf8(&message_line).unwrap_or("")).into();
|
||||
if message.is_some() {
|
||||
return message;
|
||||
}
|
||||
} else {
|
||||
if is_from {
|
||||
self.message =
|
||||
Message::new(std::str::from_utf8(&message_line).unwrap_or("")).into();
|
||||
}
|
||||
message_line.clear();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
self.message.take().map(Ok)
|
||||
if let Some(message) = &mut self.message {
|
||||
if message_line[0] != b'>' {
|
||||
message.contents.extend_from_slice(&message_line);
|
||||
message_line.clear();
|
||||
continue;
|
||||
}
|
||||
// can become split_once once slice_split_once becomes stable
|
||||
let i = message_line
|
||||
.iter()
|
||||
.position(|&ch| ch != b'>')
|
||||
.unwrap_or(message_line.len());
|
||||
if message_line[i..].starts_with(b"From ") {
|
||||
message.contents.extend_from_slice(&message_line[1..]);
|
||||
} else {
|
||||
message.contents.extend_from_slice(&message_line);
|
||||
}
|
||||
}
|
||||
message_line.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::borrow::Cow;
|
||||
@@ -247,6 +242,7 @@ impl<'x> MessageStream<'x> {
|
||||
continue;
|
||||
}
|
||||
b'<' if parser.state == AddressState::Name => {
|
||||
parser.is_token_email = false;
|
||||
parser.add_token(self, false);
|
||||
parser.state_stack.push(AddressState::Name);
|
||||
parser.state = AddressState::Address;
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::borrow::Cow;
|
||||
@@ -223,7 +218,7 @@ impl<'x> ContentTypeParser<'x> {
|
||||
self.reset_parser();
|
||||
}
|
||||
|
||||
fn add_attr_position(&mut self, stream: &MessageStream) -> bool {
|
||||
fn add_attr_position(&mut self, stream: &MessageStream<'_>) -> bool {
|
||||
if self.token_start > 0 {
|
||||
self.attr_position =
|
||||
String::from_utf8_lossy(&stream.data[self.token_start - 1..self.token_end])
|
||||
@@ -318,6 +313,7 @@ impl<'x> MessageStream<'x> {
|
||||
}
|
||||
ContentState::AttributeQuotedValue => {
|
||||
if next_is_space {
|
||||
self.next();
|
||||
parser.remove_crlf = true;
|
||||
continue;
|
||||
} else {
|
||||
@@ -328,6 +324,9 @@ impl<'x> MessageStream<'x> {
|
||||
}
|
||||
|
||||
if next_is_space {
|
||||
if parser.state == ContentState::Type {
|
||||
continue;
|
||||
}
|
||||
parser.state = ContentState::AttributeName;
|
||||
self.next();
|
||||
|
||||
|
||||
+42
-20
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::fmt;
|
||||
@@ -267,7 +262,7 @@ impl DateTime {
|
||||
|
||||
impl PartialOrd for DateTime {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
self.cmp(other).into()
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -282,7 +277,7 @@ impl Ord for DateTime {
|
||||
}
|
||||
|
||||
impl fmt::Display for DateTime {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt.write_str(&self.to_rfc3339())
|
||||
}
|
||||
}
|
||||
@@ -338,13 +333,9 @@ impl<'x> MessageStream<'x> {
|
||||
parts_sizes[pos] -= 1;
|
||||
parts[pos] += (*ch - b'0') as u32 * u32::pow(10, parts_sizes[pos]);
|
||||
|
||||
if ignore {
|
||||
ignore = false;
|
||||
}
|
||||
}
|
||||
if is_new_token {
|
||||
is_new_token = false;
|
||||
ignore = false;
|
||||
}
|
||||
is_new_token = false;
|
||||
}
|
||||
b':' => {
|
||||
if !is_new_token && !ignore && (pos == 3 || pos == 4) {
|
||||
@@ -372,9 +363,15 @@ impl<'x> MessageStream<'x> {
|
||||
}
|
||||
month_pos += 1;
|
||||
}
|
||||
if is_new_token {
|
||||
is_new_token = false;
|
||||
if pos == 6 {
|
||||
let mut buf = [*ch, 0, 0];
|
||||
let zone = self.obs_zone(&mut buf);
|
||||
is_plus = !zone.is_negative();
|
||||
parts[pos] = 100 * zone.unsigned_abs();
|
||||
parts_sizes[pos] = 0;
|
||||
next_part = true;
|
||||
}
|
||||
is_new_token = false;
|
||||
}
|
||||
b'(' => {
|
||||
comment_count += 1;
|
||||
@@ -417,7 +414,9 @@ impl<'x> MessageStream<'x> {
|
||||
|
||||
if pos >= 6 {
|
||||
HeaderValue::DateTime(DateTime {
|
||||
year: if (1..=99).contains(&parts[2]) {
|
||||
year: if (0..=49).contains(&parts[2]) {
|
||||
parts[2] + 2000
|
||||
} else if (50..=99).contains(&parts[2]) {
|
||||
parts[2] + 1900
|
||||
} else {
|
||||
parts[2]
|
||||
@@ -431,14 +430,37 @@ impl<'x> MessageStream<'x> {
|
||||
hour: parts[3] as u8,
|
||||
minute: parts[4] as u8,
|
||||
second: parts[5] as u8,
|
||||
tz_hour: (parts[6] / 100) as u8,
|
||||
tz_minute: (parts[6] % 100) as u8,
|
||||
tz_hour: ((parts[6] / 100) % 12) as u8,
|
||||
tz_minute: ((parts[6] % 100) % 60) as u8,
|
||||
tz_before_gmt: !is_plus,
|
||||
})
|
||||
} else {
|
||||
HeaderValue::Empty
|
||||
}
|
||||
}
|
||||
// 4.3 obsolete date and time
|
||||
fn obs_zone(&mut self, buf: &mut [u8; 3]) -> i32 {
|
||||
let mut i = 1;
|
||||
for &b in self.by_ref() {
|
||||
buf[i] = b;
|
||||
i += 1;
|
||||
if i == 3 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
hashify::tiny_map! { buf.as_ref(),
|
||||
"EDT" => -4,
|
||||
"EST" => -5,
|
||||
"CDT" => -5,
|
||||
"CST" => -6,
|
||||
"MDT" => -6,
|
||||
"MST" => -7,
|
||||
"PDT" => -7,
|
||||
"PST" => -8,
|
||||
}
|
||||
.unwrap_or(0)
|
||||
}
|
||||
}
|
||||
|
||||
static MONTH_HASH: &[u8] = &[
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use crate::{parsers::MessageStream, HeaderValue};
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
pub mod address;
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use crate::{parsers::MessageStream, HeaderValue};
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
||||
@@ -365,7 +360,7 @@ impl<'x> MessageStream<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x, 'y> Iterator for Tokenizer<'x, 'y> {
|
||||
impl<'x> Iterator for Tokenizer<'x, '_> {
|
||||
type Item = TokenData<'x>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
@@ -566,22 +561,6 @@ impl<'x, 'y> Iterator for Tokenizer<'x, 'y> {
|
||||
let text = std::str::from_utf8(self.stream.bytes(start_pos..self.stream.offset() - 1))
|
||||
.unwrap_or_default();
|
||||
|
||||
/*println!(
|
||||
"({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, 0x{:x}) => Token::{},",
|
||||
n_alpha,
|
||||
n_digit,
|
||||
n_hex,
|
||||
n_dot,
|
||||
n_at,
|
||||
n_other,
|
||||
n_colon,
|
||||
n_plus,
|
||||
n_minus,
|
||||
n_utf,
|
||||
hash,
|
||||
text
|
||||
);*/
|
||||
|
||||
let token = match (
|
||||
n_alpha, n_digit, n_hex, n_dot, n_at, n_other, n_colon, n_plus, n_minus, n_utf, hash,
|
||||
) {
|
||||
@@ -760,11 +739,6 @@ impl<'x, 'y> Iterator for Tokenizer<'x, 'y> {
|
||||
}
|
||||
};
|
||||
|
||||
/*println!("{:?} => {}", token, text);
|
||||
if let Some(token) = &self.next_token {
|
||||
println!("{:?}", token.token);
|
||||
}*/
|
||||
|
||||
TokenData {
|
||||
text,
|
||||
token,
|
||||
@@ -789,7 +763,7 @@ impl<'x, 'y> Tokenizer<'x, 'y> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> From<Token> for TokenData<'x> {
|
||||
impl From<Token> for TokenData<'_> {
|
||||
fn from(token: Token) -> Self {
|
||||
Self {
|
||||
token,
|
||||
|
||||
@@ -1,20 +1,16 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
static RE_PREFIXES: &[&str] = &[
|
||||
"re", "res", "sv", "antw", "ref", "aw", "απ", "השב", "vá", "r", "rif", "bls", "odp", "ynt",
|
||||
"atb", "رد", "回复", "转发",
|
||||
];
|
||||
fn is_re_prefix(prefix: &str) -> bool {
|
||||
hashify::tiny_set! {prefix.as_bytes(), "re", "res", "sv", "antw", "ref", "aw", "απ", "השב", "vá", "r", "rif", "bls", "odp", "ynt",
|
||||
"atb", "رد", "回复", "转发",}
|
||||
}
|
||||
|
||||
static FWD_PREFIXES: &[&str] = &[
|
||||
fn is_fwd_prefix(prefix: &str) -> bool {
|
||||
hashify::tiny_set! {prefix.as_bytes(),
|
||||
"fwd",
|
||||
"fw",
|
||||
"rv",
|
||||
@@ -37,7 +33,8 @@ static FWD_PREFIXES: &[&str] = &[
|
||||
"إعادة توجيه",
|
||||
"回覆",
|
||||
"轉寄",
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
pub fn thread_name(text: &str) -> &str {
|
||||
let mut token_start = 0;
|
||||
@@ -63,9 +60,7 @@ pub fn thread_name(text: &str) -> &str {
|
||||
token_end = pos;
|
||||
}
|
||||
let prefix = text[token_start..token_end].to_lowercase();
|
||||
if RE_PREFIXES.contains(&prefix.as_ref())
|
||||
|| FWD_PREFIXES.contains(&prefix.as_ref())
|
||||
{
|
||||
if is_re_prefix(prefix.as_ref()) || is_fwd_prefix(prefix.as_ref()) {
|
||||
seen_header = true;
|
||||
} else {
|
||||
break;
|
||||
@@ -98,9 +93,7 @@ pub fn thread_name(text: &str) -> &str {
|
||||
token_end = pos;
|
||||
}
|
||||
let prefix = text[token_start..token_end].to_lowercase();
|
||||
if !RE_PREFIXES.contains(&prefix.as_ref())
|
||||
&& !FWD_PREFIXES.contains(&prefix.as_ref())
|
||||
{
|
||||
if !is_re_prefix(prefix.as_ref()) && !is_fwd_prefix(prefix.as_ref()) {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
@@ -115,10 +108,10 @@ pub fn thread_name(text: &str) -> &str {
|
||||
}
|
||||
|
||||
let prefix = text[token_start..token_end].to_lowercase();
|
||||
if FWD_PREFIXES.contains(&prefix.as_ref()) {
|
||||
if is_fwd_prefix(prefix.as_ref()) {
|
||||
token_found = false;
|
||||
seen_blob_header = true;
|
||||
} else if seen_blob_header && RE_PREFIXES.contains(&prefix.as_ref()) {
|
||||
} else if seen_blob_header && is_re_prefix(prefix.as_ref()) {
|
||||
token_found = false;
|
||||
} else {
|
||||
in_blob_ignore = true;
|
||||
@@ -175,7 +168,7 @@ pub fn trim_trailing_fwd(text: &str) -> &str {
|
||||
if in_parentheses {
|
||||
in_parentheses = false;
|
||||
if fwd_end - pos > 2
|
||||
&& FWD_PREFIXES.contains(&text[pos + 1..fwd_end].to_lowercase().as_ref())
|
||||
&& is_fwd_prefix(text[pos + 1..fwd_end].to_lowercase().as_ref())
|
||||
{
|
||||
text_end = pos;
|
||||
trim_end = true;
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
+65
-290
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::borrow::Cow;
|
||||
@@ -78,7 +73,7 @@ impl<'x> MessageStream<'x> {
|
||||
HeaderName::ContentType | HeaderName::ContentDisposition => {
|
||||
self.parse_content_type()
|
||||
}
|
||||
HeaderName::Other(_) => self.parse_raw(),
|
||||
_ => self.parse_raw(),
|
||||
}
|
||||
} else {
|
||||
(conf
|
||||
@@ -104,8 +99,8 @@ impl<'x> MessageStream<'x> {
|
||||
let mut token_start: usize = 0;
|
||||
let mut token_end: usize = 0;
|
||||
let mut token_len: usize = 0;
|
||||
let mut token_hash: usize = 0;
|
||||
let mut last_ch: u8 = 0;
|
||||
|
||||
let mut header = [0u8; 30];
|
||||
|
||||
while let Some(&ch) = self.next() {
|
||||
match ch {
|
||||
@@ -124,49 +119,25 @@ impl<'x> MessageStream<'x> {
|
||||
token_end = token_start;
|
||||
} else {
|
||||
token_end = self.offset();
|
||||
last_ch = ch;
|
||||
}
|
||||
|
||||
if let 0 | 9 = token_len {
|
||||
token_hash += {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
*HDR_HASH.get_unchecked(ch.to_ascii_lowercase() as usize)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
HDR_HASH[ch.to_ascii_lowercase() as usize]
|
||||
} as usize;
|
||||
if let Some(header) = header.get_mut(token_len) {
|
||||
*header = ch.to_ascii_lowercase();
|
||||
token_len += 1;
|
||||
}
|
||||
token_len += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if token_start != 0 {
|
||||
let field = self.bytes(token_start - 1..token_end);
|
||||
|
||||
if (2..=25).contains(&token_len) {
|
||||
token_hash += token_len + {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
*HDR_HASH.get_unchecked(last_ch.to_ascii_lowercase() as usize)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
HDR_HASH[last_ch.to_ascii_lowercase() as usize]
|
||||
} as usize;
|
||||
|
||||
if (4..=72).contains(&token_hash) {
|
||||
let token_hash = token_hash - 4;
|
||||
|
||||
if field.eq_ignore_ascii_case(HDR_NAMES[token_hash]) {
|
||||
return Some(HDR_MAP[token_hash].clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(HeaderName::Other(String::from_utf8_lossy(field)))
|
||||
header_map(&header[..token_len])
|
||||
.unwrap_or_else(|| {
|
||||
HeaderName::Other(String::from_utf8_lossy(
|
||||
self.bytes(token_start - 1..token_end),
|
||||
))
|
||||
})
|
||||
.into()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -176,58 +147,65 @@ impl<'x> MessageStream<'x> {
|
||||
impl<'x> HeaderName<'x> {
|
||||
/// Parse a header name
|
||||
pub fn parse(data: impl Into<Cow<'x, str>>) -> Option<HeaderName<'x>> {
|
||||
let mut token_hash: usize = 0;
|
||||
let mut last_ch: u8 = 0;
|
||||
let data = data.into();
|
||||
let data_ = data.as_bytes();
|
||||
|
||||
for (pos, &ch) in data_.iter().enumerate() {
|
||||
if ch.is_ascii_alphanumeric() || [b'_', b'-'].contains(&ch) {
|
||||
if let 0 | 9 = pos {
|
||||
token_hash += {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
*HDR_HASH.get_unchecked(ch.to_ascii_lowercase() as usize)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
HDR_HASH[ch.to_ascii_lowercase() as usize]
|
||||
} as usize;
|
||||
}
|
||||
last_ch = ch;
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
if (2..=25).contains(&data.len()) {
|
||||
token_hash += data.len() + {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
*HDR_HASH.get_unchecked(last_ch.to_ascii_lowercase() as usize)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
HDR_HASH[last_ch.to_ascii_lowercase() as usize]
|
||||
} as usize;
|
||||
|
||||
if (4..=72).contains(&token_hash) {
|
||||
let token_hash = token_hash - 4;
|
||||
|
||||
if data_.eq_ignore_ascii_case(HDR_NAMES[token_hash]) {
|
||||
return HDR_MAP[token_hash].clone().into();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !data.is_empty() {
|
||||
HeaderName::Other(data).into()
|
||||
let data_lc = data.to_ascii_lowercase();
|
||||
header_map(data_lc.as_bytes())
|
||||
.unwrap_or(HeaderName::Other(data))
|
||||
.into()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn header_map(name: &[u8]) -> Option<HeaderName<'static>> {
|
||||
hashify::tiny_map! {name,
|
||||
"arc-authentication-results" => HeaderName::ArcAuthenticationResults,
|
||||
"arc-seal" => HeaderName::ArcSeal,
|
||||
"arc-message-signature" => HeaderName::ArcMessageSignature,
|
||||
"bcc" => HeaderName::Bcc,
|
||||
"cc" => HeaderName::Cc,
|
||||
"comments" => HeaderName::Comments,
|
||||
"content-description" => HeaderName::ContentDescription,
|
||||
"content-disposition" => HeaderName::ContentDisposition,
|
||||
"content-id" => HeaderName::ContentId,
|
||||
"content-language" => HeaderName::ContentLanguage,
|
||||
"content-location" => HeaderName::ContentLocation,
|
||||
"content-transfer-encoding" => HeaderName::ContentTransferEncoding,
|
||||
"content-type" => HeaderName::ContentType,
|
||||
"date" => HeaderName::Date,
|
||||
"dkim-signature" => HeaderName::DkimSignature,
|
||||
"from" => HeaderName::From,
|
||||
"in-reply-to" => HeaderName::InReplyTo,
|
||||
"keywords" => HeaderName::Keywords,
|
||||
"list-archive" => HeaderName::ListArchive,
|
||||
"list-help" => HeaderName::ListHelp,
|
||||
"list-id" => HeaderName::ListId,
|
||||
"list-owner" => HeaderName::ListOwner,
|
||||
"list-post" => HeaderName::ListPost,
|
||||
"list-subscribe" => HeaderName::ListSubscribe,
|
||||
"list-unsubscribe" => HeaderName::ListUnsubscribe,
|
||||
"message-id" => HeaderName::MessageId,
|
||||
"mime-version" => HeaderName::MimeVersion,
|
||||
"received" => HeaderName::Received,
|
||||
"references" => HeaderName::References,
|
||||
"reply-to" => HeaderName::ReplyTo,
|
||||
"resent-bcc" => HeaderName::ResentBcc,
|
||||
"resent-cc" => HeaderName::ResentCc,
|
||||
"resent-date" => HeaderName::ResentDate,
|
||||
"resent-from" => HeaderName::ResentFrom,
|
||||
"resent-message-id" => HeaderName::ResentMessageId,
|
||||
"resent-sender" => HeaderName::ResentSender,
|
||||
"resent-to" => HeaderName::ResentTo,
|
||||
"return-path" => HeaderName::ReturnPath,
|
||||
"sender" => HeaderName::Sender,
|
||||
"subject" => HeaderName::Subject,
|
||||
"to" => HeaderName::To,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{parsers::MessageStream, HeaderName};
|
||||
@@ -258,206 +236,3 @@ mod tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
type ParserFnc<'x> = fn(&mut MessageStream<'x>) -> crate::HeaderValue<'x>;
|
||||
|
||||
static HDR_PARSER: &[(bool, ParserFnc)] = &[
|
||||
(false, MessageStream::parse_unstructured), // Subject = 0,
|
||||
(false, MessageStream::parse_address), // From = 1,
|
||||
(false, MessageStream::parse_address), // To = 2,
|
||||
(false, MessageStream::parse_address), // Cc = 3,
|
||||
(false, MessageStream::parse_date), // Date = 4,
|
||||
(false, MessageStream::parse_address), // Bcc = 5,
|
||||
(false, MessageStream::parse_address), // ReplyTo = 6,
|
||||
(false, MessageStream::parse_address), // Sender = 7,
|
||||
(true, MessageStream::parse_unstructured), // Comments = 8,
|
||||
(false, MessageStream::parse_id), // InReplyTo = 9,
|
||||
(true, MessageStream::parse_comma_separared), // Keywords = 10,
|
||||
(true, MessageStream::parse_raw), // Received = 11,
|
||||
(false, MessageStream::parse_id), // MessageId = 12,
|
||||
(true, MessageStream::parse_id), // References = 13, (RFC 5322 recommends One)
|
||||
(false, MessageStream::parse_id), // ReturnPath = 14,
|
||||
(false, MessageStream::parse_raw), // MimeVersion = 15,
|
||||
(false, MessageStream::parse_unstructured), // ContentDescription = 16,
|
||||
(false, MessageStream::parse_id), // ContentId = 17,
|
||||
(false, MessageStream::parse_comma_separared), // ContentLanguage = 18
|
||||
(false, MessageStream::parse_unstructured), // ContentLocation = 19
|
||||
(false, MessageStream::parse_unstructured), // ContentTransferEncoding = 20,
|
||||
(false, MessageStream::parse_content_type), // ContentType = 21,
|
||||
(false, MessageStream::parse_content_type), // ContentDisposition = 22,
|
||||
(true, MessageStream::parse_address), // ResentTo = 23,
|
||||
(true, MessageStream::parse_address), // ResentFrom = 24,
|
||||
(true, MessageStream::parse_address), // ResentBcc = 25,
|
||||
(true, MessageStream::parse_address), // ResentCc = 26,
|
||||
(true, MessageStream::parse_address), // ResentSender = 27,
|
||||
(true, MessageStream::parse_date), // ResentDate = 28,
|
||||
(true, MessageStream::parse_id), // ResentMessageId = 29,
|
||||
(false, MessageStream::parse_address), // ListArchive = 30,
|
||||
(false, MessageStream::parse_address), // ListHelp = 31,
|
||||
(false, MessageStream::parse_address), // ListId = 32,
|
||||
(false, MessageStream::parse_address), // ListOwner = 33,
|
||||
(false, MessageStream::parse_address), // ListPost = 34,
|
||||
(false, MessageStream::parse_address), // ListSubscribe = 35,
|
||||
(false, MessageStream::parse_address), // ListUnsubscribe = 36,
|
||||
(true, MessageStream::parse_raw), // Other = 37,
|
||||
];
|
||||
*/
|
||||
|
||||
static HDR_HASH: &[u8] = &[
|
||||
73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
|
||||
73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
|
||||
73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
|
||||
73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
|
||||
73, 0, 20, 5, 0, 0, 25, 0, 5, 20, 73, 25, 25, 30, 10, 10, 5, 73, 0, 0, 15, 73, 73, 73, 73, 20,
|
||||
73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
|
||||
73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
|
||||
73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
|
||||
73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
|
||||
73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
|
||||
73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
|
||||
];
|
||||
|
||||
static HDR_MAP: &[HeaderName] = &[
|
||||
HeaderName::Date,
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::Sender,
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::Received,
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::References,
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::Cc,
|
||||
HeaderName::Comments,
|
||||
HeaderName::ResentCc,
|
||||
HeaderName::ContentId,
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::ResentMessageId,
|
||||
HeaderName::ReplyTo,
|
||||
HeaderName::ResentTo,
|
||||
HeaderName::ResentBcc,
|
||||
HeaderName::ContentLanguage,
|
||||
HeaderName::Subject,
|
||||
HeaderName::ResentSender,
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::ResentDate,
|
||||
HeaderName::To,
|
||||
HeaderName::Bcc,
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::ContentTransferEncoding,
|
||||
HeaderName::ReturnPath,
|
||||
HeaderName::ListId,
|
||||
HeaderName::Keywords,
|
||||
HeaderName::ContentDescription,
|
||||
HeaderName::ListOwner,
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::ContentType,
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::ListHelp,
|
||||
HeaderName::MessageId,
|
||||
HeaderName::ContentLocation,
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::ListSubscribe,
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::ListPost,
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::ResentFrom,
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::ContentDisposition,
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::InReplyTo,
|
||||
HeaderName::ListArchive,
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::From,
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::ListUnsubscribe,
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::MimeVersion, // Invalid
|
||||
HeaderName::MimeVersion,
|
||||
];
|
||||
|
||||
static HDR_NAMES: &[&[u8]] = &[
|
||||
b"date",
|
||||
b"",
|
||||
b"sender",
|
||||
b"",
|
||||
b"received",
|
||||
b"",
|
||||
b"references",
|
||||
b"",
|
||||
b"cc",
|
||||
b"comments",
|
||||
b"resent-cc",
|
||||
b"content-id",
|
||||
b"",
|
||||
b"resent-message-id",
|
||||
b"reply-to",
|
||||
b"resent-to",
|
||||
b"resent-bcc",
|
||||
b"content-language",
|
||||
b"subject",
|
||||
b"resent-sender",
|
||||
b"",
|
||||
b"",
|
||||
b"resent-date",
|
||||
b"to",
|
||||
b"bcc",
|
||||
b"",
|
||||
b"content-transfer-encoding",
|
||||
b"return-path",
|
||||
b"list-id",
|
||||
b"keywords",
|
||||
b"content-description",
|
||||
b"list-owner",
|
||||
b"",
|
||||
b"content-type",
|
||||
b"",
|
||||
b"list-help",
|
||||
b"message-id",
|
||||
b"content-location",
|
||||
b"",
|
||||
b"",
|
||||
b"list-subscribe",
|
||||
b"",
|
||||
b"",
|
||||
b"",
|
||||
b"",
|
||||
b"list-post",
|
||||
b"",
|
||||
b"resent-from",
|
||||
b"",
|
||||
b"",
|
||||
b"content-disposition",
|
||||
b"",
|
||||
b"in-reply-to",
|
||||
b"list-archive",
|
||||
b"",
|
||||
b"from",
|
||||
b"",
|
||||
b"list-unsubscribe",
|
||||
b"",
|
||||
b"",
|
||||
b"",
|
||||
b"",
|
||||
b"",
|
||||
b"",
|
||||
b"",
|
||||
b"",
|
||||
b"",
|
||||
b"",
|
||||
b"mime-version",
|
||||
];
|
||||
|
||||
+34
-40
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level dir&ectory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::borrow::Cow;
|
||||
@@ -38,7 +33,7 @@ enum MimeType {
|
||||
|
||||
#[inline(always)]
|
||||
fn mime_type(
|
||||
content_type: Option<&ContentType>,
|
||||
content_type: Option<&ContentType<'_>>,
|
||||
parent_content_type: &MimeType,
|
||||
) -> (bool, bool, bool, MimeType) {
|
||||
if let Some(content_type) = content_type {
|
||||
@@ -163,9 +158,7 @@ impl MessageParser {
|
||||
mime_type(content_type, &state.mime_type);
|
||||
|
||||
if is_multipart {
|
||||
if let Some(mime_boundary) =
|
||||
content_type.map_or_else(|| None, |f| f.attribute("boundary"))
|
||||
{
|
||||
if let Some(mime_boundary) = content_type.and_then(|f| f.attribute("boundary")) {
|
||||
if stream.seek_next_part(mime_boundary.as_bytes()) {
|
||||
let part_id = message.parts.len();
|
||||
let new_state = MessageParserState {
|
||||
@@ -201,7 +194,7 @@ impl MessageParser {
|
||||
}
|
||||
}
|
||||
|
||||
let (mut encoding, decode_fnc): (Encoding, DecodeFnc) = match part_headers
|
||||
let (mut encoding, decode_fnc): (Encoding, DecodeFnc<'_>) = match part_headers
|
||||
.header_value(&HeaderName::ContentTransferEncoding)
|
||||
{
|
||||
Some(HeaderValue::Text(encoding)) if encoding.eq_ignore_ascii_case("base64") => {
|
||||
@@ -252,7 +245,9 @@ impl MessageParser {
|
||||
let mut is_encoding_problem = offset_end == usize::MAX;
|
||||
if is_encoding_problem {
|
||||
encoding = Encoding::None;
|
||||
mime_type = MimeType::TextOther;
|
||||
if mime_type != MimeType::TextPlain {
|
||||
mime_type = MimeType::TextOther;
|
||||
}
|
||||
is_inline = false;
|
||||
is_text = true;
|
||||
|
||||
@@ -272,15 +267,21 @@ impl MessageParser {
|
||||
let is_inline = is_inline
|
||||
&& part_headers
|
||||
.header_value(&HeaderName::ContentDisposition)
|
||||
.map_or_else(
|
||||
|| true,
|
||||
|d| !d.as_content_type().map_or(false, |ct| ct.is_attachment()),
|
||||
)
|
||||
.map_or(true, |d| {
|
||||
!d.as_content_type().is_some_and(|ct| ct.is_attachment())
|
||||
})
|
||||
&& (state.parts == 1
|
||||
|| (state.mime_type != MimeType::MultipartRelated
|
||||
|| state.mime_type != MimeType::MultipartRelated
|
||||
&& (mime_type == MimeType::Inline
|
||||
|| content_type
|
||||
.map_or_else(|| true, |c| !c.has_attribute("name")))));
|
||||
|| content_type.map_or(true, |c| !c.has_attribute("name"))));
|
||||
|
||||
// if message consists of single text/plain part, classify as text regardless
|
||||
// of encoding issues: see malformed/018.eml
|
||||
let is_inline = is_inline
|
||||
|| state.parts == 1
|
||||
&& state.mime_type == MimeType::Message
|
||||
&& mime_type == MimeType::TextPlain
|
||||
&& is_encoding_problem;
|
||||
|
||||
let (add_to_html, add_to_text) =
|
||||
if let MimeType::MultipartAlternative = state.mime_type {
|
||||
@@ -306,6 +307,13 @@ impl MessageParser {
|
||||
(false, false)
|
||||
};
|
||||
|
||||
if add_to_html {
|
||||
message.html_body.push(message.parts.len());
|
||||
}
|
||||
if add_to_text {
|
||||
message.text_body.push(message.parts.len());
|
||||
}
|
||||
|
||||
if is_text {
|
||||
let text = match (
|
||||
bytes,
|
||||
@@ -326,17 +334,7 @@ impl MessageParser {
|
||||
|
||||
let is_html = mime_type == MimeType::TextHtml;
|
||||
|
||||
if add_to_html && !is_html {
|
||||
message.html_body.push(message.parts.len());
|
||||
} else if add_to_text && is_html {
|
||||
message.text_body.push(message.parts.len());
|
||||
}
|
||||
|
||||
if add_to_html && is_html {
|
||||
message.html_body.push(message.parts.len());
|
||||
} else if add_to_text && !is_html {
|
||||
message.text_body.push(message.parts.len());
|
||||
} else {
|
||||
if !add_to_html && is_html || !add_to_text && !is_html {
|
||||
message.attachments.push(message.parts.len());
|
||||
}
|
||||
|
||||
@@ -346,13 +344,6 @@ impl MessageParser {
|
||||
PartType::Text(text)
|
||||
}
|
||||
} else {
|
||||
if add_to_html {
|
||||
message.html_body.push(message.parts.len());
|
||||
}
|
||||
if add_to_text {
|
||||
message.text_body.push(message.parts.len());
|
||||
}
|
||||
|
||||
message.attachments.push(message.parts.len());
|
||||
|
||||
if !is_inline {
|
||||
@@ -610,7 +601,7 @@ mod tests {
|
||||
|
||||
for file_name in fs::read_dir(&test_dir).unwrap() {
|
||||
let mut file_name = file_name.unwrap().path();
|
||||
if file_name.extension().map_or(false, |e| e == "eml") {
|
||||
if file_name.extension().is_some_and(|e| e == "eml") {
|
||||
let raw_original = fs::read(&file_name).unwrap();
|
||||
tests_run += 1;
|
||||
|
||||
@@ -622,6 +613,9 @@ mod tests {
|
||||
let message = MessageParser::default().parse(&raw_message).unwrap();
|
||||
let json_message = serde_json::to_string_pretty(&message).unwrap();
|
||||
|
||||
// Test that deserialization also works
|
||||
let _: super::Message<'_> = serde_json::from_str(&json_message).unwrap();
|
||||
|
||||
if json_message.as_bytes() != expected_result {
|
||||
file_name.set_extension("failed");
|
||||
fs::write(&file_name, json_message.as_bytes()).unwrap();
|
||||
@@ -640,7 +634,7 @@ mod tests {
|
||||
let json_message = serde_json::to_string_pretty(&message).unwrap();
|
||||
|
||||
if json_message.as_bytes() != expected_result {
|
||||
file_name.set_extension("crlf.failed");
|
||||
file_name.set_extension("failed");
|
||||
fs::write(&file_name, json_message.as_bytes()).unwrap();
|
||||
panic!(
|
||||
"Test failed, parsed message saved to {}",
|
||||
|
||||
+5
-10
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::borrow::Cow;
|
||||
@@ -134,15 +129,15 @@ impl<'x> MessageStream<'x> {
|
||||
self.checkpoint();
|
||||
|
||||
match (self.next(), self.peek()) {
|
||||
(Some(&a), Some(&&b)) if a == b'\r' && b == b'\n' => {
|
||||
(Some(b'\r'), Some(b'\n')) => {
|
||||
self.next();
|
||||
false
|
||||
}
|
||||
(Some(&a), Some(&&b)) if a == b'-' && b == b'-' => {
|
||||
(Some(b'-'), Some(b'-')) => {
|
||||
self.next();
|
||||
true
|
||||
}
|
||||
(Some(&a), _) if a == b'\n' => false,
|
||||
(Some(b'\n'), _) => false,
|
||||
(Some(&a), _) if a.is_ascii_whitespace() => {
|
||||
self.skip_crlf();
|
||||
false
|
||||
|
||||
+2
-7
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::{iter::Peekable, ops::Range, slice::Iter};
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use mail_parser::*;
|
||||
|
||||
Reference in New Issue
Block a user