mirror of
https://github.com/stalwartlabs/mail-parser.git
synced 2026-11-28 08:01:24 +00:00
Body structure and raw offsets support.
This commit is contained in:
@@ -1,3 +1,8 @@
|
||||
mail-parser 0.3.1
|
||||
================================
|
||||
- Support for non-standard headers.
|
||||
- Raw message offsets are stored in the message object.
|
||||
- Message body structure is now stored in the message object.
|
||||
|
||||
mail-parser 0.3
|
||||
================================
|
||||
|
||||
Generated
+1
-1
@@ -59,7 +59,7 @@ checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
|
||||
|
||||
[[package]]
|
||||
name = "mail-parser"
|
||||
version = "0.3.0"
|
||||
version = "0.3.1"
|
||||
dependencies = [
|
||||
"encoding_rs",
|
||||
"serde",
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "mail-parser"
|
||||
description = "Fast and robust e-mail parsing library for Rust"
|
||||
version = "0.3.0"
|
||||
version = "0.3.1"
|
||||
edition = "2018"
|
||||
authors = [ "Stalwart Labs <hello@stalw.art>"]
|
||||
license = "Apache-2.0 OR MIT"
|
||||
|
||||
@@ -29,8 +29,8 @@ Performance and memory safety were two important factors while designing _mail-p
|
||||
- **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
|
||||
meticulously [tested with MIRI](#testing-fuzzing--benchmarking).
|
||||
- Thoroughly **battle-tested** with millions of real-world e-mail messages dating from 1995 until today.
|
||||
thoroughly [tested with MIRI](#testing-fuzzing--benchmarking).
|
||||
- **Battle-tested** with millions of real-world e-mail messages dating from 1995 until today.
|
||||
|
||||
## Usage Example
|
||||
|
||||
|
||||
+251
-97
@@ -34,8 +34,8 @@
|
||||
//! - **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
|
||||
//! meticulously [tested with MIRI](#testing-fuzzing--benchmarking).
|
||||
//! - Thoroughly **battle-tested** with millions of real-world e-mail messages dating from 1995 until today.
|
||||
//! thoroughly [tested with MIRI](#testing-fuzzing--benchmarking).
|
||||
//! - **Battle-tested** with millions of real-world e-mail messages dating from 1995 until today.
|
||||
//!
|
||||
//! Jump to the [example](#usage-example).
|
||||
//!
|
||||
@@ -261,7 +261,7 @@
|
||||
pub mod decoders;
|
||||
pub mod parsers;
|
||||
|
||||
use std::{borrow::Cow, collections::HashMap, fmt, slice::Iter};
|
||||
use std::{borrow::Cow, collections::HashMap, fmt};
|
||||
|
||||
#[cfg(feature = "serde_support")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -273,7 +273,7 @@ pub struct Message<'x> {
|
||||
#[cfg_attr(feature = "serde_support", serde(borrow))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "HashMap::is_empty")
|
||||
serde(skip_serializing_if = "Headers::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub headers: Headers<'x>,
|
||||
@@ -283,19 +283,65 @@ pub struct Message<'x> {
|
||||
serde(skip_serializing_if = "Vec::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub html_body: Vec<InlinePart<'x>>,
|
||||
pub html_body: Vec<MessagePartId>,
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Vec::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub text_body: Vec<InlinePart<'x>>,
|
||||
pub text_body: Vec<MessagePartId>,
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Vec::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub attachments: Vec<MessagePart<'x>>,
|
||||
pub attachments: Vec<AttachmentType>,
|
||||
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Vec::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub parts: Vec<MessagePart<'x>>,
|
||||
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub structure: MessageStructure,
|
||||
|
||||
pub offset_header: usize,
|
||||
pub offset_body: usize,
|
||||
pub offset_end: usize,
|
||||
}
|
||||
|
||||
/// Attachment type
|
||||
#[derive(Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
pub enum AttachmentType {
|
||||
Inline(MessagePartId),
|
||||
Attachment(MessagePartId),
|
||||
}
|
||||
|
||||
impl AttachmentType {
|
||||
pub fn unwrap(&self) -> (MessagePartId, bool) {
|
||||
match self {
|
||||
AttachmentType::Inline(id) => (*id, false),
|
||||
AttachmentType::Attachment(id) => (*id, true),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Body structure.
|
||||
#[derive(Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
pub enum MessageStructure {
|
||||
Part(MessagePartId),
|
||||
List(Vec<MessageStructure>),
|
||||
MultiPart((MessagePartId, Vec<MessageStructure>)),
|
||||
}
|
||||
|
||||
impl Default for MessageStructure {
|
||||
fn default() -> Self {
|
||||
MessageStructure::Part(0)
|
||||
}
|
||||
}
|
||||
|
||||
/// A text message part.
|
||||
@@ -326,21 +372,15 @@ pub struct BinaryPart<'x> {
|
||||
pub contents: Cow<'x, [u8]>,
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "serde_support", serde(untagged))]
|
||||
pub enum InlinePart<'x> {
|
||||
Text(TextPart<'x>),
|
||||
InlineBinary(u32),
|
||||
}
|
||||
/// Unique ID representing a MIME part within a message.
|
||||
pub type MessagePartId = usize;
|
||||
|
||||
/// A text, binary or nested e-mail MIME message part.
|
||||
///
|
||||
/// - Text: Any text/* part
|
||||
/// - Binary: Any other part type that is not text, usually attachments.
|
||||
/// - InlineBinary: Same as the Binary variant but an inline part according to RFC 8621, Section 4.1.4
|
||||
/// - Message: A nested RFC5322 message.
|
||||
/// - Binary: Any other part type that is not text.
|
||||
/// - Message: Nested RFC5322 message.
|
||||
/// - MultiPart: Multipart part.
|
||||
///
|
||||
#[derive(Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
@@ -348,16 +388,15 @@ pub enum MessagePart<'x> {
|
||||
/// Any text/* part
|
||||
Text(TextPart<'x>),
|
||||
|
||||
/// Any other part type that is not text, usually attachments.
|
||||
/// Any other part type that is not text.
|
||||
#[cfg_attr(feature = "serde_support", serde(borrow))]
|
||||
Binary(BinaryPart<'x>),
|
||||
|
||||
/// Same as the Binary variant but an inline part according to RFC 8621, Section 4.1.4
|
||||
#[cfg_attr(feature = "serde_support", serde(borrow))]
|
||||
InlineBinary(BinaryPart<'x>),
|
||||
|
||||
/// A nested RFC5322 message.
|
||||
/// Nested RFC5322 message.
|
||||
Message(Message<'x>),
|
||||
|
||||
/// Multipart part
|
||||
Multipart(Headers<'x>),
|
||||
}
|
||||
|
||||
/// An RFC5322 or RFC2369 internet address.
|
||||
@@ -402,52 +441,91 @@ pub struct Group<'x> {
|
||||
pub addresses: Vec<Addr<'x>>,
|
||||
}
|
||||
|
||||
pub type Headers<'x> = HashMap<HeaderName, HeaderValue<'x>>;
|
||||
#[derive(Debug, Default, PartialEq)]
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
pub struct Headers<'x> {
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "HashMap::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub values: HashMap<HeaderName<'x>, HeaderValue<'x>>,
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Vec::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub offsets: Vec<HeaderOffset<'x>>,
|
||||
}
|
||||
|
||||
impl<'x> Headers<'x> {
|
||||
pub fn new() -> Self {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.values.is_empty() && self.offsets.is_empty()
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
self.values.clear();
|
||||
self.offsets.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// Offset of a message element in the raw message.
|
||||
#[derive(Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
pub struct HeaderOffset<'x> {
|
||||
pub name: HeaderName<'x>,
|
||||
pub start: usize,
|
||||
pub end: usize,
|
||||
}
|
||||
|
||||
/// A header field
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "serde_support", serde(rename_all = "snake_case"))]
|
||||
pub enum HeaderName {
|
||||
Subject = 0,
|
||||
From = 1,
|
||||
To = 2,
|
||||
Cc = 3,
|
||||
Date = 4,
|
||||
Bcc = 5,
|
||||
ReplyTo = 6,
|
||||
Sender = 7,
|
||||
Comments = 8,
|
||||
InReplyTo = 9,
|
||||
Keywords = 10,
|
||||
Received = 11,
|
||||
MessageId = 12,
|
||||
References = 13,
|
||||
ReturnPath = 14,
|
||||
MimeVersion = 15,
|
||||
ContentDescription = 16,
|
||||
ContentId = 17,
|
||||
ContentLanguage = 18,
|
||||
ContentLocation = 19,
|
||||
ContentTransferEncoding = 20,
|
||||
ContentType = 21,
|
||||
ContentDisposition = 22,
|
||||
ResentTo = 23,
|
||||
ResentFrom = 24,
|
||||
ResentBcc = 25,
|
||||
ResentCc = 26,
|
||||
ResentSender = 27,
|
||||
ResentDate = 28,
|
||||
ResentMessageId = 29,
|
||||
ListArchive = 30,
|
||||
ListHelp = 31,
|
||||
ListId = 32,
|
||||
ListOwner = 33,
|
||||
ListPost = 34,
|
||||
ListSubscribe = 35,
|
||||
ListUnsubscribe = 36,
|
||||
Unsupported = 37,
|
||||
pub enum HeaderName<'x> {
|
||||
Subject,
|
||||
From,
|
||||
To,
|
||||
Cc,
|
||||
Date,
|
||||
Bcc,
|
||||
ReplyTo,
|
||||
Sender,
|
||||
Comments,
|
||||
InReplyTo,
|
||||
Keywords,
|
||||
Received,
|
||||
MessageId,
|
||||
References,
|
||||
ReturnPath,
|
||||
MimeVersion,
|
||||
ContentDescription,
|
||||
ContentId,
|
||||
ContentLanguage,
|
||||
ContentLocation,
|
||||
ContentTransferEncoding,
|
||||
ContentType,
|
||||
ContentDisposition,
|
||||
ResentTo,
|
||||
ResentFrom,
|
||||
ResentBcc,
|
||||
ResentCc,
|
||||
ResentSender,
|
||||
ResentDate,
|
||||
ResentMessageId,
|
||||
ListArchive,
|
||||
ListHelp,
|
||||
ListId,
|
||||
ListOwner,
|
||||
ListPost,
|
||||
ListSubscribe,
|
||||
ListUnsubscribe,
|
||||
Other(Cow<'x, str>),
|
||||
Invalid,
|
||||
}
|
||||
|
||||
/// A parsed header value.
|
||||
@@ -583,12 +661,13 @@ pub struct DateTime {
|
||||
impl<'x> Message<'x> {
|
||||
/// Returns an iterator over the headers of this message.
|
||||
pub fn get_headers(&self) -> impl Iterator<Item = (&HeaderName, &HeaderValue<'x>)> {
|
||||
self.headers.iter()
|
||||
self.headers.values.iter()
|
||||
}
|
||||
|
||||
/// Returns the BCC header field
|
||||
pub fn get_bcc(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::Bcc)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -596,6 +675,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the CC header field
|
||||
pub fn get_cc(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::Cc)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -603,6 +683,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns all Comments header fields
|
||||
pub fn get_comments(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::Comments)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -610,6 +691,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the Date header field
|
||||
pub fn get_date(&self) -> Option<&DateTime> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::Date)
|
||||
.and_then(|header| header.as_datetime_ref())
|
||||
}
|
||||
@@ -617,6 +699,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the From header field
|
||||
pub fn get_from(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::From)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -624,6 +707,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns all In-Reply-To header fields
|
||||
pub fn get_in_reply_to(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::InReplyTo)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -631,6 +715,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns all Keywords header fields
|
||||
pub fn get_keywords(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::Keywords)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -638,6 +723,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the List-Archive header field
|
||||
pub fn get_list_archive(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ListArchive)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -645,6 +731,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the List-Help header field
|
||||
pub fn get_list_help(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ListHelp)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -652,6 +739,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the List-ID header field
|
||||
pub fn get_list_id(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ListId)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -659,6 +747,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the List-Owner header field
|
||||
pub fn get_list_owner(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ListOwner)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -666,6 +755,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the List-Post header field
|
||||
pub fn get_list_post(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ListPost)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -673,6 +763,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the List-Subscribe header field
|
||||
pub fn get_list_subscribe(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ListSubscribe)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -680,6 +771,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the List-Unsubscribe header field
|
||||
pub fn get_list_unsubscribe(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ListUnsubscribe)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -687,6 +779,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the Message-ID header field
|
||||
pub fn get_message_id(&self) -> Option<&str> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::MessageId)
|
||||
.and_then(|header| header.as_text_ref())
|
||||
}
|
||||
@@ -694,6 +787,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the MIME-Version header field
|
||||
pub fn get_mime_version(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::MimeVersion)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -701,6 +795,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns all Received header fields
|
||||
pub fn get_received(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::Received)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -708,6 +803,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns all References header fields
|
||||
pub fn get_references(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::References)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -715,6 +811,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the Reply-To header field
|
||||
pub fn get_reply_to(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ReplyTo)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -722,6 +819,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the Resent-BCC header field
|
||||
pub fn get_resent_bcc(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ResentBcc)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -729,6 +827,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the Resent-CC header field
|
||||
pub fn get_resent_cc(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ResentTo)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -736,6 +835,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns all Resent-Date header fields
|
||||
pub fn get_resent_date(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ResentDate)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -743,6 +843,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the Resent-From header field
|
||||
pub fn get_resent_from(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ResentFrom)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -750,6 +851,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns all Resent-Message-ID header fields
|
||||
pub fn get_resent_message_id(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ResentMessageId)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -757,6 +859,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the Sender header field
|
||||
pub fn get_resent_sender(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ResentSender)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -764,6 +867,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the Resent-To header field
|
||||
pub fn get_resent_to(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ResentTo)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -771,6 +875,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns all Return-Path header fields
|
||||
pub fn get_return_path(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ReturnPath)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -778,6 +883,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the Sender header field
|
||||
pub fn get_sender(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::Sender)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
@@ -785,6 +891,7 @@ impl<'x> Message<'x> {
|
||||
/// Returns the Subject header field
|
||||
pub fn get_subject(&self) -> Option<&str> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::Subject)
|
||||
.and_then(|header| header.as_text_ref())
|
||||
}
|
||||
@@ -792,35 +899,40 @@ impl<'x> Message<'x> {
|
||||
/// Returns the To header field
|
||||
pub fn get_to(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::To)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
|
||||
fn get_body_part(&self, list: &'x [InlinePart<'x>], pos: usize) -> Option<&'x dyn BodyPart> {
|
||||
match list.get(pos) {
|
||||
Some(InlinePart::Text(v)) => Some(v),
|
||||
Some(InlinePart::InlineBinary(v)) => match self.attachments.get(*v as usize)? {
|
||||
MessagePart::Text(v) => Some(v),
|
||||
MessagePart::Binary(v) | MessagePart::InlineBinary(v) => Some(v),
|
||||
_ => None,
|
||||
},
|
||||
/// Returns a non-standard header field
|
||||
pub fn get_other(&self, name: &'x str) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::Other(name.into()))
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
|
||||
fn get_part(&self, list: &'x [MessagePartId], pos: usize) -> Option<&'x dyn BodyPart> {
|
||||
match self.parts.get(*list.get(pos)?)? {
|
||||
MessagePart::Text(v) => Some(v),
|
||||
MessagePart::Binary(v) => Some(v),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns an inline HTML body part by position
|
||||
pub fn get_html_body(&self, pos: usize) -> Option<&dyn BodyPart> {
|
||||
self.get_body_part(&self.html_body, pos)
|
||||
self.get_part(&self.html_body, pos)
|
||||
}
|
||||
|
||||
/// Returns an inline text body part by position
|
||||
pub fn get_text_body(&self, pos: usize) -> Option<&dyn BodyPart> {
|
||||
self.get_body_part(&self.text_body, pos)
|
||||
self.get_part(&self.text_body, pos)
|
||||
}
|
||||
|
||||
/// Returns an attacment by position
|
||||
pub fn get_attachment(&self, pos: usize) -> Option<&MessagePart<'x>> {
|
||||
self.attachments.get(pos)
|
||||
self.parts.get(self.attachments.get(pos)?.unwrap().0)
|
||||
}
|
||||
|
||||
/// Returns the number of plain text body parts
|
||||
@@ -849,8 +961,8 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
|
||||
/// Returns an Interator over the attachments
|
||||
pub fn get_attachments(&self) -> Iter<MessagePart<'_>> {
|
||||
self.attachments.iter()
|
||||
pub fn get_attachments(&'x self) -> AttachmentIterator<'x> {
|
||||
AttachmentIterator::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -875,42 +987,49 @@ pub trait MimeHeaders<'x> {
|
||||
impl<'x> MimeHeaders<'x> for Message<'x> {
|
||||
fn get_content_description(&self) -> Option<&str> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ContentDescription)
|
||||
.and_then(|header| header.as_text_ref())
|
||||
}
|
||||
|
||||
fn get_content_disposition(&self) -> Option<&ContentType> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ContentDisposition)
|
||||
.and_then(|header| header.as_content_type_ref())
|
||||
}
|
||||
|
||||
fn get_content_id(&self) -> Option<&str> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ContentId)
|
||||
.and_then(|header| header.as_text_ref())
|
||||
}
|
||||
|
||||
fn get_content_transfer_encoding(&self) -> Option<&str> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ContentTransferEncoding)
|
||||
.and_then(|header| header.as_text_ref())
|
||||
}
|
||||
|
||||
fn get_content_type(&self) -> Option<&ContentType> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ContentType)
|
||||
.and_then(|header| header.as_content_type_ref())
|
||||
}
|
||||
|
||||
fn get_content_language(&self) -> &HeaderValue<'x> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ContentLanguage)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
}
|
||||
|
||||
fn get_content_location(&self) -> Option<&str> {
|
||||
self.headers
|
||||
.values
|
||||
.get(&HeaderName::ContentLocation)
|
||||
.and_then(|header| header.as_text_ref())
|
||||
}
|
||||
@@ -998,49 +1117,56 @@ impl<'x> fmt::Display for BinaryPart<'x> {
|
||||
impl<'x> MimeHeaders<'x> for TextPart<'x> {
|
||||
fn get_content_description(&self) -> Option<&str> {
|
||||
self.headers.as_ref().and_then(|h| {
|
||||
h.get(&HeaderName::ContentDescription)
|
||||
h.values
|
||||
.get(&HeaderName::ContentDescription)
|
||||
.and_then(|header| header.as_text_ref())
|
||||
})
|
||||
}
|
||||
|
||||
fn get_content_disposition(&self) -> Option<&ContentType> {
|
||||
self.headers.as_ref().and_then(|h| {
|
||||
h.get(&HeaderName::ContentDisposition)
|
||||
h.values
|
||||
.get(&HeaderName::ContentDisposition)
|
||||
.and_then(|header| header.as_content_type_ref())
|
||||
})
|
||||
}
|
||||
|
||||
fn get_content_id(&self) -> Option<&str> {
|
||||
self.headers.as_ref().and_then(|h| {
|
||||
h.get(&HeaderName::ContentId)
|
||||
h.values
|
||||
.get(&HeaderName::ContentId)
|
||||
.and_then(|header| header.as_text_ref())
|
||||
})
|
||||
}
|
||||
|
||||
fn get_content_transfer_encoding(&self) -> Option<&str> {
|
||||
self.headers.as_ref().and_then(|h| {
|
||||
h.get(&HeaderName::ContentTransferEncoding)
|
||||
h.values
|
||||
.get(&HeaderName::ContentTransferEncoding)
|
||||
.and_then(|header| header.as_text_ref())
|
||||
})
|
||||
}
|
||||
|
||||
fn get_content_type(&self) -> Option<&ContentType> {
|
||||
self.headers.as_ref().and_then(|h| {
|
||||
h.get(&HeaderName::ContentType)
|
||||
h.values
|
||||
.get(&HeaderName::ContentType)
|
||||
.and_then(|header| header.as_content_type_ref())
|
||||
})
|
||||
}
|
||||
|
||||
fn get_content_language(&self) -> &HeaderValue<'x> {
|
||||
self.headers.as_ref().map_or(&HeaderValue::Empty, |h| {
|
||||
h.get(&HeaderName::ContentLanguage)
|
||||
h.values
|
||||
.get(&HeaderName::ContentLanguage)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
})
|
||||
}
|
||||
|
||||
fn get_content_location(&self) -> Option<&str> {
|
||||
self.headers.as_ref().and_then(|h| {
|
||||
h.get(&HeaderName::ContentLocation)
|
||||
h.values
|
||||
.get(&HeaderName::ContentLocation)
|
||||
.and_then(|header| header.as_text_ref())
|
||||
})
|
||||
}
|
||||
@@ -1049,49 +1175,56 @@ impl<'x> MimeHeaders<'x> for TextPart<'x> {
|
||||
impl<'x> MimeHeaders<'x> for BinaryPart<'x> {
|
||||
fn get_content_description(&self) -> Option<&str> {
|
||||
self.headers.as_ref().and_then(|h| {
|
||||
h.get(&HeaderName::ContentDescription)
|
||||
h.values
|
||||
.get(&HeaderName::ContentDescription)
|
||||
.and_then(|header| header.as_text_ref())
|
||||
})
|
||||
}
|
||||
|
||||
fn get_content_disposition(&self) -> Option<&ContentType> {
|
||||
self.headers.as_ref().and_then(|h| {
|
||||
h.get(&HeaderName::ContentDisposition)
|
||||
h.values
|
||||
.get(&HeaderName::ContentDisposition)
|
||||
.and_then(|header| header.as_content_type_ref())
|
||||
})
|
||||
}
|
||||
|
||||
fn get_content_id(&self) -> Option<&str> {
|
||||
self.headers.as_ref().and_then(|h| {
|
||||
h.get(&HeaderName::ContentId)
|
||||
h.values
|
||||
.get(&HeaderName::ContentId)
|
||||
.and_then(|header| header.as_text_ref())
|
||||
})
|
||||
}
|
||||
|
||||
fn get_content_transfer_encoding(&self) -> Option<&str> {
|
||||
self.headers.as_ref().and_then(|h| {
|
||||
h.get(&HeaderName::ContentTransferEncoding)
|
||||
h.values
|
||||
.get(&HeaderName::ContentTransferEncoding)
|
||||
.and_then(|header| header.as_text_ref())
|
||||
})
|
||||
}
|
||||
|
||||
fn get_content_type(&self) -> Option<&ContentType> {
|
||||
self.headers.as_ref().and_then(|h| {
|
||||
h.get(&HeaderName::ContentType)
|
||||
h.values
|
||||
.get(&HeaderName::ContentType)
|
||||
.and_then(|header| header.as_content_type_ref())
|
||||
})
|
||||
}
|
||||
|
||||
fn get_content_language(&self) -> &HeaderValue<'x> {
|
||||
self.headers.as_ref().map_or(&HeaderValue::Empty, |h| {
|
||||
h.get(&HeaderName::ContentLanguage)
|
||||
h.values
|
||||
.get(&HeaderName::ContentLanguage)
|
||||
.unwrap_or(&HeaderValue::Empty)
|
||||
})
|
||||
}
|
||||
|
||||
fn get_content_location(&self) -> Option<&str> {
|
||||
self.headers.as_ref().and_then(|h| {
|
||||
h.get(&HeaderName::ContentLocation)
|
||||
h.values
|
||||
.get(&HeaderName::ContentLocation)
|
||||
.and_then(|header| header.as_text_ref())
|
||||
})
|
||||
}
|
||||
@@ -1100,12 +1233,18 @@ impl<'x> MimeHeaders<'x> for BinaryPart<'x> {
|
||||
#[doc(hidden)]
|
||||
pub struct BodyPartIterator<'x> {
|
||||
message: &'x Message<'x>,
|
||||
list: &'x [InlinePart<'x>],
|
||||
list: &'x [MessagePartId],
|
||||
pos: isize,
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct AttachmentIterator<'x> {
|
||||
message: &'x Message<'x>,
|
||||
pos: isize,
|
||||
}
|
||||
|
||||
impl<'x> BodyPartIterator<'x> {
|
||||
fn new(message: &'x Message<'x>, list: &'x [InlinePart<'x>]) -> BodyPartIterator<'x> {
|
||||
fn new(message: &'x Message<'x>, list: &'x [MessagePartId]) -> BodyPartIterator<'x> {
|
||||
BodyPartIterator {
|
||||
message,
|
||||
list,
|
||||
@@ -1119,7 +1258,22 @@ impl<'x> Iterator for BodyPartIterator<'x> {
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.pos += 1;
|
||||
self.message.get_body_part(self.list, self.pos as usize)
|
||||
self.message.get_part(self.list, self.pos as usize)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> AttachmentIterator<'x> {
|
||||
fn new(message: &'x Message<'x>) -> AttachmentIterator<'x> {
|
||||
AttachmentIterator { message, pos: -1 }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> Iterator for AttachmentIterator<'x> {
|
||||
type Item = &'x MessagePart<'x>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.pos += 1;
|
||||
self.message.get_attachment(self.pos as usize)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+109
-74
@@ -11,25 +11,19 @@
|
||||
|
||||
use std::collections::hash_map::Entry;
|
||||
|
||||
use crate::{HeaderName, HeaderValue, Headers};
|
||||
use crate::{HeaderName, HeaderOffset, HeaderValue, Headers};
|
||||
|
||||
use super::{
|
||||
fields::{
|
||||
address::parse_address,
|
||||
content_type::parse_content_type,
|
||||
date::parse_date,
|
||||
id::parse_id,
|
||||
list::parse_comma_separared,
|
||||
raw::{parse_and_ignore, parse_raw},
|
||||
unstructured::parse_unstructured,
|
||||
address::parse_address, content_type::parse_content_type, date::parse_date, id::parse_id,
|
||||
list::parse_comma_separared, raw::parse_raw, unstructured::parse_unstructured,
|
||||
},
|
||||
message::MessageStream,
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum HeaderParserResult<'x> {
|
||||
Supported(HeaderName),
|
||||
Unsupported(&'x [u8]),
|
||||
Header(HeaderName<'x>),
|
||||
Lf,
|
||||
Eof,
|
||||
}
|
||||
@@ -40,19 +34,26 @@ pub fn parse_headers<'x>(headers: &mut Headers<'x>, stream: &mut MessageStream<'
|
||||
stream.pos += bytes_read;
|
||||
|
||||
match result {
|
||||
HeaderParserResult::Supported(name) => {
|
||||
let (is_many, parser) = HDR_PARSER[name as usize];
|
||||
HeaderParserResult::Header(name) => {
|
||||
let (is_many, parser) = HDR_PARSER[name.id() as usize];
|
||||
|
||||
let from_offset = stream.pos;
|
||||
let value = parser(stream);
|
||||
headers.offsets.push(HeaderOffset {
|
||||
name: name.clone(),
|
||||
start: from_offset,
|
||||
end: stream.pos,
|
||||
});
|
||||
|
||||
if !value.is_empty() {
|
||||
if is_many {
|
||||
match headers.entry(name) {
|
||||
match headers.values.entry(name.clone()) {
|
||||
Entry::Occupied(mut e) => {
|
||||
if let HeaderValue::Collection(col) = e.get_mut() {
|
||||
col.push(value);
|
||||
} else {
|
||||
let old_value = e.remove();
|
||||
headers.insert(
|
||||
headers.values.insert(
|
||||
name,
|
||||
HeaderValue::Collection(vec![old_value, value]),
|
||||
);
|
||||
@@ -63,11 +64,10 @@ pub fn parse_headers<'x>(headers: &mut Headers<'x>, stream: &mut MessageStream<'
|
||||
}
|
||||
}
|
||||
} else {
|
||||
headers.insert(name, value);
|
||||
headers.values.insert(name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
HeaderParserResult::Unsupported(_name) => parse_and_ignore(stream),
|
||||
HeaderParserResult::Lf => return true,
|
||||
HeaderParserResult::Eof => return false,
|
||||
}
|
||||
@@ -100,12 +100,17 @@ pub fn parse_header_name(data: &[u8]) -> (usize, HeaderParserResult) {
|
||||
if field.eq_ignore_ascii_case(HDR_NAMES[token_hash]) {
|
||||
return (
|
||||
bytes_read,
|
||||
HeaderParserResult::Supported(HDR_MAP[token_hash]),
|
||||
HeaderParserResult::Header(HDR_MAP[token_hash].clone()),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return (bytes_read, HeaderParserResult::Unsupported(field));
|
||||
return (
|
||||
bytes_read,
|
||||
HeaderParserResult::Header(HeaderName::Other(String::from_utf8_lossy(
|
||||
field,
|
||||
))),
|
||||
);
|
||||
}
|
||||
}
|
||||
b'\n' => {
|
||||
@@ -133,24 +138,51 @@ pub fn parse_header_name(data: &[u8]) -> (usize, HeaderParserResult) {
|
||||
(bytes_read, HeaderParserResult::Eof)
|
||||
}
|
||||
|
||||
/*fn set_unsupported(&mut self, stream: &mut MessageStream<'x>, name: &'x [u8]) {
|
||||
if let Ok(name) = std::str::from_utf8(name) {
|
||||
let value = parse_unstructured(stream);
|
||||
if !value.is_empty() {
|
||||
match self.others.entry(name) {
|
||||
Entry::Occupied(mut e) => {
|
||||
if let HeaderValue::Collection(col) = e.get_mut() {
|
||||
col.push(value);
|
||||
} else {
|
||||
let old_value = e.remove();
|
||||
self.others.insert(name,HeaderValue::Collection(vec![old_value, value]));
|
||||
}
|
||||
},
|
||||
Entry::Vacant(e) => {e.insert(value);},
|
||||
}
|
||||
impl<'x> HeaderName<'x> {
|
||||
fn id(&self) -> u8 {
|
||||
match self {
|
||||
HeaderName::Subject => 0,
|
||||
HeaderName::From => 1,
|
||||
HeaderName::To => 2,
|
||||
HeaderName::Cc => 3,
|
||||
HeaderName::Date => 4,
|
||||
HeaderName::Bcc => 5,
|
||||
HeaderName::ReplyTo => 6,
|
||||
HeaderName::Sender => 7,
|
||||
HeaderName::Comments => 8,
|
||||
HeaderName::InReplyTo => 9,
|
||||
HeaderName::Keywords => 10,
|
||||
HeaderName::Received => 11,
|
||||
HeaderName::MessageId => 12,
|
||||
HeaderName::References => 13,
|
||||
HeaderName::ReturnPath => 14,
|
||||
HeaderName::MimeVersion => 15,
|
||||
HeaderName::ContentDescription => 16,
|
||||
HeaderName::ContentId => 17,
|
||||
HeaderName::ContentLanguage => 18,
|
||||
HeaderName::ContentLocation => 19,
|
||||
HeaderName::ContentTransferEncoding => 20,
|
||||
HeaderName::ContentType => 21,
|
||||
HeaderName::ContentDisposition => 22,
|
||||
HeaderName::ResentTo => 23,
|
||||
HeaderName::ResentFrom => 24,
|
||||
HeaderName::ResentBcc => 25,
|
||||
HeaderName::ResentCc => 26,
|
||||
HeaderName::ResentSender => 27,
|
||||
HeaderName::ResentDate => 28,
|
||||
HeaderName::ResentMessageId => 29,
|
||||
HeaderName::ListArchive => 30,
|
||||
HeaderName::ListHelp => 31,
|
||||
HeaderName::ListId => 32,
|
||||
HeaderName::ListOwner => 33,
|
||||
HeaderName::ListPost => 34,
|
||||
HeaderName::ListSubscribe => 35,
|
||||
HeaderName::ListUnsubscribe => 36,
|
||||
HeaderName::Other(_) => 37,
|
||||
HeaderName::Invalid => 38,
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
@@ -161,27 +193,30 @@ mod tests {
|
||||
#[test]
|
||||
fn header_name_parse() {
|
||||
let inputs = [
|
||||
("From: ", HeaderParserResult::Supported(HeaderName::From)),
|
||||
("From: ", HeaderParserResult::Header(HeaderName::From)),
|
||||
(
|
||||
"receiVED: ",
|
||||
HeaderParserResult::Supported(HeaderName::Received),
|
||||
HeaderParserResult::Header(HeaderName::Received),
|
||||
),
|
||||
(
|
||||
" subject : ",
|
||||
HeaderParserResult::Supported(HeaderName::Subject),
|
||||
HeaderParserResult::Header(HeaderName::Subject),
|
||||
),
|
||||
(
|
||||
"X-Custom-Field : ",
|
||||
HeaderParserResult::Unsupported(b"X-Custom-Field"),
|
||||
HeaderParserResult::Header(HeaderName::Other("X-Custom-Field".into())),
|
||||
),
|
||||
(
|
||||
" T : ",
|
||||
HeaderParserResult::Header(HeaderName::Other("T".into())),
|
||||
),
|
||||
(" T : ", HeaderParserResult::Unsupported(b"T")),
|
||||
(
|
||||
"mal formed: ",
|
||||
HeaderParserResult::Unsupported(b"mal formed"),
|
||||
HeaderParserResult::Header(HeaderName::Other("mal formed".into())),
|
||||
),
|
||||
(
|
||||
"MIME-version : ",
|
||||
HeaderParserResult::Supported(HeaderName::MimeVersion),
|
||||
HeaderParserResult::Header(HeaderName::MimeVersion),
|
||||
),
|
||||
];
|
||||
|
||||
@@ -235,7 +270,7 @@ static HDR_PARSER: &[(
|
||||
(false, parse_address), // ListPost = 34,
|
||||
(false, parse_address), // ListSubscribe = 35,
|
||||
(false, parse_address), // ListUnsubscribe = 36,
|
||||
(false, parse_raw), // Unsupported = 37,
|
||||
(true, parse_raw), // Other = 37,
|
||||
];
|
||||
|
||||
static HDR_HASH: &[u8] = &[
|
||||
@@ -254,18 +289,18 @@ static HDR_HASH: &[u8] = &[
|
||||
|
||||
static HDR_MAP: &[HeaderName] = &[
|
||||
HeaderName::Date,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Sender,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Received,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::References,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Cc,
|
||||
HeaderName::Comments,
|
||||
HeaderName::ResentCc,
|
||||
HeaderName::ContentId,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::ResentMessageId,
|
||||
HeaderName::ReplyTo,
|
||||
HeaderName::ResentTo,
|
||||
@@ -273,54 +308,54 @@ static HDR_MAP: &[HeaderName] = &[
|
||||
HeaderName::ContentLanguage,
|
||||
HeaderName::Subject,
|
||||
HeaderName::ResentSender,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::ResentDate,
|
||||
HeaderName::To,
|
||||
HeaderName::Bcc,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::ContentTransferEncoding,
|
||||
HeaderName::ReturnPath,
|
||||
HeaderName::ListId,
|
||||
HeaderName::Keywords,
|
||||
HeaderName::ContentDescription,
|
||||
HeaderName::ListOwner,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::ContentType,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::ListHelp,
|
||||
HeaderName::MessageId,
|
||||
HeaderName::ContentLocation,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::ListSubscribe,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::ListPost,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::ResentFrom,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::ContentDisposition,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::InReplyTo,
|
||||
HeaderName::ListArchive,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::From,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::ListUnsubscribe,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Unsupported,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::Invalid,
|
||||
HeaderName::MimeVersion,
|
||||
];
|
||||
|
||||
|
||||
+201
-63
@@ -19,13 +19,13 @@ use crate::{
|
||||
quoted_printable::decode_quoted_printable,
|
||||
DecodeFnc, DecodeResult,
|
||||
},
|
||||
BinaryPart, ContentType, HeaderName, HeaderValue, Headers, InlinePart, Message, MessagePart,
|
||||
TextPart,
|
||||
AttachmentType, BinaryPart, ContentType, HeaderName, HeaderValue, Headers, Message,
|
||||
MessagePart, MessagePartId, MessageStructure, TextPart,
|
||||
};
|
||||
|
||||
use super::{
|
||||
header::parse_headers,
|
||||
mime::{get_bytes_to_boundary, seek_next_part, skip_crlf, skip_multipart_end},
|
||||
mime::{get_bytes_to_boundary, seek_crlf_end, seek_next_part, skip_crlf, skip_multipart_end},
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
@@ -42,6 +42,12 @@ enum MimeType {
|
||||
Other,
|
||||
}
|
||||
|
||||
impl Default for MimeType {
|
||||
fn default() -> Self {
|
||||
MimeType::Message
|
||||
}
|
||||
}
|
||||
|
||||
fn result_to_string<'x>(
|
||||
result: DecodeResult,
|
||||
data: &'x [u8],
|
||||
@@ -111,6 +117,7 @@ fn get_mime_type(
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct MessageParserState {
|
||||
mime_type: MimeType,
|
||||
mime_boundary: Option<Vec<u8>>,
|
||||
@@ -120,6 +127,8 @@ struct MessageParserState {
|
||||
text_parts: usize,
|
||||
need_html_body: bool,
|
||||
need_text_body: bool,
|
||||
header_id: Option<MessagePartId>,
|
||||
structure: Vec<MessageStructure>,
|
||||
}
|
||||
|
||||
impl MessageParserState {
|
||||
@@ -133,6 +142,20 @@ impl MessageParserState {
|
||||
text_parts: 0,
|
||||
need_text_body: true,
|
||||
need_html_body: true,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_structure(&mut self) -> MessageStructure {
|
||||
if let Some(header_id) = self.header_id {
|
||||
MessageStructure::MultiPart((header_id, std::mem::take(&mut self.structure)))
|
||||
} else if self.structure.len() > 1 {
|
||||
MessageStructure::List(std::mem::take(&mut self.structure))
|
||||
} else if self.structure.len() == 1 {
|
||||
self.structure.pop().unwrap()
|
||||
} else {
|
||||
debug_assert!(false, "No parts found.");
|
||||
MessageStructure::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -180,20 +203,24 @@ impl<'x> Message<'x> {
|
||||
|
||||
'outer: loop {
|
||||
// Obtain reference to either the message or the MIME part's header
|
||||
let header = if let MimeType::Message = state.mime_type {
|
||||
&mut message.headers
|
||||
let (is_message, header) = if let MimeType::Message = state.mime_type {
|
||||
message.offset_header = stream.pos;
|
||||
(true, &mut message.headers)
|
||||
} else {
|
||||
&mut mime_part_header
|
||||
(false, &mut mime_part_header)
|
||||
};
|
||||
|
||||
// Parse headers
|
||||
if !parse_headers(header, &mut stream) {
|
||||
break;
|
||||
} else if is_message {
|
||||
message.offset_body = seek_crlf_end(&stream, stream.pos);
|
||||
}
|
||||
|
||||
state.parts += 1;
|
||||
|
||||
let content_type = header
|
||||
.values
|
||||
.get(&HeaderName::ContentType)
|
||||
.and_then(|c| c.as_content_type_ref());
|
||||
|
||||
@@ -212,13 +239,32 @@ impl<'x> Message<'x> {
|
||||
|| mime_type == MimeType::MultipartAlernative,
|
||||
mime_type,
|
||||
mime_boundary: mime_boundary.into(),
|
||||
parts: 0,
|
||||
html_parts: message.html_body.len(),
|
||||
text_parts: message.text_body.len(),
|
||||
need_html_body: state.need_html_body,
|
||||
need_text_body: state.need_text_body,
|
||||
header_id: if !is_message {
|
||||
Some(message.parts.len())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
mime_part_header.clear();
|
||||
if !is_message {
|
||||
if mime_part_header.is_empty() {
|
||||
mime_part_header.values.insert(
|
||||
HeaderName::ContentType,
|
||||
HeaderValue::ContentType(ContentType {
|
||||
c_type: "text".into(),
|
||||
c_subtype: Some("plain".into()),
|
||||
attributes: None,
|
||||
}),
|
||||
);
|
||||
}
|
||||
message.parts.push(MessagePart::Multipart(std::mem::take(
|
||||
&mut mime_part_header,
|
||||
)));
|
||||
}
|
||||
state_stack.push(state);
|
||||
state = new_state;
|
||||
skip_crlf(&mut stream);
|
||||
@@ -232,14 +278,26 @@ impl<'x> Message<'x> {
|
||||
let new_state = MessageParserState {
|
||||
mime_type: MimeType::Message,
|
||||
mime_boundary: state.mime_boundary.take(),
|
||||
in_alternative: false,
|
||||
parts: 0,
|
||||
html_parts: 0,
|
||||
text_parts: 0,
|
||||
need_html_body: true,
|
||||
need_text_body: true,
|
||||
..Default::default()
|
||||
};
|
||||
mime_part_header.clear();
|
||||
if mime_part_header.is_empty() {
|
||||
mime_part_header.values.insert(
|
||||
HeaderName::ContentType,
|
||||
HeaderValue::ContentType(ContentType {
|
||||
c_type: "message".into(),
|
||||
c_subtype: Some("rfc822".into()),
|
||||
attributes: None,
|
||||
}),
|
||||
);
|
||||
}
|
||||
state
|
||||
.structure
|
||||
.push(MessageStructure::Part(message.parts.len()));
|
||||
message.parts.push(MessagePart::Multipart(std::mem::take(
|
||||
&mut mime_part_header,
|
||||
)));
|
||||
message_stack.push(message);
|
||||
state_stack.push(state);
|
||||
message = Message::new();
|
||||
@@ -251,6 +309,7 @@ impl<'x> Message<'x> {
|
||||
skip_crlf(&mut stream);
|
||||
|
||||
let (is_binary, decode_fnc): (bool, DecodeFnc) = match header
|
||||
.values
|
||||
.get(&HeaderName::ContentTransferEncoding)
|
||||
{
|
||||
Some(HeaderValue::Text(encoding)) if encoding.eq_ignore_ascii_case("base64") => {
|
||||
@@ -322,6 +381,7 @@ impl<'x> Message<'x> {
|
||||
|
||||
let is_inline = is_inline
|
||||
&& header
|
||||
.values
|
||||
.get(&HeaderName::ContentDisposition)
|
||||
.map_or_else(|| true, |d| !d.get_content_type().is_attachment())
|
||||
&& (state.parts == 1
|
||||
@@ -366,54 +426,63 @@ impl<'x> Message<'x> {
|
||||
let is_html = mime_type == MimeType::TextHtml;
|
||||
|
||||
if add_to_html && !is_html {
|
||||
message.html_body.push(InlinePart::Text(TextPart {
|
||||
message.html_body.push(message.parts.len());
|
||||
message.parts.push(MessagePart::Text(TextPart {
|
||||
headers: None,
|
||||
contents: text_to_html(&text_part.contents).into(),
|
||||
}));
|
||||
} else if add_to_text && is_html {
|
||||
message.text_body.push(InlinePart::Text(TextPart {
|
||||
message.text_body.push(message.parts.len());
|
||||
message.parts.push(MessagePart::Text(TextPart {
|
||||
headers: None,
|
||||
contents: html_to_text(&text_part.contents).into(),
|
||||
}));
|
||||
}
|
||||
|
||||
if add_to_html && is_html {
|
||||
message.html_body.push(InlinePart::Text(text_part));
|
||||
message.html_body.push(message.parts.len());
|
||||
} else if add_to_text && !is_html {
|
||||
message.text_body.push(InlinePart::Text(text_part));
|
||||
message.text_body.push(message.parts.len());
|
||||
} else {
|
||||
message.attachments.push(MessagePart::Text(text_part));
|
||||
message
|
||||
.attachments
|
||||
.push(AttachmentType::Attachment(message.parts.len()));
|
||||
}
|
||||
state
|
||||
.structure
|
||||
.push(MessageStructure::Part(message.parts.len()));
|
||||
message.parts.push(MessagePart::Text(text_part));
|
||||
} else {
|
||||
let binary_part = BinaryPart {
|
||||
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(if is_inline {
|
||||
AttachmentType::Inline(message.parts.len())
|
||||
} else {
|
||||
AttachmentType::Attachment(message.parts.len())
|
||||
});
|
||||
|
||||
state
|
||||
.structure
|
||||
.push(MessageStructure::Part(message.parts.len()));
|
||||
|
||||
message.parts.push(MessagePart::Binary(BinaryPart {
|
||||
headers: if !mime_part_header.is_empty() {
|
||||
Some(std::mem::take(&mut mime_part_header))
|
||||
} else {
|
||||
None
|
||||
},
|
||||
contents: result_to_bytes(bytes, stream.data),
|
||||
};
|
||||
|
||||
if add_to_html {
|
||||
message
|
||||
.html_body
|
||||
.push(InlinePart::InlineBinary(message.attachments.len() as u32));
|
||||
}
|
||||
if add_to_text {
|
||||
message
|
||||
.text_body
|
||||
.push(InlinePart::InlineBinary(message.attachments.len() as u32));
|
||||
}
|
||||
|
||||
message.attachments.push(if !is_inline {
|
||||
MessagePart::Binary(binary_part)
|
||||
} else {
|
||||
MessagePart::InlineBinary(binary_part)
|
||||
});
|
||||
}));
|
||||
};
|
||||
|
||||
if state.mime_boundary.is_some() {
|
||||
// Currently processing a MIME part
|
||||
let mut last_part_offset = stream.pos;
|
||||
|
||||
'inner: loop {
|
||||
if let MimeType::Message = state.mime_type {
|
||||
@@ -421,7 +490,12 @@ impl<'x> Message<'x> {
|
||||
if let (Some(mut prev_message), Some(mut prev_state)) =
|
||||
(message_stack.pop(), state_stack.pop())
|
||||
{
|
||||
prev_message.attachments.push(MessagePart::Message(message));
|
||||
message.structure = state.get_structure();
|
||||
message.offset_end = seek_crlf_end(&stream, last_part_offset);
|
||||
prev_message
|
||||
.attachments
|
||||
.push(AttachmentType::Attachment(prev_message.parts.len()));
|
||||
prev_message.parts.push(MessagePart::Message(message));
|
||||
message = prev_message;
|
||||
prev_state.mime_boundary = state.mime_boundary;
|
||||
state = prev_state;
|
||||
@@ -442,16 +516,24 @@ impl<'x> Message<'x> {
|
||||
if state.text_parts == message.text_body.len()
|
||||
&& state.html_parts != message.html_body.len()
|
||||
{
|
||||
for part in message.html_body[state.html_parts..].iter() {
|
||||
message.text_body.push(match part {
|
||||
InlinePart::Text(part) => InlinePart::Text(TextPart {
|
||||
headers: None,
|
||||
contents: html_to_text(&part.contents).into(),
|
||||
}),
|
||||
InlinePart::InlineBinary(index) => {
|
||||
InlinePart::InlineBinary(*index)
|
||||
}
|
||||
});
|
||||
for part_id in message.html_body[state.html_parts..].iter() {
|
||||
message.text_body.push(
|
||||
match message.parts.get(*part_id).unwrap() {
|
||||
MessagePart::Text(text) => {
|
||||
let part_id = message.parts.len();
|
||||
let text = MessagePart::Text(TextPart {
|
||||
headers: None,
|
||||
contents: html_to_text(&text.contents).into(),
|
||||
});
|
||||
message.parts.push(text);
|
||||
part_id
|
||||
}
|
||||
MessagePart::Binary(_) => *part_id,
|
||||
_ => {
|
||||
panic!("Unexpected part type");
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -459,23 +541,35 @@ impl<'x> Message<'x> {
|
||||
if state.html_parts == message.html_body.len()
|
||||
&& state.text_parts != message.text_body.len()
|
||||
{
|
||||
for part in message.text_body[state.text_parts..].iter() {
|
||||
message.html_body.push(match part {
|
||||
InlinePart::Text(part) => InlinePart::Text(TextPart {
|
||||
headers: None,
|
||||
contents: text_to_html(&part.contents).into(),
|
||||
}),
|
||||
InlinePart::InlineBinary(index) => {
|
||||
InlinePart::InlineBinary(*index)
|
||||
}
|
||||
});
|
||||
for part_id in message.text_body[state.html_parts..].iter() {
|
||||
message.html_body.push(
|
||||
match message.parts.get(*part_id).unwrap() {
|
||||
MessagePart::Text(text) => {
|
||||
let part_id = message.parts.len();
|
||||
let text = MessagePart::Text(TextPart {
|
||||
headers: None,
|
||||
contents: text_to_html(&text.contents).into(),
|
||||
});
|
||||
message.parts.push(text);
|
||||
part_id
|
||||
}
|
||||
MessagePart::Binary(_) => *part_id,
|
||||
_ => {
|
||||
panic!("Unexpected part type");
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(prev_state) = state_stack.pop() {
|
||||
if let Some(mut prev_state) = state_stack.pop() {
|
||||
// Add headers and substructure to parent part
|
||||
prev_state.structure.push(state.get_structure());
|
||||
|
||||
// Restore ancestor's state
|
||||
state = prev_state;
|
||||
last_part_offset = stream.pos;
|
||||
|
||||
if let Some(ref mime_boundary) = state.mime_boundary {
|
||||
// Ancestor has a MIME boundary, seek it.
|
||||
@@ -496,13 +590,24 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
while let Some(mut prev_message) = message_stack.pop() {
|
||||
while let (Some(mut prev_message), Some(prev_state)) =
|
||||
(message_stack.pop(), state_stack.pop())
|
||||
{
|
||||
message.offset_end = stream.pos;
|
||||
if !message.is_empty() {
|
||||
prev_message.attachments.push(MessagePart::Message(message));
|
||||
message.structure = state.get_structure();
|
||||
prev_message
|
||||
.attachments
|
||||
.push(AttachmentType::Attachment(prev_message.parts.len()));
|
||||
prev_message.parts.push(MessagePart::Message(message));
|
||||
}
|
||||
message = prev_message;
|
||||
state = prev_state;
|
||||
}
|
||||
|
||||
message.structure = state.get_structure();
|
||||
message.offset_end = stream.pos;
|
||||
|
||||
if !message.is_empty() {
|
||||
Some(message)
|
||||
} else {
|
||||
@@ -516,10 +621,10 @@ mod tests {
|
||||
|
||||
use crate::parsers::message::Message;
|
||||
|
||||
const SEPARATOR: &[u8] = "\n---- EXPECTED STRUCTURE ----\n".as_bytes();
|
||||
|
||||
#[test]
|
||||
fn parse_full_messages() {
|
||||
const SEPARATOR: &[u8] = "\n---- EXPECTED STRUCTURE ----\n".as_bytes();
|
||||
|
||||
for test_suite in ["rfc", "legacy", "thirdparty", "malformed"] {
|
||||
let mut test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
test_dir.push("tests");
|
||||
@@ -569,6 +674,39 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn message_to_yaml() {
|
||||
let mut file_name = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
file_name.push("tests");
|
||||
file_name.push("rfc");
|
||||
file_name.push("002.txt");
|
||||
|
||||
let mut input = fs::read(&file_name).unwrap();
|
||||
let mut pos = 0;
|
||||
|
||||
for sep_pos in 0..input.len() {
|
||||
if input[sep_pos..sep_pos + SEPARATOR.len()].eq(SEPARATOR) {
|
||||
pos = sep_pos;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assert!(
|
||||
pos > 0,
|
||||
"Failed to find separator in test file '{}'.",
|
||||
file_name.display()
|
||||
);
|
||||
|
||||
let input = input.split_at_mut(pos);
|
||||
let message = Message::parse(input.0).unwrap();
|
||||
let result = serde_yaml::to_string(&message).unwrap();
|
||||
|
||||
//fs::write("test.yaml", &result).unwrap();
|
||||
|
||||
println!("{}", result);
|
||||
}
|
||||
|
||||
/*
|
||||
#[test]
|
||||
fn generate_test_samples() {
|
||||
|
||||
+17
-5
@@ -19,7 +19,7 @@ pub fn seek_next_part(stream: &mut MessageStream, boundary: &[u8]) -> bool {
|
||||
|
||||
let mut match_count = 0;
|
||||
|
||||
for ch in stream.data[pos..].iter() {
|
||||
for ch in &stream.data[pos..] {
|
||||
pos += 1;
|
||||
|
||||
if ch == &boundary[match_count] {
|
||||
@@ -52,10 +52,10 @@ pub fn get_bytes_to_boundary<'x>(
|
||||
) -> (usize, DecodeResult) {
|
||||
let mut read_pos = start_pos;
|
||||
|
||||
return if !boundary.is_empty() {
|
||||
if !boundary.is_empty() {
|
||||
let mut match_count = 0;
|
||||
|
||||
for ch in stream.data[read_pos..].iter() {
|
||||
for ch in &stream.data[read_pos..] {
|
||||
read_pos += 1;
|
||||
|
||||
if ch == &boundary[match_count] {
|
||||
@@ -95,7 +95,7 @@ pub fn get_bytes_to_boundary<'x>(
|
||||
)
|
||||
} else {
|
||||
(0, DecodeResult::Empty)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
@@ -123,7 +123,7 @@ pub fn skip_multipart_end(stream: &mut MessageStream) -> bool {
|
||||
|
||||
#[inline(always)]
|
||||
pub fn skip_crlf(stream: &mut MessageStream) {
|
||||
for ch in stream.data[stream.pos..].iter() {
|
||||
for ch in &stream.data[stream.pos..] {
|
||||
match ch {
|
||||
b'\r' | b' ' | b'\t' => stream.pos += 1,
|
||||
b'\n' => {
|
||||
@@ -134,3 +134,15 @@ pub fn skip_crlf(stream: &mut MessageStream) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn seek_crlf_end(stream: &MessageStream, mut start_pos: usize) -> usize {
|
||||
for ch in &stream.data[start_pos..] {
|
||||
if ch.is_ascii_whitespace() {
|
||||
start_pos += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
start_pos
|
||||
}
|
||||
|
||||
+40
-36
@@ -13,42 +13,46 @@ use mail_parser::*;
|
||||
|
||||
#[test]
|
||||
fn test_api() {
|
||||
let input = concat!(
|
||||
"From: Art Vandelay <art@vandelay.com> (Vandelay Industries)\n",
|
||||
"To: \"Colleagues\": \"James Smythe\" <james@vandelay.com>; Friends:\n",
|
||||
" jane@example.com, =?UTF-8?Q?John_Sm=C3=AEth?= <john@example.com>;\n",
|
||||
"Date: Sat, 20 Nov 2021 14:22:01 -0800\n",
|
||||
"Subject: Why not both importing AND exporting? =?utf-8?b?4pi6?=\n",
|
||||
"Content-Type: multipart/mixed; boundary=\"festivus\";\n\n",
|
||||
"--festivus\n",
|
||||
"Content-Type: text/html; charset=\"us-ascii\"\n",
|
||||
"Content-Transfer-Encoding: base64\n\n",
|
||||
"PGh0bWw+PHA+SSB3YXMgdGhpbmtpbmcgYWJvdXQgcXVpdHRpbmcgdGhlICZsZHF1bztle\n",
|
||||
"HBvcnRpbmcmcmRxdW87IHRvIGZvY3VzIGp1c3Qgb24gdGhlICZsZHF1bztpbXBvcnRpbm\n",
|
||||
"cmcmRxdW87LDwvcD48cD5idXQgdGhlbiBJIHRob3VnaHQsIHdoeSBub3QgZG8gYm90aD8\n",
|
||||
"gJiN4MjYzQTs8L3A+PC9odG1sPg==\n",
|
||||
"--festivus\n",
|
||||
"Content-Type: message/rfc822\n\n",
|
||||
"From: \"Cosmo Kramer\" <kramer@kramerica.com>\n",
|
||||
"Subject: Exporting my book about coffee tables\n",
|
||||
"Content-Type: multipart/mixed; boundary=\"giddyup\";\n\n",
|
||||
"--giddyup\n",
|
||||
"Content-Type: text/plain; charset=\"utf-16\"\n",
|
||||
"Content-Transfer-Encoding: quoted-printable\n\n",
|
||||
"=FF=FE=0C!5=D8\"=DD5=D8)=DD5=D8-=DD =005=D8*=DD5=D8\"=DD =005=D8\"=\n",
|
||||
"=DD5=D85=DD5=D8-=DD5=D8,=DD5=D8/=DD5=D81=DD =005=D8*=DD5=D86=DD =\n",
|
||||
"=005=D8=1F=DD5=D8,=DD5=D8,=DD5=D8(=DD =005=D8-=DD5=D8)=DD5=D8\"=\n",
|
||||
"=DD5=D8=1E=DD5=D80=DD5=D8\"=DD!=00\n",
|
||||
"--giddyup\n",
|
||||
"Content-Type: image/gif; name*1=\"about \"; name*0=\"Book \";\n",
|
||||
" name*2*=utf-8''%e2%98%95 tables.gif\n",
|
||||
"Content-Transfer-Encoding: Base64\n",
|
||||
"Content-Disposition: attachment\n\n",
|
||||
"R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\n",
|
||||
"--giddyup--\n",
|
||||
"--festivus--\n",
|
||||
)
|
||||
.as_bytes();
|
||||
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 = Message::parse(input).unwrap();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user