Token handling functions now take mutable refs to structs.

This commit is contained in:
Mauro D
2021-10-12 17:02:59 +00:00
parent 8c6f1b1a35
commit 83043bcc74
4 changed files with 274 additions and 227 deletions
+110 -91
View File
@@ -88,36 +88,7 @@ pub struct AddressParser<'x> {
group_comment: Option<Cow<'x, str>>,
}
impl<'x> AddressParser<'x> {
pub fn new() -> AddressParser<'x> {
AddressParser {
token_start: 0,
token_end: 0,
is_token_safe: true,
is_token_email: false,
is_token_start: true,
is_group_end: false,
is_escaped: false,
name_tokens: Vec::with_capacity(3),
mail_tokens: Vec::with_capacity(3),
comment_tokens: Vec::with_capacity(3),
state: AddressState::Name,
state_stack: Vec::with_capacity(5),
addresses: AddressList::new(),
group_name: None,
group_comment: None,
}
}
}
pub fn add_token<'x>(
mut parser: AddressParser<'x>,
stream: &'x MessageStream,
) -> AddressParser<'x> {
pub fn add_token<'x>(parser: &mut AddressParser<'x>, stream: &'x MessageStream) {
let token = stream
.get_string(
parser.token_start - 1,
@@ -144,7 +115,7 @@ pub fn add_token<'x>(
};
if add_space && !list.is_empty() {
list.push(Cow::from(" "));
list.push(" ".into());
}
list.push(token);
@@ -154,7 +125,6 @@ pub fn add_token<'x>(
parser.is_token_email = false;
parser.is_token_start = true;
parser.is_escaped = false;
parser
}
fn concat_tokens<'x>(tokens: &mut Vec<Cow<'x, str>>) -> Cow<'x, str> {
@@ -162,13 +132,13 @@ fn concat_tokens<'x>(tokens: &mut Vec<Cow<'x, str>>) -> Cow<'x, str> {
if tokens.len() == 1 {
result = tokens.pop().unwrap();
} else {
result = Cow::from(tokens.concat());
result = tokens.concat().into();
tokens.clear();
}
result
}
pub fn add_address(mut parser: AddressParser) -> AddressParser {
pub fn add_address(parser: &mut AddressParser) {
let has_mail = !parser.mail_tokens.is_empty();
let has_name = !parser.name_tokens.is_empty();
let has_comment = !parser.comment_tokens.is_empty();
@@ -212,11 +182,9 @@ pub fn add_address(mut parser: AddressParser) -> AddressParser {
.addresses
.push(Address::Invalid(concat_tokens(&mut parser.comment_tokens)));
}
parser
}
pub fn add_group(mut parser: AddressParser) -> AddressParser {
pub fn add_group(parser: &mut AddressParser) {
let has_mail = !parser.mail_tokens.is_empty();
let has_name = !parser.name_tokens.is_empty();
let has_comment = !parser.comment_tokens.is_empty();
@@ -233,18 +201,36 @@ pub fn add_group(mut parser: AddressParser) -> AddressParser {
if has_comment {
parser.group_comment = Some(concat_tokens(&mut parser.comment_tokens));
}
parser
}
pub fn parse_address<'x>(stream: &'x MessageStream) -> AddressField<'x> {
let mut parser = AddressParser::new();
let mut parser = AddressParser {
token_start: 0,
token_end: 0,
is_token_safe: true,
is_token_email: false,
is_token_start: true,
is_group_end: false,
is_escaped: false,
name_tokens: Vec::with_capacity(3),
mail_tokens: Vec::with_capacity(3),
comment_tokens: Vec::with_capacity(3),
state: AddressState::Name,
state_stack: Vec::with_capacity(5),
addresses: AddressList::new(),
group_name: None,
group_comment: None,
};
while let Some(ch) = stream.next() {
match ch {
b'\n' => {
if parser.token_start > 0 {
parser = add_token(parser, stream);
add_token(&mut parser, stream);
}
match stream.peek() {
@@ -263,7 +249,7 @@ pub fn parse_address<'x>(stream: &'x MessageStream) -> AddressField<'x> {
if parser.state == AddressState::Quote {
parser.token_end = stream.get_pos() - 1;
}
parser = add_token(parser, stream);
add_token(&mut parser, stream);
}
parser.is_escaped = true;
@@ -271,14 +257,14 @@ pub fn parse_address<'x>(stream: &'x MessageStream) -> AddressField<'x> {
}
b',' if parser.state == AddressState::Name => {
if parser.token_start > 0 {
parser = add_token(parser, stream);
add_token(&mut parser, stream);
}
parser = add_address(parser);
add_address(&mut parser);
continue;
}
b'<' if parser.state == AddressState::Name => {
if parser.token_start > 0 {
parser = add_token(parser, stream);
add_token(&mut parser, stream);
}
parser.state_stack.push(AddressState::Name);
parser.state = AddressState::Address;
@@ -286,7 +272,7 @@ pub fn parse_address<'x>(stream: &'x MessageStream) -> AddressField<'x> {
}
b'>' if parser.state == AddressState::Address => {
if parser.token_start > 0 {
parser = add_token(parser, stream);
add_token(&mut parser, stream);
}
parser.state = parser.state_stack.pop().unwrap();
continue;
@@ -296,13 +282,13 @@ pub fn parse_address<'x>(stream: &'x MessageStream) -> AddressField<'x> {
parser.state_stack.push(AddressState::Name);
parser.state = AddressState::Quote;
if parser.token_start > 0 {
parser = add_token(parser, stream);
add_token(&mut parser, stream);
}
continue;
}
AddressState::Quote => {
if parser.token_start > 0 {
parser = add_token(parser, stream);
add_token(&mut parser, stream);
}
parser.state = parser.state_stack.pop().unwrap();
continue;
@@ -316,7 +302,7 @@ pub fn parse_address<'x>(stream: &'x MessageStream) -> AddressField<'x> {
let pos_back = stream.get_pos() - 1;
if let Some(token) = parse_encoded_word(stream) {
let add_space = if parser.token_start > 0 {
parser = add_token(parser, stream);
add_token(&mut parser, stream);
parser.state != AddressState::Quote
} else {
false
@@ -357,7 +343,7 @@ pub fn parse_address<'x>(stream: &'x MessageStream) -> AddressField<'x> {
if parser.state != AddressState::Comment {
if parser.token_start > 0 {
parser = add_token(parser, stream);
add_token(&mut parser, stream);
}
parser.state = AddressState::Comment;
continue;
@@ -367,7 +353,7 @@ pub fn parse_address<'x>(stream: &'x MessageStream) -> AddressField<'x> {
let new_state = parser.state_stack.pop().unwrap();
if parser.state != new_state {
if parser.token_start > 0 {
parser = add_token(parser, stream);
add_token(&mut parser, stream);
}
parser.state = new_state;
continue;
@@ -375,16 +361,16 @@ pub fn parse_address<'x>(stream: &'x MessageStream) -> AddressField<'x> {
}
b':' if parser.state == AddressState::Name && !parser.is_escaped => {
if parser.token_start > 0 {
parser = add_token(parser, stream);
add_token(&mut parser, stream);
}
parser = add_group(parser);
add_group(&mut parser);
continue;
}
b';' if parser.state == AddressState::Name => {
if parser.token_start > 0 {
parser = add_token(parser, stream);
add_token(&mut parser, stream);
}
parser = add_address(parser);
add_address(&mut parser);
parser.is_group_end = true;
continue;
}
@@ -413,9 +399,9 @@ pub fn parse_address<'x>(stream: &'x MessageStream) -> AddressField<'x> {
}
if !parser.is_group_end {
parser = add_address(parser);
add_address(&mut parser);
} else {
parser = add_group(parser);
add_group(&mut parser);
}
if parser.group_name.is_none() {
@@ -439,7 +425,7 @@ pub fn parse_address<'x>(stream: &'x MessageStream) -> AddressField<'x> {
comment: parser.group_comment.unwrap(),
list: parser.addresses,
})
}
}
}
mod tests {
@@ -452,103 +438,136 @@ mod tests {
let inputs = [
(
"John Doe <jdoe@machine.example>\n".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("John Doe"), email: Cow::from("jdoe@machine.example") }))),
AddressField::Address(Address::Mailbox(Mailbox { name: ("John Doe").into(), email: Cow::from("jdoe@machine.example") }))
),
(
" Mary Smith <mary@example.net>\n".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("Mary Smith"), email: Cow::from("mary@example.net") }))),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("Mary Smith"), email: Cow::from("mary@example.net") }))
),
(
"\"Joe Q. Public\" <john.q.public@example.com>\n".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("Joe Q. Public"), email: Cow::from("john.q.public@example.com") }))),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("Joe Q. Public"), email: Cow::from("john.q.public@example.com") }))
),
(
"Mary Smith <mary@x.test>, jdoe@example.org, Who? <one@y.test>\n".to_string(),
AddressField::List(AddressList::from([Address::Mailbox(Mailbox { name: Cow::from("Mary Smith"), email: Cow::from("mary@x.test") }), Address::Email(Cow::from("jdoe@example.org")), Address::Mailbox(Mailbox { name: Cow::from("Who?"), email: Cow::from("one@y.test") })]))),
AddressField::List(AddressList::from([Address::Mailbox(Mailbox { name: Cow::from("Mary Smith"), email: Cow::from("mary@x.test") }), Address::Email(Cow::from("jdoe@example.org")), Address::Mailbox(Mailbox { name: Cow::from("Who?"), email: Cow::from("one@y.test") })]))
),
(
"<boss@nil.test>, \"Giant; \\\"Big\\\" Box\" <sysservices@example.net>\n".to_string(),
AddressField::List(AddressList::from([Address::Email(Cow::from("boss@nil.test")), Address::Mailbox(Mailbox { name: Cow::from("Giant; \"Big\" Box"), email: Cow::from("sysservices@example.net") })]))),
AddressField::List(AddressList::from([Address::Email(Cow::from("boss@nil.test")), Address::Mailbox(Mailbox { name: Cow::from("Giant; \"Big\" Box"), email: Cow::from("sysservices@example.net") })]))
),
(
"A Group:Ed Jones <c@a.test>,joe@where.test,John <jdoe@one.test>;\n".to_string(),
AddressField::Group(Group { name: Cow::from("A Group"), list: AddressList::from([Address::Mailbox(Mailbox { name: Cow::from("Ed Jones"), email: Cow::from("c@a.test") }), Address::Email(Cow::from("joe@where.test")), Address::Mailbox(Mailbox { name: Cow::from("John"), email: Cow::from("jdoe@one.test") })]) })),
AddressField::Group(Group { name: Cow::from("A Group"), list: AddressList::from([Address::Mailbox(Mailbox { name: Cow::from("Ed Jones"), email: Cow::from("c@a.test") }), Address::Email(Cow::from("joe@where.test")), Address::Mailbox(Mailbox { name: Cow::from("John"), email: Cow::from("jdoe@one.test") })]) })
),
(
"Undisclosed recipients:;\n".to_string(),
AddressField::Group(Group { name: Cow::from("Undisclosed recipients"), list: AddressList::from([]) })),
AddressField::Group(Group { name: Cow::from("Undisclosed recipients"), list: AddressList::from([]) })
),
(
"\"Mary Smith: Personal Account\" <smith@home.example >\n".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("Mary Smith: Personal Account"), email: Cow::from("smith@home.example") }))),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("Mary Smith: Personal Account"), email: Cow::from("smith@home.example") }))
),
(
"Pete(A nice \\) chap) <pete(his account)@silly.test(his host)>\n".to_string(),
AddressField::Address(Address::MailboxWithComment(MailboxWithComment { name: Cow::from("Pete"), comment: Cow::from("A nice ) chap his account his host"), email: Cow::from("pete@silly.test") }))),
AddressField::Address(Address::MailboxWithComment(MailboxWithComment { name: Cow::from("Pete"), comment: Cow::from("A nice ) chap his account his host"), email: Cow::from("pete@silly.test") }))
),
(
"Pete(A nice \n \\\n ) chap) <pete(his\n account)@silly\n .test(his host)>\n".to_string(),
AddressField::Address(Address::MailboxWithComment(MailboxWithComment { name: Cow::from("Pete"), comment: Cow::from("A nice ) chap his account his host"), email: Cow::from("pete@silly.test") }))),
AddressField::Address(Address::MailboxWithComment(MailboxWithComment { name: Cow::from("Pete"), comment: Cow::from("A nice ) chap his account his host"), email: Cow::from("pete@silly.test") }))
),
(
"A Group(Some people)\n :Chris Jones <c@(Chris's host.)public.example>,\n joe@example.org,\n John <jdoe@one.test> (my dear friend); (the end of the group)\n".to_string(),
AddressField::GroupWithComment(GroupWithComment { name: Cow::from("A Group"), comment: Cow::from("the end of the group"), list: AddressList::from([Address::MailboxWithComment(MailboxWithComment { name: Cow::from("Chris Jones"), comment: Cow::from("Chris's host."), email: Cow::from("c@public.example") }), Address::Email(Cow::from("joe@example.org")), Address::MailboxWithComment(MailboxWithComment { name: Cow::from("John"), comment: Cow::from("my dear friend"), email: Cow::from("jdoe@one.test") })]) }),),
AddressField::GroupWithComment(GroupWithComment { name: Cow::from("A Group"), comment: Cow::from("the end of the group"), list: AddressList::from([Address::MailboxWithComment(MailboxWithComment { name: Cow::from("Chris Jones"), comment: Cow::from("Chris's host."), email: Cow::from("c@public.example") }), Address::Email(Cow::from("joe@example.org")), Address::MailboxWithComment(MailboxWithComment { name: Cow::from("John"), comment: Cow::from("my dear friend"), email: Cow::from("jdoe@one.test") })]) }),
),
(
"(Empty list)(start)Hidden recipients :(nobody(that I know)) ;\n".to_string(),
AddressField::GroupWithComment(GroupWithComment { name: Cow::from("Hidden recipients"), comment: Cow::from("Empty list start"), list: AddressList::from([Address::Invalid(Cow::from("nobody(that I know)"))]) })),
AddressField::GroupWithComment(GroupWithComment { name: Cow::from("Hidden recipients"), comment: Cow::from("Empty list start"), list: AddressList::from([Address::Invalid(Cow::from("nobody(that I know)"))]) })
),
(
"Joe Q. Public <john.q.public@example.com>\n".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("Joe Q. Public"), email: Cow::from("john.q.public@example.com") }))),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("Joe Q. Public"), email: Cow::from("john.q.public@example.com") }))
),
(
"Mary Smith <@node.test:mary@example.net>, , jdoe@test . example\n".to_string(),
AddressField::List(AddressList::from([Address::Mailbox(Mailbox { name: Cow::from("Mary Smith"), email: Cow::from("@node.test:mary@example.net") }), Address::Email(Cow::from("jdoe@test . example"))]))),
AddressField::List(AddressList::from([Address::Mailbox(Mailbox { name: Cow::from("Mary Smith"), email: Cow::from("@node.test:mary@example.net") }), Address::Email(Cow::from("jdoe@test . example"))]))
),
(
"John Doe <jdoe@machine(comment). example>\n".to_string(),
AddressField::Address(Address::MailboxWithComment(MailboxWithComment { name: Cow::from("John Doe"), comment: Cow::from("comment"), email: Cow::from("jdoe@machine. example") }))),
AddressField::Address(Address::MailboxWithComment(MailboxWithComment { name: Cow::from("John Doe"), comment: Cow::from("comment"), email: Cow::from("jdoe@machine. example") }))
),
(
"Mary Smith\n \n\t<mary@example.net>\n".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("Mary Smith"), email: Cow::from("mary@example.net") }))),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("Mary Smith"), email: Cow::from("mary@example.net") }))
),
(
"=?US-ASCII*EN?Q?Keith_Moore?= <moore@cs.utk.edu>\n".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("Keith Moore"), email: Cow::from("moore@cs.utk.edu") }))),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("Keith Moore"), email: Cow::from("moore@cs.utk.edu") }))
),
(
"John =?US-ASCII*EN?Q?Doe?= <moore@cs.utk.edu>\n".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("John Doe"), email: Cow::from("moore@cs.utk.edu") }))),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("John Doe"), email: Cow::from("moore@cs.utk.edu") }))
),
(
"=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>\n".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("Keld Jørn Simonsen"), email: Cow::from("keld@dkuug.dk") }))),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("Keld Jørn Simonsen"), email: Cow::from("keld@dkuug.dk") }))
),
(
"=?ISO-8859-1?Q?Andr=E9?= Pirard <PIRARD@vm1.ulg.ac.be>\n".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("André Pirard"), email: Cow::from("PIRARD@vm1.ulg.ac.be") }))),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("André Pirard"), email: Cow::from("PIRARD@vm1.ulg.ac.be") }))
),
(
"=?ISO-8859-1?Q?Olle_J=E4rnefors?= <ojarnef@admin.kth.se>\n".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("Olle Järnefors"), email: Cow::from("ojarnef@admin.kth.se") }))),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("Olle Järnefors"), email: Cow::from("ojarnef@admin.kth.se") }))
),
(
"ietf-822@dimacs.rutgers.edu, ojarnef@admin.kth.se\n".to_string(),
AddressField::List(AddressList::from([Address::Email(Cow::from("ietf-822@dimacs.rutgers.edu")), Address::Email(Cow::from("ojarnef@admin.kth.se"))]))),
AddressField::List(AddressList::from([Address::Email(Cow::from("ietf-822@dimacs.rutgers.edu")), Address::Email(Cow::from("ojarnef@admin.kth.se"))]))
),
(
"Nathaniel Borenstein <nsb@thumper.bellcore.com>\n (=?iso-8859-8?b?7eXs+SDv4SDp7Oj08A==?=)\n".to_string(),
AddressField::Address(Address::MailboxWithComment(MailboxWithComment { name: Cow::from("Nathaniel Borenstein"), comment: Cow::from("םולש ןב ילטפנ"), email: Cow::from("nsb@thumper.bellcore.com") })),),
AddressField::Address(Address::MailboxWithComment(MailboxWithComment { name: Cow::from("Nathaniel Borenstein"), comment: Cow::from("םולש ןב ילטפנ"), email: Cow::from("nsb@thumper.bellcore.com") })),
),
(
"Greg Vaudreuil <gvaudre@NRI.Reston.VA.US>, Ned Freed\n <ned@innosoft.com>, Keith Moore <moore@cs.utk.edu>\n".to_string(),
AddressField::List(AddressList::from([Address::Mailbox(Mailbox { name: Cow::from("Greg Vaudreuil"), email: Cow::from("gvaudre@NRI.Reston.VA.US") }), Address::Mailbox(Mailbox { name: Cow::from("Ned Freed"), email: Cow::from("ned@innosoft.com") }), Address::Mailbox(Mailbox { name: Cow::from("Keith Moore"), email: Cow::from("moore@cs.utk.edu") })]))),
AddressField::List(AddressList::from([Address::Mailbox(Mailbox { name: Cow::from("Greg Vaudreuil"), email: Cow::from("gvaudre@NRI.Reston.VA.US") }), Address::Mailbox(Mailbox { name: Cow::from("Ned Freed"), email: Cow::from("ned@innosoft.com") }), Address::Mailbox(Mailbox { name: Cow::from("Keith Moore"), email: Cow::from("moore@cs.utk.edu") })]))
),
(
"=?ISO-8859-1?Q?a?= <test@test.com>\n".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("a"), email: Cow::from("test@test.com") })),),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("a"), email: Cow::from("test@test.com") })),
),
(
"\"=?ISO-8859-1?Q?a?= b\" <test@test.com>\n".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("a b"), email: Cow::from("test@test.com") }))),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("a b"), email: Cow::from("test@test.com") }))
),
(
"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?= <test@test.com>\n".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("ab"), email: Cow::from("test@test.com") }))),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("ab"), email: Cow::from("test@test.com") }))
),
(
"=?ISO-8859-1?Q?a?=\n =?ISO-8859-1?Q?b?= <test@test.com>\n".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("ab"), email: Cow::from("test@test.com") }))),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("ab"), email: Cow::from("test@test.com") }))
),
(
"=?ISO-8859-1?Q?a?= \"=?ISO-8859-2?Q?_b?=\" <test@test.com>\n".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("a b"), email: Cow::from("test@test.com") }))),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("a b"), email: Cow::from("test@test.com") }))
),
(
" <test@test.com>\n".to_string(),
AddressField::Address(Address::Email(Cow::from("test@test.com")))),
AddressField::Address(Address::Email(Cow::from("test@test.com")))
),
(
"test@test.com\ninvalid@address.com\n".to_string(),
AddressField::Address(Address::Email(Cow::from("test@test.com")))),
AddressField::Address(Address::Email(Cow::from("test@test.com")))
),
(
"\"=?ISO-8859-1?Q =?ISO-8859-1?Q?a?= \\\" =?ISO-8859-1?Q?b?=\" <last@address.com>\n\nbody@content.com".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("=?ISO-8859-1?Q a \" b"), email: Cow::from("last@address.com") }))),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("=?ISO-8859-1?Q a \" b"), email: Cow::from("last@address.com") }))
),
(
"=? <name@domain.com>\n".to_string(),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("=?"), email: Cow::from("name@domain.com") }))),
AddressField::Address(Address::Mailbox(Mailbox { name: Cow::from("=?"), email: Cow::from("name@domain.com") }))
),
];
for input in inputs {
+135 -100
View File
@@ -46,34 +46,7 @@ struct ContentTypeParser<'x> {
is_token_start: bool,
}
impl<'x> ContentTypeParser<'x> {
fn new() -> ContentTypeParser<'x> {
ContentTypeParser {
state: ContentState::Type,
state_stack: Vec::new(),
c_type: None,
c_subtype: None,
attr_name: None,
attributes: HashMap::new(),
values: Vec::new(),
is_encoded_attribute: false,
is_lower_case: true,
is_token_safe: true,
is_token_start: true,
is_escaped: false,
token_start: 0,
token_end: 0,
}
}
}
fn add_attribute<'x>(
mut parser: ContentTypeParser<'x>,
stream: &'x MessageStream,
) -> ContentTypeParser<'x> {
fn add_attribute<'x>(parser: &mut ContentTypeParser<'x>, stream: &'x MessageStream) {
let mut attr = stream.get_string(
parser.token_start - 1,
parser.token_end,
@@ -95,15 +68,11 @@ fn add_attribute<'x>(
parser.token_start = 0;
parser.is_token_safe = true;
parser.is_token_start = true;
parser
}
fn add_value<'x>(
mut parser: ContentTypeParser<'x>,
stream: &'x MessageStream,
) -> ContentTypeParser<'x> {
fn add_value<'x>(parser: &mut ContentTypeParser<'x>, stream: &'x MessageStream) {
if parser.attr_name.is_none() {
return parser;
return;
}
let has_values = !parser.values.is_empty();
@@ -115,7 +84,7 @@ fn add_value<'x>(
)
} else {
if !has_values {
return parser;
return;
}
None
};
@@ -129,7 +98,7 @@ fn add_value<'x>(
if let Some(value) = value {
parser.values.push(value);
}
Cow::from(parser.values.concat())
parser.values.concat().into()
},
);
} else {
@@ -141,7 +110,7 @@ fn add_value<'x>(
value
}
} else {
Cow::from(parser.values.concat())
parser.values.concat().into()
};
if let Some(charset) = parser.attributes.get(&(attr_name.clone() + "-charset")) {
@@ -183,11 +152,28 @@ fn add_value<'x>(
parser.token_start = 0;
parser.is_token_start = true;
parser.is_token_safe = true;
parser
}
pub fn parse_content_type<'x>(stream: &'x MessageStream) -> Option<ContentType<'x>> {
let mut parser = ContentTypeParser::new();
let mut parser = ContentTypeParser {
state: ContentState::Type,
state_stack: Vec::new(),
c_type: None,
c_subtype: None,
attr_name: None,
attributes: HashMap::new(),
values: Vec::new(),
is_encoded_attribute: false,
is_lower_case: true,
is_token_safe: true,
is_token_start: true,
is_escaped: false,
token_start: 0,
token_end: 0,
};
while let Some(ch) = stream.next() {
match ch {
@@ -220,10 +206,10 @@ pub fn parse_content_type<'x>(stream: &'x MessageStream) -> Option<ContentType<'
ContentState::Type | ContentState::AttributeName | ContentState::SubType
if parser.token_start > 0 =>
{
parser = add_attribute(parser, stream)
add_attribute(&mut parser, stream)
}
ContentState::AttributeValue | ContentState::AttributeQuotedValue => {
parser = add_value(parser, stream)
add_value(&mut parser, stream)
}
_ => (),
}
@@ -250,7 +236,7 @@ pub fn parse_content_type<'x>(stream: &'x MessageStream) -> Option<ContentType<'
}
b'/' if parser.state == ContentState::Type => {
if parser.token_start > 0 {
parser = add_attribute(parser, stream);
add_attribute(&mut parser, stream);
}
parser.state = ContentState::SubType;
continue;
@@ -258,14 +244,14 @@ pub fn parse_content_type<'x>(stream: &'x MessageStream) -> Option<ContentType<'
b';' => match parser.state {
ContentState::Type | ContentState::SubType | ContentState::AttributeName => {
if parser.token_start > 0 {
parser = add_attribute(parser, stream);
add_attribute(&mut parser, stream);
}
parser.state = ContentState::AttributeName;
continue;
}
ContentState::AttributeValue => {
if !parser.is_escaped {
parser = add_value(parser, stream);
add_value(&mut parser, stream);
parser.state = ContentState::AttributeName;
} else {
parser.is_escaped = false;
@@ -277,7 +263,7 @@ pub fn parse_content_type<'x>(stream: &'x MessageStream) -> Option<ContentType<'
b'*' if parser.state == ContentState::AttributeName => {
if !parser.is_encoded_attribute {
if parser.token_start > 0 {
parser = add_attribute(parser, stream);
add_attribute(&mut parser, stream);
}
parser.is_encoded_attribute = true;
}
@@ -287,7 +273,7 @@ pub fn parse_content_type<'x>(stream: &'x MessageStream) -> Option<ContentType<'
ContentState::AttributeName => {
if parser.token_start > 0 {
if !parser.is_encoded_attribute {
parser = add_attribute(parser, stream);
add_attribute(&mut parser, stream);
} else {
parser.token_start = 0;
}
@@ -313,12 +299,12 @@ pub fn parse_content_type<'x>(stream: &'x MessageStream) -> Option<ContentType<'
.unwrap(),
);
if parser.state != ContentState::AttributeQuotedValue {
parser.values.push(Cow::from(" "));
parser.values.push(" ".into());
}
parser.token_start = 0;
parser.is_token_safe = true;
}
parser.values.push(Cow::from(token));
parser.values.push(token.into());
continue;
} else {
stream.set_pos(pos_back);
@@ -336,7 +322,7 @@ pub fn parse_content_type<'x>(stream: &'x MessageStream) -> Option<ContentType<'
}
ContentState::AttributeQuotedValue => {
if !parser.is_escaped {
parser = add_value(parser, stream);
add_value(&mut parser, stream);
parser.state = ContentState::AttributeName;
continue;
} else {
@@ -406,9 +392,9 @@ pub fn parse_content_type<'x>(stream: &'x MessageStream) -> Option<ContentType<'
| ContentState::SubType
if parser.token_start > 0 =>
{
parser = add_attribute(parser, stream)
add_attribute(&mut parser, stream);
}
ContentState::AttributeValue => parser = add_value(parser, stream),
ContentState::AttributeValue => add_value(&mut parser, stream),
_ => (),
}
@@ -467,79 +453,104 @@ mod tests {
let inputs = [
(
"audio/basic\n".to_string(),
"audio||basic".to_string()),
"audio||basic".to_string()
),
(
"application/postscript \n".to_string(),
"application||postscript".to_string()),
"application||postscript".to_string()
),
(
"image/ jpeg\n".to_string(),
"image||jpeg".to_string()),
"image||jpeg".to_string()
),
(
" message / rfc822\n".to_string(),
"message||rfc822".to_string()),
"message||rfc822".to_string()
),
(
"inline\n".to_string(),
"inline".to_string()),
"inline".to_string()
),
(
" text/plain; charset =us-ascii (Plain text)\n".to_string(),
"text||plain||charset~~us-ascii".to_string()),
"text||plain||charset~~us-ascii".to_string()
),
(
"text/plain; charset= \"us-ascii\"\n".to_string(),
"text||plain||charset~~us-ascii".to_string()),
"text||plain||charset~~us-ascii".to_string()
),
(
"text/plain; charset =ISO-8859-1\n".to_string(),
"text||plain||charset~~ISO-8859-1".to_string()),
"text||plain||charset~~ISO-8859-1".to_string()
),
(
"text/foo; charset= bar\n".to_string(),
"text||foo||charset~~bar".to_string()),
"text||foo||charset~~bar".to_string()
),
(
" text /plain; charset=\"iso-8859-1\"; format=flowed\n".to_string(),
"text||plain||charset~~iso-8859-1||format~~flowed".to_string()),
"text||plain||charset~~iso-8859-1||format~~flowed".to_string()
),
(
"application/pgp-signature; x-mac-type=70674453;\n name=PGP.sig\n".to_string(),
"application||pgp-signature||x-mac-type~~70674453||name~~PGP.sig".to_string()),
"application||pgp-signature||x-mac-type~~70674453||name~~PGP.sig".to_string()
),
(
"multipart/mixed; boundary=gc0p4Jq0M2Yt08j34c0p\n".to_string(),
"multipart||mixed||boundary~~gc0p4Jq0M2Yt08j34c0p".to_string()),
"multipart||mixed||boundary~~gc0p4Jq0M2Yt08j34c0p".to_string()
),
(
"multipart/mixed; boundary=gc0pJq0M:08jU534c0p\n".to_string(),
"multipart||mixed||boundary~~gc0pJq0M:08jU534c0p".to_string()),
"multipart||mixed||boundary~~gc0pJq0M:08jU534c0p".to_string()
),
(
"multipart/mixed; boundary=\"gc0pJq0M:08jU534c0p\"\n".to_string(),
"multipart||mixed||boundary~~gc0pJq0M:08jU534c0p".to_string()),
"multipart||mixed||boundary~~gc0pJq0M:08jU534c0p".to_string()
),
(
"multipart/mixed; boundary=\"simple boundary\"\n".to_string(),
"multipart||mixed||boundary~~simple boundary".to_string()),
"multipart||mixed||boundary~~simple boundary".to_string()
),
(
"multipart/alternative; boundary=boundary42\n".to_string(),
"multipart||alternative||boundary~~boundary42".to_string()),
"multipart||alternative||boundary~~boundary42".to_string()
),
(
" multipart/mixed;\n boundary=\"---- main boundary ----\"\n".to_string(),
"multipart||mixed||boundary~~---- main boundary ----".to_string()),
"multipart||mixed||boundary~~---- main boundary ----".to_string()
),
(
"multipart/alternative; boundary=42\n".to_string(),
"multipart||alternative||boundary~~42".to_string()),
"multipart||alternative||boundary~~42".to_string()
),
(
"message/partial; id=\"ABC@host.com\";\n".to_string(),
"message||partial||id~~ABC@host.com".to_string()),
"message||partial||id~~ABC@host.com".to_string()
),
(
"multipart/parallel;boundary=unique-boundary-2\n".to_string(),
"multipart||parallel||boundary~~unique-boundary-2".to_string()),
"multipart||parallel||boundary~~unique-boundary-2".to_string()
),
(
"message/external-body; name=\"BodyFormats.ps\";\n site=\"thumper.bellcore.com\"; mode=\"image\";\n access-type=ANON-FTP; directory=\"pub\";\n expiration=\"Fri, 14 Jun 1991 19:13:14 -0400 (EDT)\"\n".to_string(),
"message||external-body||name~~BodyFormats.ps||site~~thumper.bellcore.com||mode~~image||access-type~~ANON-FTP||directory~~pub||expiration~~Fri, 14 Jun 1991 19:13:14 -0400 (EDT)".to_string()),
"message||external-body||name~~BodyFormats.ps||site~~thumper.bellcore.com||mode~~image||access-type~~ANON-FTP||directory~~pub||expiration~~Fri, 14 Jun 1991 19:13:14 -0400 (EDT)".to_string()
),
(
"message/external-body; access-type=local-file;\n name=\"/u/nsb/writing/rfcs/RFC-MIME.ps\";\n site=\"thumper.bellcore.com\";\n expiration=\"Fri, 14 Jun 1991 19:13:14 -0400 (EDT)\"\n".to_string(),
"message||external-body||access-type~~local-file||expiration~~Fri, 14 Jun 1991 19:13:14 -0400 (EDT)||name~~/u/nsb/writing/rfcs/RFC-MIME.ps||site~~thumper.bellcore.com".to_string()),
"message||external-body||access-type~~local-file||expiration~~Fri, 14 Jun 1991 19:13:14 -0400 (EDT)||name~~/u/nsb/writing/rfcs/RFC-MIME.ps||site~~thumper.bellcore.com".to_string()
),
(
"message/external-body;\n access-type=mail-server\n server=\"listserv@bogus.bitnet\";\n expiration=\"Fri, 14 Jun 1991 19:13:14 -0400 (EDT)\"\n".to_string(),
"message||external-body||access-type~~mail-server||server~~listserv@bogus.bitnet||expiration~~Fri, 14 Jun 1991 19:13:14 -0400 (EDT)".to_string()),
"message||external-body||access-type~~mail-server||server~~listserv@bogus.bitnet||expiration~~Fri, 14 Jun 1991 19:13:14 -0400 (EDT)".to_string()
),
(
"Message/Partial; number=2; total=3;\n id=\"oc=jpbe0M2Yt4s@thumper.bellcore.com\"\n".to_string(),
"message||partial||number~~2||total~~3||id~~oc=jpbe0M2Yt4s@thumper.bellcore.com".to_string()),
"message||partial||number~~2||total~~3||id~~oc=jpbe0M2Yt4s@thumper.bellcore.com".to_string()
),
(
"multipart/signed; micalg=pgp-sha1; protocol=\"application/pgp-signature\";\n boundary=\"=-J1qXPoyGtE2XNN5N6Z6j\"\n".to_string(),
"multipart||signed||protocol~~application/pgp-signature||boundary~~=-J1qXPoyGtE2XNN5N6Z6j||micalg~~pgp-sha1".to_string()),
"multipart||signed||protocol~~application/pgp-signature||boundary~~=-J1qXPoyGtE2XNN5N6Z6j||micalg~~pgp-sha1".to_string()
),
(
"message/external-body;\n access-type=local-file;\n name=\"/u/nsb/Me.jpeg\"\n".to_string(),
"message||external-body||access-type~~local-file||name~~/u/nsb/Me.jpeg".to_string()
@@ -565,22 +576,28 @@ mod tests {
),
(
" image/png;\n name=\"=?utf-8?q?=E3=83=8F=E3=83=AD=E3=83=BC=E3=83=BB=E3=83=AF=E3=83=BC=E3=83=AB=E3=83=89?=.png\"\n".to_string(),
"image||png||name~~ハロー・ワールド.png".to_string()),
"image||png||name~~ハロー・ワールド.png".to_string()
),
(
" image/gif;\n name==?iso-8859-6?b?5dHNyMcgyMfk2cfk5Q==?=.gif\n".to_string(),
"image||gif||name~~مرحبا بالعالم.gif".to_string()),
"image||gif||name~~مرحبا بالعالم.gif".to_string()
),
(
"image/jpeg;\n name=\"=?iso-8859-1?B?4Q==?= =?utf-8?B?w6k=?= =?iso-8859-1?q?=ED?=.jpeg\"\n".to_string(),
"image||jpeg||name~~á é í.jpeg".to_string()),
"image||jpeg||name~~á é í.jpeg".to_string()
),
(
"image/jpeg;\n name==?iso-8859-1?B?4Q==?= =?utf-8?B?w6k=?= =?iso-8859-1?q?=ED?=.jpeg\n".to_string(),
"image||jpeg||name~~áéí.jpeg".to_string()),
"image||jpeg||name~~áéí.jpeg".to_string()
),
(
"image/gif;\n name==?iso-8859-6?b?5dHNyMcgyMfk2cfk5S5naWY=?=\n".to_string(),
"image||gif||name~~مرحبا بالعالم.gif".to_string()),
"image||gif||name~~مرحبا بالعالم.gif".to_string()
),
(
" image/gif;\n name=\"=?iso-8859-6?b?5dHNyMcgyMfk2cfk5S5naWY=?=\"\n".to_string(),
"image||gif||name~~مرحبا بالعالم.gif".to_string()),
"image||gif||name~~مرحبا بالعالم.gif".to_string()
),
(
" inline; filename=\" best \\\"file\\\" ever with \\\\ escaped \\' stuff. \"\n".to_string(),
"inline||||filename~~ best \"file\" ever with \\ escaped ' stuff. ".to_string()
@@ -591,61 +608,79 @@ mod tests {
),
(
"/invalid\n".to_string(),
"".to_string()),
"".to_string()
),
(
"/\n".to_string(),
"".to_string()),
"".to_string()
),
(
";\n".to_string(),
"".to_string()),
"".to_string()
),
(
"/ ; name=value\n".to_string(),
"".to_string()),
"".to_string()
),
(
"text/plain;\n".to_string(),
"text||plain".to_string()),
"text||plain".to_string()
),
(
"text/plain;;\n".to_string(),
"text||plain".to_string()),
"text||plain".to_string()
),
(
"text/plain ;;;;; = ;; name=\"value\"\n".to_string(),
"text||plain||name~~value".to_string()),
"text||plain||name~~value".to_string()
),
(
"=\n".to_string(),
"=".to_string()),
"=".to_string()
),
(
"name=value\n".to_string(),
"name=value".to_string()),
"name=value".to_string()
),
(
"text/plain; name= \n".to_string(),
"text||plain".to_string()),
"text||plain".to_string()
),
(
"a/b; = \n".to_string(),
"a||b".to_string()),
"a||b".to_string()
),
(
"a/b; = \n \n".to_string(),
"a||b".to_string()),
(
"a/b; =value\n".to_string(),
"a||b".to_string()),
"a||b".to_string()
),
(
"test/test; =\"value\"\n".to_string(),
"test||test".to_string()),
"test||test".to_string()
),
(
"á/é; á=é\n".to_string(),
"á||é||á~~é".to_string()),
"á||é||á~~é".to_string()
),
(
"inva/lid; name=\" \n".to_string(),
"inva||lid||name~~ ".to_string()),
"inva||lid||name~~ ".to_string()
),
(
"inva/lid; name=\" \n \n".to_string(),
"inva||lid||name~~ ".to_string()),
"inva||lid||name~~ ".to_string()
),
(
"inva/lid; name=\" \n \"; test=test\n".to_string(),
"inva||lid||name~~ ||test~~test".to_string()),
"inva||lid||name~~ ||test~~test".to_string()
),
(
"name=value\n".to_string(),
"name=value".to_string()),
"name=value".to_string()
),
];
+24 -35
View File
@@ -9,49 +9,38 @@ pub struct UnstructuredParser<'x> {
tokens: Vec<Cow<'x, str>>,
}
impl<'x> UnstructuredParser<'x> {
pub fn new() -> UnstructuredParser<'x> {
UnstructuredParser {
token_start: 0,
token_end: 0,
is_token_safe: true,
is_token_start: true,
tokens: Vec::new(),
}
}
}
pub fn add_token<'x>(
mut parser: UnstructuredParser<'x>,
stream: &'x MessageStream,
) -> UnstructuredParser<'x> {
let bytes = stream
.get_bytes(parser.token_start - 1, parser.token_end)
.unwrap();
pub fn add_token<'x>(parser: &mut UnstructuredParser<'x>, stream: &'x MessageStream) {
if !parser.tokens.is_empty() {
parser.tokens.push(Cow::from(" "));
parser.tokens.push(" ".into());
}
parser.tokens.push(if parser.is_token_safe {
Cow::from(unsafe { std::str::from_utf8_unchecked(bytes) })
} else {
parser.is_token_safe = true;
String::from_utf8_lossy(bytes)
});
parser.tokens.push(
stream
.get_string(
parser.token_start - 1,
parser.token_end,
parser.is_token_safe,
)
.unwrap(),
);
parser.token_start = 0;
parser.is_token_safe = true;
parser.is_token_start = true;
parser
}
pub fn parse_unstructured<'x>(stream: &'x MessageStream) -> Option<Cow<'x, str>> {
let mut parser = UnstructuredParser::new();
let mut parser = UnstructuredParser {
token_start: 0,
token_end: 0,
is_token_safe: true,
is_token_start: true,
tokens: Vec::new(),
};
while let Some(ch) = stream.next() {
match ch {
b'\n' => {
if parser.token_start > 0 {
parser = add_token(parser, stream);
add_token(&mut parser, stream);
}
match stream.peek() {
Some(b' ' | b'\t') => {
@@ -65,7 +54,7 @@ pub fn parse_unstructured<'x>(stream: &'x MessageStream) -> Option<Cow<'x, str>>
return match parser.tokens.len() {
1 => parser.tokens.pop(),
0 => None,
_ => Some(Cow::from(parser.tokens.concat())),
_ => Some(parser.tokens.concat().into()),
};
}
}
@@ -81,10 +70,10 @@ pub fn parse_unstructured<'x>(stream: &'x MessageStream) -> Option<Cow<'x, str>>
if let Some(token) = parse_encoded_word(stream) {
if parser.token_start > 0 {
parser = add_token(parser, stream);
parser.tokens.push(Cow::from(" "));
add_token(&mut parser, stream);
parser.tokens.push(" ".into());
}
parser.tokens.push(Cow::from(token));
parser.tokens.push(token.into());
continue;
} else {
stream.set_pos(pos_back);
+5 -1
View File
@@ -22,7 +22,7 @@ impl<'x> MessageStream<'x> {
let bytes = self.data.get(from..to)?;
Some(if utf8_valid {
Cow::from(unsafe { std::str::from_utf8_unchecked(bytes) })
(unsafe { std::str::from_utf8_unchecked(bytes) }).into()
} else {
String::from_utf8_lossy(bytes)
})
@@ -55,6 +55,10 @@ impl<'x> MessageStream<'x> {
}
}
pub fn next2(&mut self) -> Option<&u8> {
None
}
#[inline(always)]
pub fn peek(&self) -> Option<&u8> {
unsafe { self.data.get(*self.pos.get()) }