Compare commits

..
17 Commits
Author SHA1 Message Date
Mauro D e604f8fcec get_bytes_to_boundary fix (#21) 2022-06-06 18:19:18 +00:00
Mauro D 907984861e get_bytes_to_boundary fix (#21) 2022-06-06 18:18:39 +00:00
Mauro D 28fe4d3aff get_bytes_to_boundary fix (#21) 2022-06-06 18:18:18 +00:00
Mauro D 0891cb0292 v0.4.7 2022-06-03 06:50:26 +00:00
Mauro D 2805e029ff v0.4.7 2022-06-03 06:40:21 +00:00
Mauro D 35b4846c7e Added get_header function. 2022-06-02 17:33:39 +00:00
Mauro D 75d9a9e96a Added get_raw_headers function. 2022-06-02 17:27:45 +00:00
Mauro D 1e8dfd31cf Handle clients sending malformed Return-Path fields. 2022-06-02 13:13:58 +00:00
Bryan Leong 0be5ed4a41 Add support for ks_c_5601 charsets (#20)
* Add mapping for ks_c_5601-1987 charset

* Add mapping for ks_c_5601-1989 charset
2022-06-02 11:12:00 +02:00
Mauro D 62057172d0 Into implementations for RfcHeader 2022-05-20 12:05:17 +00:00
Mauro D a963f0caa7 DateTime into i64. 2022-05-19 14:56:26 +00:00
Mauro D e2b33a90f3 Zero offset timezones should be positive (#17) 2022-04-08 08:20:43 +00:00
BryanLeong d157525b1c Zero offset timezones should be positive (#17) 2022-04-08 10:19:17 +02:00
Mauro D 9f1edeed33 Updated copyright notice. 2022-04-04 14:55:30 +00:00
Mauro D 509fb718f3 DateTime to_timestamp fix #15. 2022-03-19 18:07:41 +00:00
Alex Wennerberg eab28c0b2d Change date is_valid check to inclusive range (#15)
* Use inclusive range for is_valid check

* Remove year check
2022-03-19 19:03:15 +01:00
Mauro D 2cae61c6d2 Replaced overflowing_add with wrapping_add. 2022-03-15 14:35:57 +00:00
38 changed files with 334 additions and 199 deletions
+16
View File
@@ -1,3 +1,19 @@
mail-parser 0.4.8
================================
- get_bytes_to_boundary fix (#21)
mail-parser 0.4.7
================================
- Retrieving message headers in order (#19)
- Added `get_raw_headers` and `get_header` methods.
- Added `get_return_address` method to obtain the return address from the Return-Path or From headers.
- Support for malformed Return-Path headers.
- Support for ks_c_5601 charsets (#20)
mail-parser 0.4.6
================================
- DateTime is_valid() fix (#15)
mail-parser 0.4.5
================================
- DateTime to UNIX timestamp conversion.
+1 -1
View File
@@ -1,4 +1,4 @@
Copyright (C) 2020-2022, Stalwart Labs, Minter Ltd.
Copyright (C) 2020-2022, Stalwart Labs Ltd.
All rights reserved.
Redistribution and use in source and binary forms, with or without
Generated
+37 -43
View File
@@ -4,9 +4,9 @@ version = 3
[[package]]
name = "autocfg"
version = "1.0.1"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bincode"
@@ -36,17 +36,11 @@ dependencies = [
"winapi",
]
[[package]]
name = "dtoa"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0"
[[package]]
name = "encoding_rs"
version = "0.8.28"
version = "0.8.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80df024fbc5ac80f87dfef0d9f5209a252f2a497f7f42944cff24d8253cac065"
checksum = "9852635589dc9f9ea1b6fe9f05b50ef208c85c834a562f0c6abb1c475736ec2b"
dependencies = [
"cfg-if",
]
@@ -59,9 +53,9 @@ checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "indexmap"
version = "1.7.0"
version = "1.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5"
checksum = "e6012d540c5baa3589337a98ce73408de9b5a25ec9fc2c6fd6be8f0d39e0ca5a"
dependencies = [
"autocfg",
"hashbrown",
@@ -69,15 +63,15 @@ dependencies = [
[[package]]
name = "itoa"
version = "0.4.8"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4"
checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
[[package]]
name = "libc"
version = "0.2.120"
version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad5c14e80759d0939d013e6ca49930e59fc53dd8e5009132f76240c179380c09"
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
[[package]]
name = "linked-hash-map"
@@ -87,7 +81,7 @@ checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
[[package]]
name = "mail-parser"
version = "0.4.5"
version = "0.4.8"
dependencies = [
"bincode",
"chrono",
@@ -99,9 +93,9 @@ dependencies = [
[[package]]
name = "num-integer"
version = "0.1.44"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
@@ -109,51 +103,51 @@ dependencies = [
[[package]]
name = "num-traits"
version = "0.2.14"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
]
[[package]]
name = "proc-macro2"
version = "1.0.30"
version = "1.0.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edc3358ebc67bc8b7fa0c007f945b0b18226f78437d61bec735a9eb96b61ee70"
checksum = "c54b25569025b7fc9651de43004ae593a75ad88543b17178aa5e1b9c4f15f56f"
dependencies = [
"unicode-xid",
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.10"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
version = "1.0.5"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695"
[[package]]
name = "serde"
version = "1.0.136"
version = "1.0.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789"
checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.136"
version = "1.0.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9"
checksum = "1f26faba0c3959972377d3b2d306ee9f71faee9714294e41bb777f83f88578be"
dependencies = [
"proc-macro2",
"quote",
@@ -162,9 +156,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.68"
version = "1.0.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f690853975602e1bfe1ccbf50504d67174e3bcf340f23b5ea9992e0587a52d8"
checksum = "9b7ce2b32a1aed03c558dc61a5cd328f15aff2dbc17daad8fb8af04d2100e15c"
dependencies = [
"itoa",
"ryu",
@@ -173,25 +167,25 @@ dependencies = [
[[package]]
name = "serde_yaml"
version = "0.8.21"
version = "0.8.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8c608a35705a5d3cdc9fbe403147647ff34b921f8e833e49306df898f9b20af"
checksum = "707d15895415db6628332b737c838b88c598522e4dc70647e59b72312924aebc"
dependencies = [
"dtoa",
"indexmap",
"ryu",
"serde",
"yaml-rust",
]
[[package]]
name = "syn"
version = "1.0.80"
version = "1.0.96"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d010a1623fbd906d51d650a9916aaefc05ffa0e4053ff7fe601167f3e715d194"
checksum = "0748dd251e24453cb8717f0354206b91557e4ec8703673a4b30208f2abaf1ebf"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
"unicode-ident",
]
[[package]]
@@ -206,10 +200,10 @@ dependencies = [
]
[[package]]
name = "unicode-xid"
version = "0.2.2"
name = "unicode-ident"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee"
[[package]]
name = "wasi"
+1 -1
View File
@@ -1,7 +1,7 @@
[package]
name = "mail-parser"
description = "Fast and robust e-mail parsing library for Rust"
version = "0.4.5"
version = "0.4.8"
edition = "2018"
authors = [ "Stalwart Labs <hello@stalw.art>"]
license = "Apache-2.0 OR MIT"
+1 -1
View File
@@ -277,7 +277,7 @@ at your option.
## Copyright
Copyright (C) 2020-2022, Stalwart Labs, Minter Ltd.
Copyright (C) 2020-2022, Stalwart Labs Ltd.
See [COPYING] for the license.
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+1 -11
View File
@@ -42,11 +42,10 @@ dependencies = [
[[package]]
name = "mail-parser"
version = "0.4.2"
version = "0.4.7"
dependencies = [
"encoding_rs",
"serde",
"serde_bytes",
]
[[package]]
@@ -90,15 +89,6 @@ dependencies = [
"serde_derive",
]
[[package]]
name = "serde_bytes"
version = "0.11.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16ae07dd2f88a366f15bd0632ba725227018c69a1c8550a927324f8eb8368bb9"
dependencies = [
"serde",
]
[[package]]
name = "serde_derive"
version = "1.0.130"
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+5 -5
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
@@ -550,7 +550,7 @@ static CH_MAP: &[&[u8]; 758] = &[
b"",
b"",
b"",
b"",
b"ks_c_5601-1989",
b"",
b"",
b"",
@@ -660,7 +660,7 @@ static CH_MAP: &[&[u8]; 758] = &[
b"",
b"",
b"",
b"",
b"ks_c_5601-1987",
b"",
b"",
b"",
@@ -1312,7 +1312,7 @@ static FNC_MAP: &[for<'x> fn(&'x [u8]) -> String; 758] = &[
no_op,
no_op,
no_op,
no_op,
decoder_euc_kr,
no_op,
no_op,
no_op,
@@ -1422,7 +1422,7 @@ static FNC_MAP: &[for<'x> fn(&'x [u8]) -> String; 758] = &[
no_op,
no_op,
no_op,
no_op,
decoder_euc_kr,
no_op,
no_op,
no_op,
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+78 -19
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
@@ -497,7 +497,7 @@ impl<'x> Group<'x> {
}
pub type RfcHeaders<'x> = HashMap<RfcHeader, HeaderValue<'x>>;
pub type RawHeaders<'x> = HashMap<HeaderName<'x>, Vec<HeaderOffset>>;
pub type RawHeaders<'x> = Vec<(HeaderName<'x>, HeaderOffset)>;
/// Offset of a message element in the raw message.
#[derive(Debug, PartialEq, Eq, Clone)]
@@ -583,7 +583,7 @@ pub enum RfcHeader {
}
impl RfcHeader {
pub fn as_str(&self) -> &str {
pub fn as_str(&self) -> &'static str {
match self {
RfcHeader::Subject => "Subject",
RfcHeader::From => "From",
@@ -632,36 +632,36 @@ impl Display for RfcHeader {
}
}
/// A parsed header value.
/// Parsed header value.
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
//#[cfg_attr(feature = "serde_support", serde(tag = "type"))]
pub enum HeaderValue<'x> {
/// A single address
/// Single address
Address(Addr<'x>),
/// An address list
/// Address list
AddressList(Vec<Addr<'x>>),
/// A group of addresses
/// Group of addresses
Group(Group<'x>),
/// A list containing two or more address groups
/// List containing two or more address groups
GroupList(Vec<Group<'x>>),
/// A string
/// String
Text(Cow<'x, str>),
/// A list of strings
/// List of strings
TextList(Vec<Cow<'x, str>>),
/// A datetime
/// Datetime
DateTime(DateTime),
/// A Content-Type or Content-Disposition header
/// Content-Type or Content-Disposition header
ContentType(ContentType<'x>),
/// A collection of multiple header fields, for example
/// Collection of multiple header fields, for example
/// Resent-To, References, etc.
Collection(Vec<HeaderValue<'x>>),
Empty,
@@ -794,6 +794,11 @@ pub struct DateTime {
}
impl<'x> Message<'x> {
/// Returns a parsed RFC header.
pub fn get_header(&self, header: &RfcHeader) -> Option<&HeaderValue> {
self.headers_rfc.get(header)
}
/// Returns an iterator over the RFC headers of this message.
pub fn get_headers_rfc(&self) -> impl Iterator<Item = (&RfcHeader, &HeaderValue<'x>)> {
self.headers_rfc.iter()
@@ -988,6 +993,22 @@ impl<'x> Message<'x> {
.unwrap_or(&HeaderValue::Empty)
}
/// Returns the return address from either the Return-Path
/// or From header fields
pub fn get_return_address(&self) -> Option<&str> {
match self.headers_rfc.get(&RfcHeader::ReturnPath) {
Some(HeaderValue::Text(text)) => Some(text.as_ref()),
Some(HeaderValue::TextList(text_list)) => text_list.last().map(|t| t.as_ref()),
_ => match self.headers_rfc.get(&RfcHeader::From) {
Some(HeaderValue::Address(addr)) => addr.address.as_deref(),
Some(HeaderValue::AddressList(addr_list)) => {
addr_list.last().and_then(|a| a.address.as_deref())
}
_ => None,
},
}
}
/// Returns the Sender header field
pub fn get_sender(&self) -> &HeaderValue<'x> {
self.headers_rfc
@@ -1026,15 +1047,35 @@ impl<'x> Message<'x> {
}
}
/// Returns all headers in raw format
pub fn get_raw_headers(&self) -> impl Iterator<Item = (&HeaderName, Cow<str>)> {
self.headers_raw.iter().map(move |(name, offset)| {
(
name,
String::from_utf8_lossy(&self.raw_message[offset.start..offset.end]),
)
})
}
pub fn _get_raw(&'x self, name: HeaderName) -> Option<Vec<Cow<'x, str>>> {
self.headers_raw
.get(&name)?
let headers = self
.headers_raw
.iter()
.map(|offset| {
String::from_utf8_lossy(self.raw_message.get(offset.start..offset.end).unwrap())
.filter_map(|(k, v)| {
if k == &name {
Some(String::from_utf8_lossy(
self.raw_message.get(v.start..v.end).unwrap(),
))
} else {
None
}
})
.collect::<Vec<Cow<str>>>()
.into()
.collect::<Vec<Cow<str>>>();
if !headers.is_empty() {
Some(headers)
} else {
None
}
}
/// Returns in raw format a header field not defined in the conformed RFCs
@@ -1532,3 +1573,21 @@ impl<'x, 'de> Deserialize<'de> for MessageAttachment<'x> {
panic!("Deserializing message attachments is not supported at this time.")
}
}
impl From<DateTime> for i64 {
fn from(value: DateTime) -> Self {
value.to_timestamp()
}
}
impl From<RfcHeader> for String {
fn from(header: RfcHeader) -> Self {
header.to_string()
}
}
impl From<RfcHeader> for Cow<'_, str> {
fn from(header: RfcHeader) -> Self {
Cow::Borrowed(header.as_str())
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+34 -41
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
@@ -24,64 +24,57 @@ impl DateTime {
self.hour,
self.minute,
self.second,
if self.tz_before_gmt { "-" } else { "+" },
if self.tz_before_gmt && (self.tz_hour > 0 || self.tz_minute > 0) {
"-"
} else {
"+"
},
self.tz_hour,
self.tz_minute
)
}
/// Returns true if the date is vald
/// Returns true if the date is valid
pub fn is_valid(&self) -> bool {
(0..23).contains(&self.tz_hour)
&& (0..59).contains(&self.tz_minute)
&& (1970..2500).contains(&self.year)
&& (1..12).contains(&self.month)
&& (1..31).contains(&self.day)
&& (0..23).contains(&self.hour)
&& (0..59).contains(&self.minute)
&& (0..59).contains(&self.second)
(0..=23).contains(&self.tz_hour)
&& (1970..=3000).contains(&self.year)
&& (0..=59).contains(&self.tz_minute)
&& (1..=12).contains(&self.month)
&& (1..=31).contains(&self.day)
&& (0..=23).contains(&self.hour)
&& (0..=59).contains(&self.minute)
&& (0..=59).contains(&self.second)
}
/// Returns the numbers of seconds since 1970-01-01T00:00:00Z (Unix epoch)
/// or None if the date is invalid.
pub fn to_timestamp(&self) -> Option<i64> {
pub fn to_timestamp(&self) -> i64 {
// Ported from https://github.com/protocolbuffers/upb/blob/22182e6e/upb/json_decode.c#L982-L992
if self.is_valid() {
let year_base = 4800; /* Before min year, multiple of 400. */
let m_adj = self.month.overflowing_sub(3).0; /* March-based month. */
let carry = if m_adj > self.month { 1 } else { 0 };
let adjust = if carry > 0 { 12 } else { 0 };
let y_adj = self.year as i64 + year_base - carry;
let month_days = ((m_adj.overflowing_add(adjust).0) * 62719 + 769) / 2048;
let leap_days = y_adj / 4 - y_adj / 100 + y_adj / 400;
((y_adj * 365 + leap_days + month_days as i64 + (self.day as i64 - 1) - 2472632)
* 86400
+ self.hour as i64 * 3600
+ self.minute as i64 * 60
+ self.second as i64
+ ((self.tz_hour as i64 * 3600 + self.tz_minute as i64 * 60)
* if self.tz_before_gmt { 1 } else { -1 }))
.into()
} else {
None
}
let year_base = 4800; /* Before min year, multiple of 400. */
let m_adj = self.month.wrapping_sub(3); /* March-based month. */
let carry = if m_adj > self.month { 1 } else { 0 };
let adjust = if carry > 0 { 12 } else { 0 };
let y_adj = self.year as i64 + year_base - carry;
let month_days = ((m_adj.wrapping_add(adjust)) * 62719 + 769) / 2048;
let leap_days = y_adj / 4 - y_adj / 100 + y_adj / 400;
(y_adj * 365 + leap_days + month_days as i64 + (self.day as i64 - 1) - 2472632) * 86400
+ self.hour as i64 * 3600
+ self.minute as i64 * 60
+ self.second as i64
+ ((self.tz_hour as i64 * 3600 + self.tz_minute as i64 * 60)
* if self.tz_before_gmt { 1 } else { -1 })
}
}
impl PartialOrd for DateTime {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match self.to_timestamp()? - other.to_timestamp()? {
0 => std::cmp::Ordering::Equal,
x if x > 0 => std::cmp::Ordering::Greater,
_ => std::cmp::Ordering::Less,
}
.into()
self.cmp(other).into()
}
}
impl Ord for DateTime {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.to_timestamp().unwrap_or_default() - other.to_timestamp().unwrap_or_default() {
match self.to_timestamp() - other.to_timestamp() {
0 => std::cmp::Ordering::Equal,
x if x > 0 => std::cmp::Ordering::Greater,
_ => std::cmp::Ordering::Less,
@@ -340,11 +333,11 @@ mod tests {
{
assert_eq!(
chrono_datetime.timestamp(),
datetime.to_timestamp().unwrap(),
datetime.to_timestamp(),
"{} -> {} ({}) -> {} ({})",
input.0.escape_debug(),
datetime.to_timestamp().unwrap(),
Utc.timestamp_opt(datetime.to_timestamp().unwrap(), 0)
datetime.to_timestamp(),
Utc.timestamp_opt(datetime.to_timestamp(), 0)
.unwrap()
.to_rfc3339_opts(SecondsFormat::Secs, true),
chrono_datetime.timestamp(),
+31 -4
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
@@ -14,6 +14,8 @@ use crate::{parsers::message::MessageStream, HeaderValue};
pub fn parse_id<'x>(stream: &mut MessageStream<'x>) -> HeaderValue<'x> {
let mut token_start: usize = 0;
let mut token_end: usize = 0;
let mut token_invalid_start: usize = 0; // Handle broken clients
let mut token_invalid_end: usize = 0; // Handle broken clients
let mut is_id_part = false;
let mut ids = Vec::new();
@@ -31,7 +33,15 @@ pub fn parse_id<'x>(stream: &mut MessageStream<'x>) -> HeaderValue<'x> {
_ => {
return match ids.len() {
1 => HeaderValue::Text(ids.pop().unwrap()),
0 => HeaderValue::Empty,
0 => {
if token_invalid_start > 0 {
HeaderValue::Text(String::from_utf8_lossy(
&stream.data[token_invalid_start - 1..token_invalid_end],
))
} else {
HeaderValue::Empty
}
}
_ => HeaderValue::TextList(ids),
};
}
@@ -52,13 +62,18 @@ pub fn parse_id<'x>(stream: &mut MessageStream<'x>) -> HeaderValue<'x> {
}
}
b' ' | b'\t' | b'\r' => continue,
_ => (),
_ => {}
}
if is_id_part {
if token_start == 0 {
token_start = stream.pos;
}
token_end = stream.pos;
} else {
if token_invalid_start == 0 {
token_invalid_start = stream.pos;
}
token_invalid_end = stream.pos;
}
}
@@ -102,6 +117,17 @@ mod tests {
"<1234 @ local(blah) .machine .example>\n",
vec!["1234 @ local(blah) .machine .example"],
),
("<>\n", vec![""]),
// Malformed Ids should be parsed anyway
(
"malformed@id.machine.example\n",
vec!["malformed@id.machine.example"],
),
(
" malformed2@id.machine.example \t \n",
vec!["malformed2@id.machine.example"],
),
(" m \n", vec!["m"]),
];
for input in inputs {
@@ -114,7 +140,8 @@ mod tests {
assert!(input.1.len() == 1, "Failed to parse '{:?}'", input.0);
assert_eq!(id, input.1[0], "Failed to parse '{:?}'", input.0);
}
_ => panic!("Unexpected result"),
HeaderValue::Empty if input.1[0].is_empty() => {}
result => panic!("Unexpected result: {:?}", result),
}
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+51 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
@@ -73,6 +73,7 @@ pub fn parse_and_ignore(stream: &mut MessageStream) {
mod tests {
use crate::parsers::fields::raw::parse_raw;
use crate::parsers::message::MessageStream;
use crate::{HeaderName, Message, RfcHeader};
#[test]
fn parse_raw_text() {
@@ -103,4 +104,53 @@ mod tests {
);
}
}
#[test]
fn ordered_raw_headers() {
let input = br#"From: Art Vandelay <art@vandelay.com>
To: jane@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";
Here's a message body.
"#;
let message = Message::parse(input).unwrap();
let mut iter = message.get_raw_headers();
assert_eq!(
iter.next().unwrap(),
(
&HeaderName::Rfc(RfcHeader::From),
" Art Vandelay <art@vandelay.com>\n".into()
)
);
assert_eq!(
iter.next().unwrap(),
(
&HeaderName::Rfc(RfcHeader::To),
" jane@example.com\n".into()
)
);
assert_eq!(
iter.next().unwrap(),
(
&HeaderName::Rfc(RfcHeader::Date),
" Sat, 20 Nov 2021 14:22:01 -0800\n".into()
)
);
assert_eq!(
iter.next().unwrap(),
(
&HeaderName::Rfc(RfcHeader::Subject),
" Why not both importing AND exporting? =?utf-8?b?4pi6?=\n".into()
)
);
assert_eq!(
iter.next().unwrap(),
(
&HeaderName::Rfc(RfcHeader::ContentType),
" multipart/mixed; boundary=\"festivus\";\n".into()
)
);
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+11 -11
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
@@ -49,13 +49,13 @@ pub fn parse_headers<'x>(
let from_offset = stream.pos;
let value = parser(stream);
headers_raw
.entry(HeaderName::Rfc(name))
.or_insert_with(Vec::new)
.push(HeaderOffset {
headers_raw.push((
HeaderName::Rfc(name),
HeaderOffset {
start: from_offset,
end: stream.pos,
});
},
));
if !value.is_empty() {
match headers_rfc.entry(name) {
@@ -77,13 +77,13 @@ pub fn parse_headers<'x>(
HeaderParserResult::Other(name) => {
let from_offset = stream.pos;
parse_and_ignore(stream);
headers_raw
.entry(HeaderName::Other(name))
.or_insert_with(Vec::new)
.push(HeaderOffset {
headers_raw.push((
HeaderName::Other(name),
HeaderOffset {
start: from_offset,
end: stream.pos,
});
},
));
}
HeaderParserResult::Lf => return true,
HeaderParserResult::Eof => return false,
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+41 -35
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
@@ -25,6 +25,16 @@ use super::{
mime::{get_bytes_to_boundary, seek_crlf_end, seek_next_part, skip_crlf, skip_multipart_end},
};
const MIME_HEADERS: &[RfcHeader] = &[
RfcHeader::ContentType,
RfcHeader::ContentDisposition,
RfcHeader::ContentId,
RfcHeader::ContentLanguage,
RfcHeader::ContentLocation,
RfcHeader::ContentTransferEncoding,
RfcHeader::ContentDescription,
];
#[derive(Debug, PartialEq)]
enum MimeType {
MultipartMixed,
@@ -436,24 +446,22 @@ impl<'x> Message<'x> {
// If there is a single part in the message, move MIME headers
// to the part
if is_message {
for header_name in [
RfcHeader::ContentType,
RfcHeader::ContentDisposition,
RfcHeader::ContentId,
RfcHeader::ContentLanguage,
RfcHeader::ContentLocation,
RfcHeader::ContentTransferEncoding,
RfcHeader::ContentDescription,
] {
if let Some(value) = message.headers_rfc.remove(&header_name) {
text_part.headers_rfc.insert(header_name, value);
for header_name in MIME_HEADERS {
if let Some(value) = message.headers_rfc.remove(header_name) {
text_part.headers_rfc.insert(*header_name, value);
}
if let Some(value) =
message.headers_raw.get(&HeaderName::Rfc(header_name))
{
text_part
.headers_raw
.insert(HeaderName::Rfc(header_name), value.to_vec());
}
for (header_name, offset) in &message.headers_raw {
match header_name {
HeaderName::Rfc(header_name)
if MIME_HEADERS.contains(header_name) =>
{
text_part
.headers_raw
.push((HeaderName::Rfc(*header_name), offset.clone()));
}
_ => (),
}
}
}
@@ -498,24 +506,22 @@ impl<'x> Message<'x> {
// If there is a single part in the message, move MIME headers
// to the part
if is_message {
for header_name in [
RfcHeader::ContentType,
RfcHeader::ContentDisposition,
RfcHeader::ContentId,
RfcHeader::ContentLanguage,
RfcHeader::ContentLocation,
RfcHeader::ContentTransferEncoding,
RfcHeader::ContentDescription,
] {
if let Some(value) = message.headers_rfc.remove(&header_name) {
binary_part.headers_rfc.insert(header_name, value);
for header_name in MIME_HEADERS {
if let Some(value) = message.headers_rfc.remove(header_name) {
binary_part.headers_rfc.insert(*header_name, value);
}
if let Some(value) =
message.headers_raw.get(&HeaderName::Rfc(header_name))
{
binary_part
.headers_raw
.insert(HeaderName::Rfc(header_name), value.to_vec());
}
for (header_name, offset) in &message.headers_raw {
match header_name {
HeaderName::Rfc(header_name)
if MIME_HEADERS.contains(header_name) =>
{
binary_part
.headers_raw
.push((HeaderName::Rfc(*header_name), offset.clone()));
}
_ => (),
}
}
}
+2 -2
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
@@ -64,7 +64,7 @@ pub fn get_bytes_to_boundary<'x>(
if is_boundary_end(stream, read_pos) {
let mut match_pos = read_pos - match_count;
if let Some(b'\r') = stream.data.get(match_pos - 1) {
if let Some(b'\r') = stream.data.get(match_pos.saturating_sub(1)) {
match_pos -= 1;
}
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
* Copyright Stalwart Labs Ltd. See the COPYING
* file at the top-level directory of this distribution.
*
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or