Header parser functions completed.

This commit is contained in:
Mauro D
2021-10-17 13:05:00 +00:00
parent 59f05753ec
commit 5510bf9eb0
18 changed files with 972 additions and 212 deletions
+6 -1
View File
@@ -163,7 +163,12 @@ mod tests {
let result = writer.get_bytes().unwrap_or_else(|| [].into());
let result_str = std::str::from_utf8(result.as_ref()).unwrap();
//println!("'{}' -> '{}'", input.0.escape_debug(), result_str.escape_debug());
assert_eq!(result_str, input.1, "Failed for '{}'", input.0.escape_debug());
assert_eq!(
result_str,
input.1,
"Failed for '{}'",
input.0.escape_debug()
);
}
}
}
+2 -4
View File
@@ -1,19 +1,18 @@
use super::Writer;
pub struct BufferWriter {
buf: Vec<u8>
buf: Vec<u8>,
}
impl BufferWriter {
pub fn with_capacity(capacity: usize) -> BufferWriter {
BufferWriter {
buf: Vec::with_capacity(capacity)
buf: Vec::with_capacity(capacity),
}
}
}
impl Writer for BufferWriter {
fn write_byte(&mut self, byte: &u8) -> bool {
self.buf.push(*byte);
true
@@ -31,5 +30,4 @@ impl Writer for BufferWriter {
None
}
}
}
+13 -52
View File
@@ -132,80 +132,47 @@ impl SingleByteDecoder {
}
pub fn get_cp1250(capacity: usize) -> Box<dyn Writer> {
Box::new(SingleByteDecoder::new(
SingleByteDecoder::CP1250,
capacity,
))
Box::new(SingleByteDecoder::new(SingleByteDecoder::CP1250, capacity))
}
pub fn get_cp1251(capacity: usize) -> Box<dyn Writer> {
Box::new(SingleByteDecoder::new(
SingleByteDecoder::CP1251,
capacity,
))
Box::new(SingleByteDecoder::new(SingleByteDecoder::CP1251, capacity))
}
pub fn get_cp1252(capacity: usize) -> Box<dyn Writer> {
Box::new(SingleByteDecoder::new(
SingleByteDecoder::CP1252,
capacity,
))
Box::new(SingleByteDecoder::new(SingleByteDecoder::CP1252, capacity))
}
pub fn get_cp1253(capacity: usize) -> Box<dyn Writer> {
Box::new(SingleByteDecoder::new(
SingleByteDecoder::CP1253,
capacity,
))
Box::new(SingleByteDecoder::new(SingleByteDecoder::CP1253, capacity))
}
pub fn get_cp1254(capacity: usize) -> Box<dyn Writer> {
Box::new(SingleByteDecoder::new(
SingleByteDecoder::CP1254,
capacity,
))
Box::new(SingleByteDecoder::new(SingleByteDecoder::CP1254, capacity))
}
pub fn get_cp1255(capacity: usize) -> Box<dyn Writer> {
Box::new(SingleByteDecoder::new(
SingleByteDecoder::CP1255,
capacity,
))
Box::new(SingleByteDecoder::new(SingleByteDecoder::CP1255, capacity))
}
pub fn get_cp1256(capacity: usize) -> Box<dyn Writer> {
Box::new(SingleByteDecoder::new(
SingleByteDecoder::CP1256,
capacity,
))
Box::new(SingleByteDecoder::new(SingleByteDecoder::CP1256, capacity))
}
pub fn get_cp1257(capacity: usize) -> Box<dyn Writer> {
Box::new(SingleByteDecoder::new(
SingleByteDecoder::CP1257,
capacity,
))
Box::new(SingleByteDecoder::new(SingleByteDecoder::CP1257, capacity))
}
pub fn get_cp1258(capacity: usize) -> Box<dyn Writer> {
Box::new(SingleByteDecoder::new(
SingleByteDecoder::CP1258,
capacity,
))
Box::new(SingleByteDecoder::new(SingleByteDecoder::CP1258, capacity))
}
pub fn get_koi8_r(capacity: usize) -> Box<dyn Writer> {
Box::new(SingleByteDecoder::new(
SingleByteDecoder::KOI8_R,
capacity,
))
Box::new(SingleByteDecoder::new(SingleByteDecoder::KOI8_R, capacity))
}
pub fn get_koi8_u(capacity: usize) -> Box<dyn Writer> {
Box::new(SingleByteDecoder::new(
SingleByteDecoder::KOI8_U,
capacity,
))
Box::new(SingleByteDecoder::new(SingleByteDecoder::KOI8_U, capacity))
}
pub fn get_macintosh(capacity: usize) -> Box<dyn Writer> {
@@ -216,17 +183,11 @@ impl SingleByteDecoder {
}
pub fn get_ibm_850(capacity: usize) -> Box<dyn Writer> {
Box::new(SingleByteDecoder::new(
SingleByteDecoder::IBM850,
capacity,
))
Box::new(SingleByteDecoder::new(SingleByteDecoder::IBM850, capacity))
}
pub fn get_tis_620(capacity: usize) -> Box<dyn Writer> {
Box::new(SingleByteDecoder::new(
SingleByteDecoder::TIS_620,
capacity,
))
Box::new(SingleByteDecoder::new(SingleByteDecoder::TIS_620, capacity))
}
pub const ISO_8859_1: &'static [char] = &[
@@ -63,7 +63,9 @@ pub fn parse_encoded_word(stream: &MessageStream) -> Option<String> {
#[cfg(test)]
mod tests {
use crate::parsers::{encoded_word::parse_encoded_word, message_stream::MessageStream};
use crate::{
decoders::encoded_word::parse_encoded_word, parsers::message_stream::MessageStream,
};
#[test]
fn decode_rfc2047() {
@@ -78,7 +80,11 @@ mod tests {
"this is some text",
true,
),
("?US-ASCII?Q?Keith_Moore?=".to_string(), "Keith Moore", false),
(
"?US-ASCII?Q?Keith_Moore?=".to_string(),
"Keith Moore",
false,
),
(
"?iso_8859-1:1987?Q?Keld_J=F8rn_Simonsen?=".to_string(),
"Keld Jørn Simonsen",
@@ -146,7 +152,7 @@ mod tests {
//println!("Decoded '{}'", string);
assert_eq!(string, input.1);
}
None => panic!("Failed to parse '{}'", input.0),
None => panic!("Failed to decode '{}'", input.0),
}
}
}
+1 -1
View File
@@ -51,7 +51,7 @@ pub fn decode_hex(src: &[u8], dest: &mut dyn Writer) -> bool {
#[cfg(test)]
mod tests {
use crate::decoders::{Writer, buffer_writer::BufferWriter, hex::decode_hex};
use crate::decoders::{buffer_writer::BufferWriter, hex::decode_hex, Writer};
#[test]
fn decode_hex_line() {
+2 -1
View File
@@ -1,8 +1,9 @@
pub mod base64;
pub mod buffer_writer;
pub mod charsets;
pub mod encoded_word;
pub mod hex;
pub mod quoted_printable;
pub mod buffer_writer;
pub trait Writer {
fn write_byte(&mut self, byte: &u8) -> bool;
+6 -1
View File
@@ -194,7 +194,12 @@ mod tests {
"-".repeat(50)
);*/
assert_eq!(input.1, result_str, "Failed for '{}'", input.0.escape_debug());
assert_eq!(
input.1,
result_str,
"Failed for '{}'",
input.0.escape_debug()
);
}
}
}
+1 -3
View File
@@ -1,10 +1,9 @@
mod decoders;
mod parsers;
use crate::decoders::buffer_writer::BufferWriter;
use crate::parsers::message_stream::MessageStream;
use crate::decoders::quoted_printable::QuotedPrintableDecoder;
use crate::parsers::message_stream::MessageStream;
fn main() {
let mail = concat!(
@@ -31,5 +30,4 @@ fn main() {
//println!("{}", vec![0u8; 100].as_mut_slice().len());
let mut parser = MessageStream::new(mail.as_bytes());
}
+168 -61
View File
@@ -1,9 +1,11 @@
use std::borrow::Cow;
use crate::parsers::{
encoded_word::parse_encoded_word,
header::{HeaderValue, NamedValue},
message_stream::MessageStream,
use crate::{
decoders::encoded_word::parse_encoded_word,
parsers::{
header::{HeaderValue, NamedValue},
message_stream::MessageStream,
},
};
#[derive(PartialEq, Clone, Copy, Debug)]
@@ -103,19 +105,19 @@ pub fn add_address(parser: &mut AddressParser) {
parser
.addresses
.push(if has_mail && has_name && has_comment {
NamedValue::new(
NamedValue::boxed(
concat_tokens(&mut parser.name_tokens),
concat_tokens(&mut parser.comment_tokens).into(),
HeaderValue::String(concat_tokens(&mut parser.mail_tokens)),
)
} else if has_name && has_mail {
NamedValue::new(
NamedValue::boxed(
concat_tokens(&mut parser.name_tokens),
None,
HeaderValue::String(concat_tokens(&mut parser.mail_tokens)),
)
} else if has_mail && has_comment {
NamedValue::new(
NamedValue::boxed(
concat_tokens(&mut parser.comment_tokens),
None,
HeaderValue::String(concat_tokens(&mut parser.mail_tokens)),
@@ -123,19 +125,19 @@ pub fn add_address(parser: &mut AddressParser) {
} else if has_mail {
HeaderValue::String(concat_tokens(&mut parser.mail_tokens))
} else if has_name && has_comment {
NamedValue::new(
NamedValue::boxed(
concat_tokens(&mut parser.name_tokens),
concat_tokens(&mut parser.comment_tokens).into(),
HeaderValue::Empty,
)
} else if has_name {
NamedValue::new(
NamedValue::boxed(
concat_tokens(&mut parser.name_tokens),
None,
HeaderValue::Empty,
)
} else if has_comment {
NamedValue::new(
NamedValue::boxed(
concat_tokens(&mut parser.comment_tokens),
None,
HeaderValue::Empty,
@@ -173,7 +175,7 @@ pub fn add_group(parser: &mut AddressParser) {
let has_addresses = !parser.addresses.is_empty();
parser.result.push(if has_name && has_addresses {
NamedValue::new(
NamedValue::boxed(
parser.group_name.take().unwrap(),
parser.group_comment.take(),
HeaderValue::Array(std::mem::take(&mut parser.addresses)),
@@ -181,7 +183,7 @@ pub fn add_group(parser: &mut AddressParser) {
} else if has_addresses {
HeaderValue::Array(std::mem::take(&mut parser.addresses))
} else if has_name {
NamedValue::new(parser.group_name.take().unwrap(), None, HeaderValue::Empty)
NamedValue::boxed(parser.group_name.take().unwrap(), None, HeaderValue::Empty)
} else {
return;
});
@@ -381,7 +383,7 @@ mod tests {
let inputs = [
(
concat!("John Doe <jdoe@machine.example>\n"),
NamedValue::new(
NamedValue::boxed(
"John Doe".into(),
None,
HeaderValue::String("jdoe@machine.example".into()),
@@ -389,7 +391,7 @@ mod tests {
),
(
concat!(" Mary Smith <mary@example.net>\n"),
NamedValue::new(
NamedValue::boxed(
"Mary Smith".into(),
None,
HeaderValue::String("mary@example.net".into()),
@@ -397,7 +399,7 @@ mod tests {
),
(
concat!("\"Joe Q. Public\" <john.q.public@example.com>\n"),
NamedValue::new(
NamedValue::boxed(
"Joe Q. Public".into(),
None,
HeaderValue::String("john.q.public@example.com".into()),
@@ -406,13 +408,13 @@ mod tests {
(
concat!("Mary Smith <mary@x.test>, jdoe@example.org, Who? <one@y.test>\n"),
HeaderValue::Array(vec![
NamedValue::new(
NamedValue::boxed(
"Mary Smith".into(),
None,
HeaderValue::String("mary@x.test".into()),
),
HeaderValue::String("jdoe@example.org".into()),
NamedValue::new(
NamedValue::boxed(
"Who?".into(),
None,
HeaderValue::String("one@y.test".into()),
@@ -423,7 +425,7 @@ mod tests {
concat!("<boss@nil.test>, \"Giant; \\\"Big\\\" Box\" <sysservices@example.net>\n"),
HeaderValue::Array(vec![
HeaderValue::String("boss@nil.test".into()),
NamedValue::new(
NamedValue::boxed(
"Giant; \"Big\" Box".into(),
None,
HeaderValue::String("sysservices@example.net".into()),
@@ -432,17 +434,17 @@ mod tests {
),
(
concat!("A Group:Ed Jones <c@a.test>,joe@where.test,John <jdoe@one.test>;\n"),
NamedValue::new(
NamedValue::boxed(
"A Group".into(),
None,
HeaderValue::Array(vec![
NamedValue::new(
NamedValue::boxed(
"Ed Jones".into(),
None,
HeaderValue::String("c@a.test".into()),
),
HeaderValue::String("joe@where.test".into()),
NamedValue::new(
NamedValue::boxed(
"John".into(),
None,
HeaderValue::String("jdoe@one.test".into()),
@@ -452,11 +454,11 @@ mod tests {
),
(
concat!("Undisclosed recipients:;\n"),
NamedValue::new("Undisclosed recipients".into(), None, HeaderValue::Empty),
NamedValue::boxed("Undisclosed recipients".into(), None, HeaderValue::Empty),
),
(
concat!("\"Mary Smith: Personal Account\" <smith@home.example >\n"),
NamedValue::new(
NamedValue::boxed(
"Mary Smith: Personal Account".into(),
None,
HeaderValue::String("smith@home.example".into()),
@@ -464,7 +466,7 @@ mod tests {
),
(
concat!("Pete(A nice \\) chap) <pete(his account)@silly.test(his host)>\n"),
NamedValue::new(
NamedValue::boxed(
"Pete".into(),
Some("A nice ) chap his account his host".into()),
HeaderValue::String("pete@silly.test".into()),
@@ -474,7 +476,7 @@ mod tests {
concat!(
"Pete(A nice \n \\\n ) chap) <pete(his\n account)@silly\n .test(his host)>\n"
),
NamedValue::new(
NamedValue::boxed(
"Pete".into(),
Some("A nice ) chap his account his host".into()),
HeaderValue::String("pete@silly.test".into()),
@@ -487,24 +489,24 @@ mod tests {
" friend); (the end of the group)\n"
),
HeaderValue::Array(vec![
NamedValue::new(
NamedValue::boxed(
"A Group".into(),
Some("Some people".into()),
HeaderValue::Array(vec![
NamedValue::new(
NamedValue::boxed(
"Chris Jones".into(),
Some("Chris's host.".into()),
HeaderValue::String("c@public.example".into()),
),
HeaderValue::String("joe@example.org".into()),
NamedValue::new(
NamedValue::boxed(
"John".into(),
Some("my dear friend".into()),
HeaderValue::String("jdoe@one.test".into()),
),
]),
),
HeaderValue::Array(vec![NamedValue::new(
HeaderValue::Array(vec![NamedValue::boxed(
"the end of the group".into(),
None,
HeaderValue::Empty,
@@ -513,10 +515,10 @@ mod tests {
),
(
concat!("(Empty list)(start)Hidden recipients :(nobody(that I know)) ;\n"),
NamedValue::new(
NamedValue::boxed(
"Hidden recipients".into(),
Some("Empty list start".into()),
HeaderValue::Array(vec![NamedValue::new(
HeaderValue::Array(vec![NamedValue::boxed(
"nobody(that I know)".into(),
None,
HeaderValue::Empty,
@@ -525,7 +527,7 @@ mod tests {
),
(
concat!("Joe Q. Public <john.q.public@example.com>\n"),
NamedValue::new(
NamedValue::boxed(
"Joe Q. Public".into(),
None,
HeaderValue::String("john.q.public@example.com".into()),
@@ -534,7 +536,7 @@ mod tests {
(
concat!("Mary Smith <@node.test:mary@example.net>, , jdoe@test . example\n"),
HeaderValue::Array(vec![
NamedValue::new(
NamedValue::boxed(
"Mary Smith".into(),
None,
HeaderValue::String("@node.test:mary@example.net".into()),
@@ -544,7 +546,7 @@ mod tests {
),
(
concat!("John Doe <jdoe@machine(comment). example>\n"),
NamedValue::new(
NamedValue::boxed(
"John Doe".into(),
Some("comment".into()),
HeaderValue::String("jdoe@machine. example".into()),
@@ -552,7 +554,7 @@ mod tests {
),
(
concat!("Mary Smith\n \n\t<mary@example.net>\n"),
NamedValue::new(
NamedValue::boxed(
"Mary Smith".into(),
None,
HeaderValue::String("mary@example.net".into()),
@@ -560,7 +562,7 @@ mod tests {
),
(
concat!("=?US-ASCII*EN?Q?Keith_Moore?= <moore@cs.utk.edu>\n"),
NamedValue::new(
NamedValue::boxed(
"Keith Moore".into(),
None,
HeaderValue::String("moore@cs.utk.edu".into()),
@@ -568,7 +570,7 @@ mod tests {
),
(
concat!("John =?US-ASCII*EN?Q?Doe?= <moore@cs.utk.edu>\n"),
NamedValue::new(
NamedValue::boxed(
"John Doe".into(),
None,
HeaderValue::String("moore@cs.utk.edu".into()),
@@ -576,7 +578,7 @@ mod tests {
),
(
concat!("=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>\n"),
NamedValue::new(
NamedValue::boxed(
"Keld Jørn Simonsen".into(),
None,
HeaderValue::String("keld@dkuug.dk".into()),
@@ -584,7 +586,7 @@ mod tests {
),
(
concat!("=?ISO-8859-1?Q?Andr=E9?= Pirard <PIRARD@vm1.ulg.ac.be>\n"),
NamedValue::new(
NamedValue::boxed(
"André Pirard".into(),
None,
HeaderValue::String("PIRARD@vm1.ulg.ac.be".into()),
@@ -592,7 +594,7 @@ mod tests {
),
(
concat!("=?ISO-8859-1?Q?Olle_J=E4rnefors?= <ojarnef@admin.kth.se>\n"),
NamedValue::new(
NamedValue::boxed(
"Olle Järnefors".into(),
None,
HeaderValue::String("ojarnef@admin.kth.se".into()),
@@ -610,7 +612,7 @@ mod tests {
"Nathaniel Borenstein <nsb@thumper.bellcore.com>\n (=?iso-8859-8?b?7e",
"Xs+SDv4SDp7Oj08A==?=)\n"
),
NamedValue::new(
NamedValue::boxed(
"Nathaniel Borenstein".into(),
Some("םולש ןב ילטפנ".into()),
HeaderValue::String("nsb@thumper.bellcore.com".into()),
@@ -622,17 +624,17 @@ mod tests {
"ft.com>, Keith Moore <moore@cs.utk.edu>\n"
),
HeaderValue::Array(vec![
NamedValue::new(
NamedValue::boxed(
"Greg Vaudreuil".into(),
None,
HeaderValue::String("gvaudre@NRI.Reston.VA.US".into()),
),
NamedValue::new(
NamedValue::boxed(
"Ned Freed".into(),
None,
HeaderValue::String("ned@innosoft.com".into()),
),
NamedValue::new(
NamedValue::boxed(
"Keith Moore".into(),
None,
HeaderValue::String("moore@cs.utk.edu".into()),
@@ -641,7 +643,7 @@ mod tests {
),
(
concat!("=?ISO-8859-1?Q?a?= <test@test.com>\n"),
NamedValue::new(
NamedValue::boxed(
"a".into(),
None,
HeaderValue::String("test@test.com".into()),
@@ -649,7 +651,7 @@ mod tests {
),
(
concat!("\"=?ISO-8859-1?Q?a?= b\" <test@test.com>\n"),
NamedValue::new(
NamedValue::boxed(
"a b".into(),
None,
HeaderValue::String("test@test.com".into()),
@@ -657,7 +659,7 @@ mod tests {
),
(
concat!("=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?= <test@test.com>\n"),
NamedValue::new(
NamedValue::boxed(
"ab".into(),
None,
HeaderValue::String("test@test.com".into()),
@@ -665,7 +667,7 @@ mod tests {
),
(
concat!("=?ISO-8859-1?Q?a?=\n =?ISO-8859-1?Q?b?= <test@test.com>\n"),
NamedValue::new(
NamedValue::boxed(
"ab".into(),
None,
HeaderValue::String("test@test.com".into()),
@@ -673,7 +675,7 @@ mod tests {
),
(
concat!("=?ISO-8859-1?Q?a?= \"=?ISO-8859-2?Q?_b?=\" <test@test.com>\n"),
NamedValue::new(
NamedValue::boxed(
"a b".into(),
None,
HeaderValue::String("test@test.com".into()),
@@ -692,7 +694,7 @@ mod tests {
"\"=?ISO-8859-1?Q =?ISO-8859-1?Q?a?= \\\" =?ISO-8859-1?Q?b?=\" <last@addres",
"s.com>\n\nbody@content.com"
),
NamedValue::new(
NamedValue::boxed(
"=?ISO-8859-1?Q a \" b".into(),
None,
HeaderValue::String("last@address.com".into()),
@@ -700,7 +702,7 @@ mod tests {
),
(
concat!("=? <name@domain.com>\n"),
NamedValue::new(
NamedValue::boxed(
"=?".into(),
None,
HeaderValue::String("name@domain.com".into()),
@@ -712,17 +714,17 @@ mod tests {
"TF-8?Q?John_Sm=C3=AEth?=\n <john@example.com>;\n"
),
HeaderValue::Array(vec![
HeaderValue::Array(vec![NamedValue::new(
HeaderValue::Array(vec![NamedValue::boxed(
" James Smythe".into(),
None,
HeaderValue::String("james@example.com".into()),
)]),
NamedValue::new(
NamedValue::boxed(
"Friends".into(),
None,
HeaderValue::Array(vec![
HeaderValue::String("jane@example.com".into()),
NamedValue::new(
NamedValue::boxed(
"John Smîth".into(),
None,
HeaderValue::String("john@example.com".into()),
@@ -737,7 +739,7 @@ mod tests {
"test.com; addr5@test.com, addr6@test.com\n"
),
HeaderValue::Array(vec![
NamedValue::new(
NamedValue::boxed(
"List 1".into(),
None,
HeaderValue::Array(vec![
@@ -745,7 +747,7 @@ mod tests {
HeaderValue::String("addr2@test.com".into()),
]),
),
NamedValue::new(
NamedValue::boxed(
"List 2".into(),
None,
HeaderValue::Array(vec![
@@ -765,7 +767,7 @@ mod tests {
"dr4@test.com; addr5@test.com, addr6@test.com\n"
),
HeaderValue::Array(vec![
NamedValue::new(
NamedValue::boxed(
"List 1".into(),
None,
HeaderValue::Array(vec![
@@ -773,7 +775,7 @@ mod tests {
HeaderValue::String("addr2@test.com".into()),
]),
),
NamedValue::new(
NamedValue::boxed(
"List 2".into(),
None,
HeaderValue::Array(vec![
@@ -794,7 +796,7 @@ mod tests {
", addr4@test.com; addr5@test.com, addr6@test.com\n"
),
HeaderValue::Array(vec![
NamedValue::new(
NamedValue::boxed(
"Thís ís válíd ÚTF8".into(),
None,
HeaderValue::Array(vec![
@@ -802,7 +804,7 @@ mod tests {
HeaderValue::String("addr2@test.com".into()),
]),
),
NamedValue::new(
NamedValue::boxed(
"Thís ís válíd ÚTF8".into(),
None,
HeaderValue::Array(vec![
@@ -816,6 +818,111 @@ mod tests {
]),
]),
),
(
"<http://www.host.com/list/archive/> (Web Archive)\n",
NamedValue::boxed(
"Web Archive".into(),
None,
HeaderValue::String("http://www.host.com/list/archive/".into()),
),
),
(
"<mailto:archive@host.com?subject=index%20list>\n",
HeaderValue::String("mailto:archive@host.com?subject=index%20list".into()),
),
(
"<mailto:moderator@host.com> (Postings are Moderated)\n",
NamedValue::boxed(
"Postings are Moderated".into(),
None,
HeaderValue::String("mailto:moderator@host.com".into()),
),
),
(
concat!(
"(Use this command to join the list)\n <mailto:list-manager@host.com?b",
"ody=subscribe%20list>\n"
),
NamedValue::boxed(
"Use this command to join the list".into(),
None,
HeaderValue::String(
"mailto:list-manager@host.com?body=subscribe%20list".into(),
),
),
),
(
concat!(
"<http://www.host.com/list.cgi?cmd=sub&lst=list>,\n <mailto:list-manag",
"er@host.com?body=subscribe%20list>\n"
),
HeaderValue::Array(vec![
HeaderValue::String("http://www.host.com/list.cgi?cmd=sub&lst=list".into()),
HeaderValue::String(
"mailto:list-manager@host.com?body=subscribe%20list".into(),
),
]),
),
(
"NO (posting not allowed on this list)\n",
NamedValue::boxed(
"NO".into(),
Some("posting not allowed on this list".into()),
HeaderValue::Empty,
),
),
(
concat!(
"<ftp://ftp.host.com/list.txt> (FTP),\n <mailto:list@host.com?subject=",
"help>\n"
),
HeaderValue::Array(vec![
NamedValue::boxed(
"FTP".into(),
None,
HeaderValue::String("ftp://ftp.host.com/list.txt".into()),
),
HeaderValue::String("mailto:list@host.com?subject=help".into()),
]),
),
(
"<http://www.host.com/list/>, <mailto:list-info@host.com>\n",
HeaderValue::Array(vec![
HeaderValue::String("http://www.host.com/list/".into()),
HeaderValue::String("mailto:list-info@host.com".into()),
]),
),
(
concat!(
"(Use this command to get off the list)\n <mailto:list-manager@host.",
"com?body=unsubscribe%20list>\n"
),
NamedValue::boxed(
"Use this command to get off the list".into(),
None,
HeaderValue::String(
"mailto:list-manager@host.com?body=unsubscribe%20list".into(),
),
),
),
(
concat!(
"<http://www.host.com/list.cgi?cmd=unsub&lst=list>,\n <mailto:list-req",
"uest@host.com?subject=unsubscribe>\n"
),
HeaderValue::Array(vec![
HeaderValue::String("http://www.host.com/list.cgi?cmd=unsub&lst=list".into()),
HeaderValue::String("mailto:list-request@host.com?subject=unsubscribe".into()),
]),
),
(
concat!("<mailto:listmom@host.com> (Contact Person for Help)\n"),
NamedValue::boxed(
"Contact Person for Help".into(),
None,
HeaderValue::String("mailto:listmom@host.com".into()),
),
),
];
for input in inputs {
@@ -837,7 +944,7 @@ mod tests {
.replace("[\"", "\"")
.replace("\"]", "\""),
format!("{:?}", result)
.replace("NamedValue(NamedValue {", "NamedValue::new(")
.replace("NamedValue(NamedValue {", "NamedValue::boxed(")
.replace("Array([", "HeaderValue::Array(vec![")
.replace("String(", "HeaderValue::String(")
.replace("})", ")")
+3 -3
View File
@@ -6,10 +6,10 @@ use crate::{
map::{get_charset_decoder, get_default_decoder},
utf8::Utf8Decoder,
},
encoded_word::parse_encoded_word,
hex::decode_hex,
},
parsers::{
encoded_word::parse_encoded_word,
header::{HeaderValue, NamedValue},
message_stream::MessageStream,
},
@@ -264,7 +264,7 @@ pub fn parse_content_type<'x>(stream: &'x MessageStream) -> HeaderValue<'x> {
_ => {
return if let Some(content_type) = parser.c_type {
if parser.c_subtype.is_some() || !parser.attributes.is_empty() {
NamedValue::new(
NamedValue::boxed(
content_type,
parser.c_subtype.take(),
if !parser.attributes.is_empty() {
@@ -704,7 +704,7 @@ mod tests {
if let Some(content_type) = c_type {
if c_subtype.is_some() || !attributes.is_empty() {
NamedValue::new(
NamedValue::boxed(
content_type,
c_subtype.take(),
if !attributes.is_empty() {
+203
View File
@@ -0,0 +1,203 @@
use std::borrow::Cow;
use crate::{
decoders::encoded_word::parse_encoded_word,
parsers::{header::HeaderValue, message_stream::MessageStream},
};
struct ListParser<'x> {
token_start: usize,
token_end: usize,
is_token_safe: bool,
is_token_start: bool,
tokens: Vec<Cow<'x, str>>,
list: Vec<HeaderValue<'x>>,
}
fn add_token<'x>(parser: &mut ListParser<'x>, stream: &'x MessageStream, add_space: bool) {
if parser.token_start > 0 {
if !parser.tokens.is_empty() {
parser.tokens.push(" ".into());
}
parser.tokens.push(
stream
.get_string(
parser.token_start - 1,
parser.token_end,
parser.is_token_safe,
)
.unwrap(),
);
if add_space {
parser.tokens.push(" ".into());
}
parser.token_start = 0;
parser.is_token_safe = true;
parser.is_token_start = true;
}
}
fn add_tokens_to_list<'x>(parser: &mut ListParser<'x>) {
if !parser.tokens.is_empty() {
parser
.list
.push(HeaderValue::String(if parser.tokens.len() == 1 {
parser.tokens.pop().unwrap()
} else {
let value = parser.tokens.concat();
parser.tokens.clear();
value.into()
}));
}
}
pub fn parse_comma_separared<'x>(stream: &'x MessageStream) -> HeaderValue<'x> {
let mut parser = ListParser {
token_start: 0,
token_end: 0,
is_token_safe: true,
is_token_start: true,
tokens: Vec::new(),
list: Vec::new(),
};
while let Some(ch) = stream.next() {
match ch {
b'\n' => {
add_token(&mut parser, stream, false);
match stream.peek() {
Some(b' ' | b'\t') => {
if !parser.is_token_start {
parser.is_token_start = true;
}
stream.advance(1);
continue;
}
_ => {
add_tokens_to_list(&mut parser);
return match parser.list.len() {
1 => parser.list.pop().unwrap(),
0 => HeaderValue::Empty,
_ => HeaderValue::Array(parser.list),
};
}
}
}
b' ' | b'\t' => {
if !parser.is_token_start {
parser.is_token_start = true;
}
continue;
}
b'=' if parser.is_token_start => {
if let Some(token) = parse_encoded_word(stream) {
add_token(&mut parser, stream, true);
parser.tokens.push(token.into());
continue;
}
}
b',' => {
add_token(&mut parser, stream, false);
add_tokens_to_list(&mut parser);
continue;
}
b'\r' => continue,
0..=0x7f => (),
_ => {
if parser.is_token_safe {
parser.is_token_safe = false;
}
}
}
if parser.is_token_start {
parser.is_token_start = false;
}
if parser.token_start == 0 {
parser.token_start = stream.get_pos();
}
parser.token_end = stream.get_pos();
}
HeaderValue::Empty
}
mod tests {
use crate::parsers::{
fields::list::parse_comma_separared, header::HeaderValue, message_stream::MessageStream,
};
#[test]
fn parse_comma_separated_text() {
let inputs = [
(" one item \n", HeaderValue::String("one item".into())),
(
"simple, list\n",
HeaderValue::Array(vec![
HeaderValue::String("simple".into()),
HeaderValue::String("list".into()),
]),
),
(
"multi \r\n list, \r\n with, cr lf \r\n",
HeaderValue::Array(vec![
HeaderValue::String("multi list".into()),
HeaderValue::String("with".into()),
HeaderValue::String("cr lf".into()),
]),
),
(
"=?iso-8859-1?q?this is some text?=, in, a, list, \n",
HeaderValue::Array(vec![
HeaderValue::String("this is some text".into()),
HeaderValue::String("in".into()),
HeaderValue::String("a".into()),
HeaderValue::String("list".into()),
]),
),
(
concat!(
" =?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\n ",
"=?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\n",
" , but, in a list, which, is, more, fun!\n"
),
HeaderValue::Array(vec![
HeaderValue::String("If you can read this you understand the example.".into()),
HeaderValue::String("but".into()),
HeaderValue::String("in a list".into()),
HeaderValue::String("which".into()),
HeaderValue::String("is".into()),
HeaderValue::String("more".into()),
HeaderValue::String("fun!".into()),
]),
),
(
"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=\n , listed\n",
HeaderValue::Array(vec![
HeaderValue::String("ab".into()),
HeaderValue::String("listed".into()),
]),
),
(
"ハロー・ワールド, and also, ascii terms\n",
HeaderValue::Array(vec![
HeaderValue::String("ハロー・ワールド".into()),
HeaderValue::String("and also".into()),
HeaderValue::String("ascii terms".into()),
]),
),
];
for input in inputs {
assert_eq!(
parse_comma_separared(&MessageStream::new(input.0.as_bytes())),
input.1
);
}
}
}
+191
View File
@@ -1,5 +1,196 @@
use std::collections::hash_map::Entry;
use self::{
address::parse_address, id::parse_id, list::parse_comma_separared, raw::parse_raw,
unstructured::parse_unstructured,
};
use super::{
header::{Header, HeaderValue},
message_stream::MessageStream,
};
pub mod address;
pub mod content_type;
pub mod date;
pub mod id;
pub mod list;
pub mod raw;
pub mod unstructured;
pub fn parse_date<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.date = self::date::parse_date(stream, false);
}
pub fn parse_sender<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.sender = parse_address(stream);
}
pub fn parse_received<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
append_value(&mut header.received, parse_raw(stream));
}
pub fn parse_references<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.references = parse_id(stream);
}
pub fn parse_cc<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.cc = parse_address(stream);
}
pub fn parse_comments<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
append_value(&mut header.comments, parse_unstructured(stream));
}
pub fn parse_resent_cc<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
append_value(&mut header.resent_cc, parse_address(stream));
}
pub fn parse_content_id<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.content_id = parse_id(stream);
}
pub fn parse_resent_message_id<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
append_value(&mut header.resent_message_id, parse_id(stream));
}
pub fn parse_reply_to<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.reply_to = parse_address(stream);
}
pub fn parse_resent_to<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
append_value(&mut header.resent_to, parse_address(stream));
}
pub fn parse_resent_bcc<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
append_value(&mut header.resent_bcc, parse_address(stream));
}
pub fn parse_subject<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.subject = parse_unstructured(stream);
}
pub fn parse_keywords<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
append_value(&mut header.keywords, parse_comma_separared(stream));
}
pub fn parse_list_help<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.list_help = parse_address(stream);
}
pub fn parse_list_id<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.list_id = parse_address(stream);
}
pub fn parse_list_owner<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.list_owner = parse_address(stream);
}
pub fn parse_resent_date<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
append_value(
&mut header.resent_date,
self::date::parse_date(stream, false),
);
}
pub fn parse_to<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.to = parse_address(stream);
}
pub fn parse_bcc<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.bcc = parse_address(stream);
}
pub fn parse_from<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.from = parse_address(stream);
}
pub fn parse_content_transfer_encoding<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.content_transfer_encoding = parse_unstructured(stream);
}
pub fn parse_return_path<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
append_value(&mut header.return_path, parse_raw(stream));
}
pub fn parse_list_archive<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.list_archive = parse_address(stream);
}
pub fn parse_resent_sender<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
append_value(&mut header.resent_sender, parse_address(stream));
}
pub fn parse_list_subscribe<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.list_subscribe = parse_address(stream);
}
pub fn parse_message_id<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.message_id = parse_id(stream);
}
pub fn parse_content_type<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.content_type = self::content_type::parse_content_type(stream);
}
pub fn parse_list_post<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.list_post = parse_address(stream);
}
pub fn parse_in_reply_to<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.reply_to = parse_address(stream);
}
pub fn parse_content_description<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.content_description = parse_unstructured(stream);
}
pub fn parse_resent_from<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
append_value(&mut header.resent_from, parse_address(stream));
}
pub fn parse_content_disposition<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.content_disposition = self::content_type::parse_content_type(stream);
}
pub fn parse_list_unsubscribe<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.list_unsubscribe = parse_address(stream);
}
pub fn parse_mime_version<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
header.mime_version = parse_raw(stream);
}
fn append_value<'x>(value: &mut HeaderValue<'x>, new_value: HeaderValue<'x>) {
if new_value != HeaderValue::Empty {
match value {
HeaderValue::Array(ref mut arr) => {
arr.push(new_value);
}
HeaderValue::Empty => *value = new_value,
_ => {
*value = HeaderValue::Array(vec![std::mem::take(value), new_value]);
}
}
}
}
pub fn parse_no_op<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {}
pub fn parse_unsupported<'x>(header: &mut Header<'x>, stream: &'x MessageStream, name: &'x [u8]) {
if let Ok(name) = std::str::from_utf8(name) {
let mut value = parse_unstructured(stream);
if let HeaderValue::String(_) = value {
if let Entry::Occupied(mut old_value) = header.others.entry(name) {
if let HeaderValue::Array(arr) = old_value.get_mut() {
arr.push(value);
return;
} else {
value = HeaderValue::Array(vec![old_value.remove(), value]);
//old_value.replace_entry(value); __ Unstable
}
}
header.others.insert(name, value);
}
}
}
+77
View File
@@ -0,0 +1,77 @@
use crate::parsers::{header::HeaderValue, message_stream::MessageStream};
pub fn parse_raw<'x>(stream: &'x MessageStream) -> HeaderValue<'x> {
let mut token_start: usize = 0;
let mut token_end: usize = 0;
let mut is_token_safe = true;
while let Some(ch) = stream.next() {
match ch {
b'\n' => match stream.peek() {
Some(b' ' | b'\t') => {
stream.advance(1);
continue;
}
_ => {
return if token_start > 0 {
HeaderValue::String(
stream
.get_string(token_start - 1, token_end, is_token_safe)
.unwrap(),
)
} else {
HeaderValue::Empty
};
}
},
b' ' | b'\t' | b'\r' => continue,
0..=0x7f => (),
_ => {
if is_token_safe {
is_token_safe = false;
}
}
}
if token_start == 0 {
token_start = stream.get_pos();
}
token_end = stream.get_pos();
}
HeaderValue::Empty
}
mod tests {
use crate::parsers::{fields::raw::parse_raw, header::HeaderValue, message_stream::MessageStream};
#[test]
fn parse_raw_text() {
let inputs = [
("Saying Hello\nMessage-Id", "Saying Hello"),
("Re: Saying Hello\r\n \r\nFrom:", "Re: Saying Hello"),
(
concat!(
" from x.y.test\n by example.net\n via TCP\n",
" with ESMTP\n id ABC12345\n ",
"for <mary@example.net>; 21 Nov 1997 10:05:43 -0600\n"
),
concat!(
"from x.y.test\n by example.net\n via TCP\n",
" with ESMTP\n id ABC12345\n ",
"for <mary@example.net>; 21 Nov 1997 10:05:43 -0600"
),
),
];
for input in inputs {
assert_eq!(
parse_raw(&MessageStream::new(input.0.as_bytes())),
HeaderValue::String(input.1.into()),
"Failed for '{:?}'",
input.0
);
}
}
}
+3 -2
View File
@@ -1,7 +1,8 @@
use std::borrow::Cow;
use crate::parsers::{
encoded_word::parse_encoded_word, header::HeaderValue, message_stream::MessageStream,
use crate::{
decoders::encoded_word::parse_encoded_word,
parsers::{header::HeaderValue, message_stream::MessageStream},
};
struct UnstructuredParser<'x> {
token_start: usize,
+215 -78
View File
@@ -2,11 +2,7 @@ use std::{borrow::Cow, collections::HashMap};
use crate::parsers::message_stream::MessageStream;
use super::fields::date::DateTime;
pub struct Message<'x> {
subject: Option<Cow<'x, str>>,
}
use super::fields::{date::DateTime, parse_unsupported};
#[derive(PartialEq, Debug)]
pub struct NamedValue<'x> {
@@ -16,7 +12,7 @@ pub struct NamedValue<'x> {
}
impl<'x> NamedValue<'x> {
pub fn new(
pub fn boxed(
name: Cow<'x, str>,
subname: Option<Cow<'x, str>>,
value: HeaderValue<'x>,
@@ -39,22 +35,77 @@ pub enum HeaderValue<'x> {
NamedValue(Box<NamedValue<'x>>),
}
pub trait MessageHeader<'x> {
fn set_subject(&mut self, stream: &'x MessageStream);
fn set_from(&mut self, stream: &'x MessageStream);
impl<'x> Default for HeaderValue<'x> {
fn default() -> Self {
HeaderValue::Empty
}
}
enum HeaderParserResult<'x, T> {
Supported(HeaderParserFnc<'x, T>),
#[derive(PartialEq, Debug, Default)]
pub struct Header<'x> {
pub bcc: HeaderValue<'x>,
pub cc: HeaderValue<'x>,
pub comments: HeaderValue<'x>,
pub content_description: HeaderValue<'x>,
pub content_disposition: HeaderValue<'x>,
pub content_id: HeaderValue<'x>,
pub content_transfer_encoding: HeaderValue<'x>,
pub content_type: HeaderValue<'x>,
pub date: HeaderValue<'x>,
pub from: HeaderValue<'x>,
pub in_reply_to: HeaderValue<'x>,
pub keywords: HeaderValue<'x>,
pub list_archive: HeaderValue<'x>,
pub list_help: HeaderValue<'x>,
pub list_id: HeaderValue<'x>,
pub list_owner: HeaderValue<'x>,
pub list_post: HeaderValue<'x>,
pub list_subscribe: HeaderValue<'x>,
pub list_unsubscribe: HeaderValue<'x>,
pub message_id: HeaderValue<'x>,
pub mime_version: HeaderValue<'x>,
pub received: HeaderValue<'x>,
pub references: HeaderValue<'x>,
pub reply_to: HeaderValue<'x>,
pub resent_bcc: HeaderValue<'x>,
pub resent_cc: HeaderValue<'x>,
pub resent_date: HeaderValue<'x>,
pub resent_from: HeaderValue<'x>,
pub resent_message_id: HeaderValue<'x>,
pub resent_sender: HeaderValue<'x>,
pub resent_to: HeaderValue<'x>,
pub return_path: HeaderValue<'x>,
pub sender: HeaderValue<'x>,
pub subject: HeaderValue<'x>,
pub to: HeaderValue<'x>,
pub others: HashMap<&'x str, HeaderValue<'x>>,
}
impl<'x> Header<'x> {
pub fn new() -> Header<'x> {
Header {
..Default::default()
}
}
}
enum HeaderParserResult<'x> {
Supported(fn(&mut Header<'x>, &'x MessageStream<'x>)),
Unsupported(&'x [u8]),
Eof,
}
type HeaderParserFnc<'x, T> = fn(&'x mut T, &MessageStream<'x>);
pub fn parse_headers<'x>(header: &mut Header<'x>, stream: &'x MessageStream<'x>) {
loop {
match parse_header_name(stream) {
HeaderParserResult::Supported(fnc) => fnc(header, stream),
HeaderParserResult::Unsupported(name) => parse_unsupported(header, stream, name),
HeaderParserResult::Eof => return,
}
}
}
pub fn parse_headers<'x>(stream: &'x MessageStream) {}
fn parse_header_name<'x, T>(stream: &'x MessageStream) -> HeaderParserResult<'x, T> {
fn parse_header_name<'x>(stream: &'x MessageStream) -> HeaderParserResult<'x> {
let mut token_start: usize = 0;
let mut token_end: usize = 0;
let mut token_len: usize = 0;
@@ -73,14 +124,15 @@ fn parse_header_name<'x, T>(stream: &'x MessageStream) -> HeaderParserResult<'x,
*HDR_HASH.get_unchecked(last_ch.to_ascii_lowercase() as usize)
} as usize;
if (4..=62).contains(&token_hash) {
if (4..=61).contains(&token_hash) {
let token_hash = token_hash - 4;
if field.eq_ignore_ascii_case(unsafe {
HDR_NAMES.get_unchecked(token_hash)
}) {
println!("Supported '{}'", std::str::from_utf8(field).unwrap());
return HeaderParserResult::Eof;
return HeaderParserResult::Supported(unsafe {
*HDR_FNCS.get_unchecked(token_hash)
});
}
}
}
@@ -117,108 +169,193 @@ fn parse_header_name<'x, T>(stream: &'x MessageStream) -> HeaderParserResult<'x,
#[cfg(test)]
mod tests {
use crate::parsers::{header::parse_header_name, message_stream::MessageStream};
use crate::parsers::{
fields::{parse_from, parse_mime_version, parse_received, parse_subject},
header::parse_header_name,
message_stream::MessageStream,
};
use super::HeaderParserResult;
#[test]
fn header_name_parse() {
let inputs = [
("From: ", "from"),
("\n\n \nreceiVED: ", "received"),
(" subject : ", "subject"),
("X-Custom-Field : ", "x-custom-field"),
(" T : ", "t"),
("mal formed: ", "mal formed"),
("MIME-version : ", "x-custom-field"),
("From: ", HeaderParserResult::Supported(parse_from)),
("receiVED: ", HeaderParserResult::Supported(parse_received)),
(
" subject : ",
HeaderParserResult::Supported(parse_subject),
),
(
"X-Custom-Field : ",
HeaderParserResult::Unsupported(b"X-Custom-Field"),
),
(" T : ", HeaderParserResult::Unsupported(b"T")),
(
"mal formed: ",
HeaderParserResult::Unsupported(b"mal formed"),
),
(
"MIME-version : ",
HeaderParserResult::Supported(parse_mime_version),
),
];
for input in inputs {
//unreachable!();
/*match parse_header_name(&MessageStream::new(input.0.as_bytes())) {
super::HeaderParserResult::Supported(_) => (),
super::HeaderParserResult::Unsupported(_) => (),
super::HeaderParserResult::Eof => (),
}*/
match parse_header_name(&MessageStream::new(input.0.as_bytes())) {
HeaderParserResult::Supported(f) => {
if let HeaderParserResult::Supported(val) = input.1 {
if f as usize == val as usize {
continue;
}
}
}
HeaderParserResult::Unsupported(name) => {
if let HeaderParserResult::Unsupported(val) = input.1 {
if name == val {
continue;
}
}
}
HeaderParserResult::Eof => panic!("EOF for '{}'", input.0.escape_debug()),
}
panic!("Failed to parse '{}'", input.0.escape_debug())
}
}
}
static HDR_HASH: &[u8] = &[
63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 20, 5, 0, 0, 0, 0, 5, 5, 63, 15, 15, 25, 20, 10, 0, 63, 0, 0, 15, 63, 63, 63, 63, 20,
63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
62, 62, 15, 5, 0, 0, 30, 5, 5, 15, 62, 20, 15, 20, 5, 30, 25, 62, 0, 0, 0, 62, 62, 62, 62, 10,
62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
];
/*static HDR_FNCS: &[for<'r, 's> fn(&'r mut T, &MessageStream<'s>)] = &[
MessageHeader::set_subject,
MessageHeader::set_from,
];*/
static HDR_FNCS: &[for<'x, 'y> fn(&'y mut Header<'x>, &'x MessageStream<'x>)] = &[
super::fields::parse_date,
super::fields::parse_no_op,
super::fields::parse_sender,
super::fields::parse_subject,
super::fields::parse_received,
super::fields::parse_no_op,
super::fields::parse_references,
super::fields::parse_resent_date,
super::fields::parse_cc,
super::fields::parse_comments,
super::fields::parse_resent_cc,
super::fields::parse_content_id,
super::fields::parse_return_path,
super::fields::parse_resent_message_id,
super::fields::parse_resent_sender,
super::fields::parse_no_op,
super::fields::parse_resent_bcc,
super::fields::parse_no_op,
super::fields::parse_list_id,
super::fields::parse_bcc,
super::fields::parse_list_post,
super::fields::parse_list_owner,
super::fields::parse_no_op,
super::fields::parse_content_type,
super::fields::parse_keywords,
super::fields::parse_content_description,
super::fields::parse_message_id,
super::fields::parse_no_op,
super::fields::parse_to,
super::fields::parse_no_op,
super::fields::parse_list_subscribe,
super::fields::parse_content_transfer_encoding,
super::fields::parse_no_op,
super::fields::parse_no_op,
super::fields::parse_reply_to,
super::fields::parse_resent_to,
super::fields::parse_no_op,
super::fields::parse_no_op,
super::fields::parse_list_archive,
super::fields::parse_no_op,
super::fields::parse_content_disposition,
super::fields::parse_no_op,
super::fields::parse_list_unsubscribe,
super::fields::parse_no_op,
super::fields::parse_no_op,
super::fields::parse_list_help,
super::fields::parse_no_op,
super::fields::parse_no_op,
super::fields::parse_mime_version,
super::fields::parse_no_op,
super::fields::parse_from,
super::fields::parse_no_op,
super::fields::parse_in_reply_to,
super::fields::parse_no_op,
super::fields::parse_no_op,
super::fields::parse_no_op,
super::fields::parse_no_op,
super::fields::parse_resent_from,
];
static HDR_NAMES: &[&[u8]] = &[
b"date",
b"",
b"sender",
b"",
b"subject",
b"received",
b"",
b"references",
b"",
b"resent-date",
b"cc",
b"comments",
b"resent-cc",
b"content-id",
b"",
b"return-path",
b"resent-message-id",
b"reply-to",
b"resent-to",
b"resent-sender",
b"",
b"resent-bcc",
b"",
b"subject",
b"keywords",
b"list-help",
b"list-owner",
b"resent-date",
b"to",
b"list-id",
b"bcc",
b"from",
b"content-transfer-encoding",
b"return-path",
b"list-archive",
b"resent-sender",
b"list-subscribe",
b"message-id",
b"list-post",
b"list-owner",
b"",
b"content-type",
b"",
b"list-post",
b"",
b"in-reply-to",
b"",
b"",
b"keywords",
b"content-description",
b"message-id",
b"",
b"resent-from",
b"to",
b"",
b"list-subscribe",
b"content-transfer-encoding",
b"",
b"",
b"reply-to",
b"resent-to",
b"",
b"",
b"list-archive",
b"",
b"content-disposition",
b"",
b"list-unsubscribe",
b"",
b"",
b"",
b"",
b"",
b"",
b"",
b"",
b"list-help",
b"",
b"",
b"mime-version",
b"",
b"from",
b"",
b"in-reply-to",
b"",
b"",
b"",
b"",
b"resent-from",
];
+67
View File
@@ -0,0 +1,67 @@
use super::{
header::{parse_headers, Header},
message_stream::MessageStream,
};
#[derive(Debug)]
pub struct Message<'x> {
header: Header<'x>,
}
pub fn parse_message<'x>(stream: &'x MessageStream<'x>) -> Message<'x> {
let mut message = Message {
header: Header::new(),
};
parse_headers(&mut message.header, stream);
println!("{:?}", message.header);
message
}
#[cfg(test)]
mod tests {
use crate::parsers::message_stream::MessageStream;
use super::parse_message;
#[test]
fn body_parse() {
let inputs = [(
concat!(
"Subject: This is a test email\n",
"Content-Type: multipart/alternative; boundary=foobar\n",
"Date: Sun, 02 Oct 2016 07:06:22 -0700 (PDT)\n",
"Message-Id: <1038776827.1181.6.camel@hurina>\n",
"List-Id: Dovecot Mailing List <dovecot.procontrol.fi>\n",
"List-Unsubscribe: <http://procontrol.fi/cgi-bin/mailman/listinfo/dovecot>,\n",
" <mailto:dovecot-request@procontrol.fi?subject=unsubscribe>\n",
"List-Archive: <http://procontrol.fi/pipermail/dovecot>\n",
"List-Post: <mailto:dovecot@procontrol.fi>\n",
"List-Help: <mailto:dovecot-request@procontrol.fi?subject=help>\n",
"List-Subscribe: <http://procontrol.fi/cgi-bin/mailman/listinfo/dovecot>,\n",
" <mailto:dovecot-request@procontrol.fi?subject=subscribe>\n",
"\n",
"--foobar\n",
"Content-Type: text/plain; charset=utf-8\n",
"Content-Transfer-Encoding: quoted-printable\n",
"\n",
"This is the plaintext version, in utf-8. Proof by Euro: =E2=82=AC\n",
"--foobar\n",
"Content-Type: text/html\n",
"Content-Transfer-Encoding: base64\n",
"\n",
"PGh0bWw+PGJvZHk+VGhpcyBpcyB0aGUgPGI+SFRNTDwvYj4gdmVyc2lvbiwgaW4g \n",
"dXMtYXNjaWkuIFByb29mIGJ5IEV1cm86ICZldXJvOzwvYm9keT48L2h0bWw+Cg== \n",
"--foobar--\n",
"After the final boundary stuff gets ignored.\n"
),
"",
)];
for input in inputs {
let value = parse_message(&MessageStream::new(input.0.as_bytes()));
}
}
}
+4 -1
View File
@@ -68,7 +68,10 @@ impl<'x> MessageStream<'x> {
}
pub fn match_bytes(&self, start_pos: usize, bytes: &[u8]) -> bool {
self.data.get(start_pos..start_pos + bytes.len()).unwrap_or(&[]) == bytes
self.data
.get(start_pos..start_pos + bytes.len())
.unwrap_or(&[])
== bytes
}
pub fn skip_byte(&self, ch: &u8) -> bool {
+1 -1
View File
@@ -1,4 +1,4 @@
pub mod encoded_word;
pub mod fields;
pub mod header;
pub mod message;
pub mod message_stream;