mirror of
https://github.com/stalwartlabs/mail-parser.git
synced 2026-11-29 00:01:49 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2805e029ff | ||
|
|
35b4846c7e | ||
|
|
75d9a9e96a | ||
|
|
1e8dfd31cf | ||
|
|
0be5ed4a41 | ||
|
|
62057172d0 | ||
|
|
a963f0caa7 | ||
|
|
e2b33a90f3 | ||
|
|
d157525b1c | ||
|
|
9f1edeed33 | ||
|
|
509fb718f3 | ||
|
|
eab28c0b2d | ||
|
|
2cae61c6d2 |
@@ -1,3 +1,14 @@
|
||||
mail-parser 0.4.7
|
||||
================================
|
||||
- Retrieving message headers in order (#19)
|
||||
- Added `get_raw_headers` and `get_header` methods.
|
||||
- Support for malformed Return-Path fields.
|
||||
- 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,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
+24
-30
@@ -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.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "80df024fbc5ac80f87dfef0d9f5209a252f2a497f7f42944cff24d8253cac065"
|
||||
checksum = "7896dc8abb250ffdda33912550faa54c88ec8b998dec0b2c55ab224921ce11df"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
@@ -59,9 +53,9 @@ checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "1.7.0"
|
||||
version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5"
|
||||
checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"hashbrown",
|
||||
@@ -69,15 +63,15 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "0.4.8"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4"
|
||||
checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.120"
|
||||
version = "0.2.121"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ad5c14e80759d0939d013e6ca49930e59fc53dd8e5009132f76240c179380c09"
|
||||
checksum = "efaa7b300f3b5fe8eb6bf21ce3895e1751d9665086af2d64b42f19701015ff4f"
|
||||
|
||||
[[package]]
|
||||
name = "linked-hash-map"
|
||||
@@ -87,7 +81,7 @@ checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
|
||||
|
||||
[[package]]
|
||||
name = "mail-parser"
|
||||
version = "0.4.5"
|
||||
version = "0.4.7"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"chrono",
|
||||
@@ -118,27 +112,27 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.30"
|
||||
version = "1.0.36"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "edc3358ebc67bc8b7fa0c007f945b0b18226f78437d61bec735a9eb96b61ee70"
|
||||
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.10"
|
||||
version = "1.0.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
|
||||
checksum = "b4af2ec4714533fcdf07e886f17025ace8b997b9ce51204ee69b6da831c3da57"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.5"
|
||||
version = "1.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
|
||||
checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
@@ -162,9 +156,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.68"
|
||||
version = "1.0.79"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0f690853975602e1bfe1ccbf50504d67174e3bcf340f23b5ea9992e0587a52d8"
|
||||
checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
@@ -173,21 +167,21 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "serde_yaml"
|
||||
version = "0.8.21"
|
||||
version = "0.8.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d8c608a35705a5d3cdc9fbe403147647ff34b921f8e833e49306df898f9b20af"
|
||||
checksum = "a4a521f2940385c165a24ee286aa8599633d162077a54bdcae2a6fd5a7bfa7a0"
|
||||
dependencies = [
|
||||
"dtoa",
|
||||
"indexmap",
|
||||
"ryu",
|
||||
"serde",
|
||||
"yaml-rust",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.80"
|
||||
version = "1.0.89"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d010a1623fbd906d51d650a9916aaefc05ffa0e4053ff7fe601167f3e715d194"
|
||||
checksum = "ea297be220d52398dcc07ce15a209fce436d361735ac1db700cab3b6cdfb9f54"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
||||
+1
-1
@@ -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.7"
|
||||
edition = "2018"
|
||||
authors = [ "Stalwart Labs <hello@stalw.art>"]
|
||||
license = "Apache-2.0 OR MIT"
|
||||
|
||||
@@ -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,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,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,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,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,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,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,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,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,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,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,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
@@ -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,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
@@ -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,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
@@ -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,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,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
@@ -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(),
|
||||
|
||||
@@ -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,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,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,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,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,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
@@ -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
@@ -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
@@ -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()));
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -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
@@ -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,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,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
|
||||
|
||||
Reference in New Issue
Block a user