mirror of
https://github.com/stalwartlabs/mail-parser.git
synced 2026-11-29 00:01:49 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b0f8052ee | ||
|
|
bfaeeeb886 | ||
|
|
514f989e7e | ||
|
|
bcf182bbc1 | ||
|
|
78c4cd424f | ||
|
|
4835ccfb9e | ||
|
|
c52670cf1f | ||
|
|
96d8123b74 |
Generated
+1
-1
@@ -59,7 +59,7 @@ checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mail-parser"
|
name = "mail-parser"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"encoding_rs",
|
"encoding_rs",
|
||||||
"serde",
|
"serde",
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mail-parser"
|
name = "mail-parser"
|
||||||
description = "Fast and robust e-mail parsing library for Rust"
|
description = "Fast and robust e-mail parsing library for Rust"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
authors = [ "Stalwart Labs <hello@stalw.art>"]
|
authors = [ "Stalwart Labs <hello@stalw.art>"]
|
||||||
license = "Apache-2.0 OR MIT"
|
license = "Apache-2.0 OR MIT"
|
||||||
|
|||||||
@@ -24,9 +24,9 @@ HTML and plain text inline body parts is done automatically when the _alternativ
|
|||||||
|
|
||||||
Performance and memory safety were two important factors while designing _mail-parser_:
|
Performance and memory safety were two important factors while designing _mail-parser_:
|
||||||
|
|
||||||
- **Zero-copy parsing**: Practically all strings returned by this library are `Cow<str>` references to the input raw message.
|
- **Zero-copy**: Practically all strings returned by this library are `Cow<str>` references to the input raw message.
|
||||||
- **High performance Base64 decoding** based on Chromium's decoder ([the fastest non-SIMD decoder](https://github.com/lemire/fastbase64)).
|
- **High performance Base64 decoding** based on Chromium's decoder ([the fastest non-SIMD decoder](https://github.com/lemire/fastbase64)).
|
||||||
- **Fast** parsing of message header fields, character set names and HTML entities using [perfect hashing](https://en.wikipedia.org/wiki/Perfect_hash_function).
|
- **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.
|
- Written in **100% safe** Rust with no external dependencies.
|
||||||
- Every function in the library has been [fuzzed](#testing-fuzzing--benchmarking) and
|
- Every function in the library has been [fuzzed](#testing-fuzzing--benchmarking) and
|
||||||
meticulously [tested with MIRI](#testing-fuzzing--benchmarking).
|
meticulously [tested with MIRI](#testing-fuzzing--benchmarking).
|
||||||
|
|||||||
@@ -1,3 +1,14 @@
|
|||||||
|
/*
|
||||||
|
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
|
||||||
|
* file at the top-level directory of this distribution.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||||
|
* option. This file may not be copied, modified, or distributed
|
||||||
|
* except according to those terms.
|
||||||
|
*/
|
||||||
|
|
||||||
use mail_parser::{Addr, Address, BodyPart, Group, Message, MessagePart, MimeFieldGet};
|
use mail_parser::{Addr, Address, BodyPart, Group, Message, MessagePart, MimeFieldGet};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
Generated
+1
-1
@@ -42,7 +42,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mail-parser"
|
name = "mail-parser"
|
||||||
version = "0.1.1"
|
version = "0.2.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"encoding_rs",
|
"encoding_rs",
|
||||||
"serde",
|
"serde",
|
||||||
|
|||||||
@@ -3,16 +3,16 @@ use libfuzzer_sys::fuzz_target;
|
|||||||
|
|
||||||
use mail_parser::{
|
use mail_parser::{
|
||||||
decoders::{
|
decoders::{
|
||||||
base64::Base64Decoder,
|
base64::decode_base64,
|
||||||
charsets::{
|
charsets::{
|
||||||
map::get_charset_decoder,
|
map::get_charset_decoder,
|
||||||
single_byte::decoder_iso_8859_1,
|
single_byte::decoder_iso_8859_1,
|
||||||
utf::{decoder_utf16, decoder_utf16_be, decoder_utf16_le, decoder_utf7},
|
utf::{decoder_utf16, decoder_utf16_be, decoder_utf16_le, decoder_utf7},
|
||||||
},
|
},
|
||||||
encoded_word::parse_encoded_word,
|
encoded_word::decode_rfc2047,
|
||||||
hex::decode_hex,
|
hex::decode_hex,
|
||||||
html::{add_html_token, html_to_text, text_to_html},
|
html::{add_html_token, html_to_text, text_to_html},
|
||||||
quoted_printable::QuotedPrintableDecoder,
|
quoted_printable::decode_quoted_printable,
|
||||||
},
|
},
|
||||||
parsers::{
|
parsers::{
|
||||||
fields::{
|
fields::{
|
||||||
@@ -24,8 +24,9 @@ use mail_parser::{
|
|||||||
raw::{parse_and_ignore, parse_raw},
|
raw::{parse_and_ignore, parse_raw},
|
||||||
unstructured::parse_unstructured,
|
unstructured::parse_unstructured,
|
||||||
},
|
},
|
||||||
|
mime::*,
|
||||||
header::parse_header_name,
|
header::parse_header_name,
|
||||||
message_stream::MessageStream,
|
message::MessageStream,
|
||||||
},
|
},
|
||||||
Message,
|
Message,
|
||||||
};
|
};
|
||||||
@@ -33,80 +34,80 @@ use mail_parser::{
|
|||||||
fuzz_target!(|data: &[u8]| {
|
fuzz_target!(|data: &[u8]| {
|
||||||
// Fuzz every parsing function
|
// Fuzz every parsing function
|
||||||
for n_fuzz in 1..=24 {
|
for n_fuzz in 1..=24 {
|
||||||
let stream = MessageStream::new(&data);
|
let mut stream = MessageStream::new(&data);
|
||||||
|
|
||||||
match n_fuzz {
|
match n_fuzz {
|
||||||
1 => {
|
1 => {
|
||||||
parse_date(&stream, false);
|
parse_date(&mut stream);
|
||||||
}
|
}
|
||||||
2 => {
|
2 => {
|
||||||
parse_address(&stream);
|
parse_address(&mut stream);
|
||||||
}
|
}
|
||||||
3 => {
|
3 => {
|
||||||
parse_id(&stream);
|
parse_id(&mut stream);
|
||||||
}
|
}
|
||||||
4 => {
|
4 => {
|
||||||
parse_comma_separared(&stream);
|
parse_comma_separared(&mut stream);
|
||||||
}
|
}
|
||||||
5 => {
|
5 => {
|
||||||
parse_and_ignore(&stream);
|
parse_and_ignore(&mut stream);
|
||||||
}
|
}
|
||||||
6 => {
|
6 => {
|
||||||
parse_raw(&stream);
|
parse_raw(&mut stream);
|
||||||
}
|
}
|
||||||
7 => {
|
7 => {
|
||||||
parse_unstructured(&stream);
|
parse_unstructured(&mut stream);
|
||||||
}
|
}
|
||||||
8 => {
|
8 => {
|
||||||
parse_content_type(&stream);
|
parse_content_type(&mut stream);
|
||||||
}
|
}
|
||||||
9 => {
|
9 => {
|
||||||
parse_header_name(&stream);
|
parse_header_name(&mut stream);
|
||||||
}
|
}
|
||||||
10 => {
|
10 => {
|
||||||
parse_encoded_word(&stream);
|
decode_rfc2047(&mut stream, 0);
|
||||||
}
|
}
|
||||||
11 => {
|
11 => {
|
||||||
stream.seek_next_part(b"\n");
|
seek_next_part(&mut stream, b"\n");
|
||||||
}
|
}
|
||||||
12 => {
|
12 => {
|
||||||
stream.get_bytes_to_boundary(b"\n");
|
get_bytes_to_boundary(&mut stream, 0, b"\n", false);
|
||||||
}
|
}
|
||||||
13 => {
|
13 => {
|
||||||
stream.get_bytes_to_boundary(&[]);
|
get_bytes_to_boundary(&mut stream, 0, &[], false);
|
||||||
}
|
}
|
||||||
14 => {
|
14 => {
|
||||||
stream.skip_crlf();
|
skip_crlf(&mut stream);
|
||||||
}
|
}
|
||||||
15 => {
|
15 => {
|
||||||
stream.is_boundary_end(0);
|
is_boundary_end(&mut stream, 0);
|
||||||
}
|
}
|
||||||
16 => {
|
16 => {
|
||||||
stream.skip_multipart_end();
|
skip_multipart_end(&mut stream);
|
||||||
}
|
}
|
||||||
17 => {
|
17 => {
|
||||||
stream.decode_base64(b"\n", true);
|
decode_base64(&mut stream, 0, b"\n", true);
|
||||||
}
|
}
|
||||||
18 => {
|
18 => {
|
||||||
stream.decode_base64(b"\n", false);
|
decode_base64(&mut stream, 0, b"\n", false);
|
||||||
}
|
}
|
||||||
19 => {
|
19 => {
|
||||||
stream.decode_base64(&[], true);
|
decode_base64(&mut stream, 0, &[], true);
|
||||||
}
|
}
|
||||||
20 => {
|
20 => {
|
||||||
stream.decode_base64(&[], false);
|
decode_base64(&mut stream, 0, &[], false);
|
||||||
}
|
}
|
||||||
21 => {
|
21 => {
|
||||||
stream.decode_quoted_printable(b"\n\n", true); // QP Boundaries have to be at least 2 bytes long
|
decode_quoted_printable(&mut stream, 0, b"\n\n", true); // QP Boundaries have to be at least 2 bytes long
|
||||||
}
|
}
|
||||||
22 => {
|
22 => {
|
||||||
stream.decode_quoted_printable(b"\n\n", false); // QP Boundaries have to be at least 2 bytes long
|
decode_quoted_printable(&mut stream, 0, b"\n\n", false); // QP Boundaries have to be at least 2 bytes long
|
||||||
}
|
}
|
||||||
23 => {
|
23 => {
|
||||||
stream.decode_quoted_printable(&[], true);
|
decode_quoted_printable(&mut stream, 0, &[], true);
|
||||||
}
|
}
|
||||||
24 => {
|
24 => {
|
||||||
stream.decode_quoted_printable(&[], false);
|
decode_quoted_printable(&mut stream, 0, &[], false);
|
||||||
}
|
}
|
||||||
0 | 25..=u32::MAX => unreachable!(),
|
0 | 25..=u32::MAX => unreachable!(),
|
||||||
}
|
}
|
||||||
|
|||||||
+93
-87
@@ -9,111 +9,108 @@
|
|||||||
* except according to those terms.
|
* except according to those terms.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use crate::parsers::message::MessageStream;
|
||||||
|
|
||||||
use crate::parsers::message_stream::MessageStream;
|
use super::DecodeResult;
|
||||||
|
|
||||||
pub trait Base64Decoder<'x> {
|
pub fn decode_base64<'x>(
|
||||||
fn decode_base64(&self, boundary: &[u8], is_word: bool) -> (bool, Option<Cow<'x, [u8]>>);
|
stream: &MessageStream<'x>,
|
||||||
}
|
start_pos: usize,
|
||||||
|
boundary: &[u8],
|
||||||
|
is_word: bool,
|
||||||
|
) -> (usize, DecodeResult) {
|
||||||
|
let mut success = boundary.is_empty();
|
||||||
|
|
||||||
impl<'x> Base64Decoder<'x> for MessageStream<'x> {
|
let mut chunk: u32 = 0;
|
||||||
fn decode_base64(&self, boundary: &[u8], is_word: bool) -> (bool, Option<Cow<'x, [u8]>>) {
|
let mut byte_count: u8 = 0;
|
||||||
let mut success = boundary.is_empty();
|
let mut match_count: usize = 0;
|
||||||
|
|
||||||
let mut chunk: u32 = 0;
|
let mut bytes_read = 0;
|
||||||
let mut byte_count: u8 = 0;
|
|
||||||
let mut match_count: usize = 0;
|
|
||||||
|
|
||||||
let start_pos = self.pos.get();
|
let mut buf = Vec::with_capacity(stream.data.len() - start_pos);
|
||||||
let mut read_pos = start_pos;
|
|
||||||
|
|
||||||
let mut buf = Vec::with_capacity(self.data.len() - read_pos);
|
for ch in stream.data[start_pos..].iter() {
|
||||||
|
bytes_read += 1;
|
||||||
|
|
||||||
for ch in self.data[read_pos..].iter() {
|
if !success {
|
||||||
read_pos += 1;
|
if *ch == boundary[match_count] {
|
||||||
|
match_count += 1;
|
||||||
if !success {
|
if match_count == boundary.len() {
|
||||||
if *ch == boundary[match_count] {
|
success = true;
|
||||||
match_count += 1;
|
|
||||||
if match_count == boundary.len() {
|
|
||||||
success = true;
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
} else if match_count > 0 {
|
|
||||||
if *ch == boundary[0] {
|
|
||||||
match_count = 1;
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
match_count = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let val = BASE64_MAP[byte_count as usize][*ch as usize];
|
|
||||||
|
|
||||||
if val >= 0x01ffffff {
|
|
||||||
if *ch == b'=' {
|
|
||||||
match byte_count {
|
|
||||||
1 | 2 => {
|
|
||||||
buf.push(chunk.to_le_bytes()[0]);
|
|
||||||
|
|
||||||
byte_count = 0;
|
|
||||||
}
|
|
||||||
3 => {
|
|
||||||
buf.extend_from_slice(&chunk.to_le_bytes()[0..2]);
|
|
||||||
byte_count = 0;
|
|
||||||
}
|
|
||||||
0 => (),
|
|
||||||
_ => {
|
|
||||||
success = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if !ch.is_ascii_whitespace() || is_word {
|
|
||||||
success = false;
|
|
||||||
break;
|
break;
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
continue;
|
} else if match_count > 0 {
|
||||||
}
|
if *ch == boundary[0] {
|
||||||
|
match_count = 1;
|
||||||
byte_count = (byte_count + 1) & 3;
|
continue;
|
||||||
|
} else {
|
||||||
if byte_count == 1 {
|
match_count = 0;
|
||||||
chunk = val;
|
|
||||||
} else {
|
|
||||||
chunk |= val;
|
|
||||||
|
|
||||||
if byte_count == 0 {
|
|
||||||
buf.extend_from_slice(&chunk.to_le_bytes()[0..3]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.pos.set(read_pos);
|
let val = BASE64_MAP[byte_count as usize][*ch as usize];
|
||||||
|
|
||||||
(
|
if val >= 0x01ffffff {
|
||||||
success,
|
if *ch == b'=' {
|
||||||
if !buf.is_empty() {
|
match byte_count {
|
||||||
buf.shrink_to_fit();
|
1 | 2 => {
|
||||||
Some(buf.into())
|
buf.push(chunk.to_le_bytes()[0]);
|
||||||
} else {
|
|
||||||
None
|
byte_count = 0;
|
||||||
},
|
}
|
||||||
)
|
3 => {
|
||||||
|
buf.extend_from_slice(&chunk.to_le_bytes()[0..2]);
|
||||||
|
byte_count = 0;
|
||||||
|
}
|
||||||
|
0 => (),
|
||||||
|
_ => {
|
||||||
|
success = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if !ch.is_ascii_whitespace() || is_word {
|
||||||
|
success = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte_count = (byte_count + 1) & 3;
|
||||||
|
|
||||||
|
if byte_count == 1 {
|
||||||
|
chunk = val;
|
||||||
|
} else {
|
||||||
|
chunk |= val;
|
||||||
|
|
||||||
|
if byte_count == 0 {
|
||||||
|
buf.extend_from_slice(&chunk.to_le_bytes()[0..3]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
(
|
||||||
|
if success { bytes_read } else { 0 },
|
||||||
|
if !buf.is_empty() {
|
||||||
|
buf.shrink_to_fit();
|
||||||
|
DecodeResult::Owned(buf)
|
||||||
|
} else {
|
||||||
|
DecodeResult::Empty
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::parsers::message_stream::MessageStream;
|
use crate::{
|
||||||
|
decoders::{base64::decode_base64, DecodeResult},
|
||||||
use super::Base64Decoder;
|
parsers::message::MessageStream,
|
||||||
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn decode_base64() {
|
fn decode_base64_strings() {
|
||||||
let inputs = [
|
let inputs = [
|
||||||
("VGVzdA==", "Test", "", true),
|
("VGVzdA==", "Test", "", true),
|
||||||
("WWU=", "Ye", "", true),
|
("WWU=", "Ye", "", true),
|
||||||
@@ -169,11 +166,20 @@ mod tests {
|
|||||||
let str = input.0.to_string();
|
let str = input.0.to_string();
|
||||||
let stream = MessageStream::new(str.as_bytes());
|
let stream = MessageStream::new(str.as_bytes());
|
||||||
|
|
||||||
let (success, result) = stream.decode_base64(input.2.as_bytes(), input.3);
|
let (bytes_read, result) = decode_base64(&stream, 0, input.2.as_bytes(), input.3);
|
||||||
assert_eq!(success, !input.1.is_empty(), "Failed for '{:?}'", input.0);
|
assert_eq!(
|
||||||
|
bytes_read > 0,
|
||||||
|
!input.1.is_empty(),
|
||||||
|
"Failed for '{:?}'",
|
||||||
|
input.0
|
||||||
|
);
|
||||||
|
|
||||||
if !input.1.is_empty() {
|
if !input.1.is_empty() {
|
||||||
let result_str = std::str::from_utf8(result.as_ref().unwrap().as_ref()).unwrap();
|
let bytes = match result {
|
||||||
|
DecodeResult::Owned(v) => v,
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
let result_str = std::str::from_utf8(&bytes).unwrap();
|
||||||
//println!("'{}' -> '{}'", input.0.escape_debug(), result_str.escape_debug());
|
//println!("'{}' -> '{}'", input.0.escape_debug(), result_str.escape_debug());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
result_str,
|
result_str,
|
||||||
|
|||||||
@@ -9,80 +9,106 @@
|
|||||||
* except according to those terms.
|
* except according to those terms.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use crate::{
|
use crate::{decoders::charsets::map::get_charset_decoder, parsers::message::MessageStream};
|
||||||
decoders::{
|
|
||||||
base64::Base64Decoder, charsets::map::get_charset_decoder,
|
use super::{
|
||||||
quoted_printable::QuotedPrintableDecoder,
|
base64::decode_base64, quoted_printable::decode_quoted_printable, DecodeFnc, DecodeResult,
|
||||||
},
|
|
||||||
parsers::message_stream::MessageStream,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn parse_encoded_word(stream: &MessageStream) -> Option<String> {
|
enum Rfc2047State {
|
||||||
if !stream.skip_byte(&b'?') {
|
Init,
|
||||||
return None;
|
Charset,
|
||||||
}
|
Encoding,
|
||||||
let charset_start = stream.get_pos();
|
Data,
|
||||||
let mut charset_end = charset_start;
|
}
|
||||||
let backup_pos = charset_start - 1;
|
|
||||||
|
|
||||||
while let Some(ch) = stream.next() {
|
pub fn decode_rfc2047(stream: &MessageStream, start_pos: usize) -> (usize, Option<String>) {
|
||||||
match ch {
|
let mut read_pos: usize = start_pos;
|
||||||
b'?' => {
|
let mut state = Rfc2047State::Init;
|
||||||
if charset_end == charset_start {
|
|
||||||
charset_end = stream.get_pos() - 1;
|
let mut charset_start = 0;
|
||||||
|
let mut charset_end = 0;
|
||||||
|
let mut decode_fnc: Option<DecodeFnc> = None;
|
||||||
|
|
||||||
|
for ch in stream.data[start_pos..].iter() {
|
||||||
|
read_pos += 1;
|
||||||
|
|
||||||
|
match state {
|
||||||
|
Rfc2047State::Init => {
|
||||||
|
if ch != &b'?' {
|
||||||
|
return (0, None);
|
||||||
}
|
}
|
||||||
break;
|
state = Rfc2047State::Charset;
|
||||||
|
charset_start = read_pos;
|
||||||
|
charset_end = read_pos;
|
||||||
}
|
}
|
||||||
b'*' => {
|
Rfc2047State::Charset => match ch {
|
||||||
if charset_end == charset_start {
|
b'?' => {
|
||||||
charset_end = stream.get_pos() - 1;
|
if charset_end == charset_start {
|
||||||
|
charset_end = read_pos - 1;
|
||||||
|
}
|
||||||
|
if (charset_end - charset_start) < 2 {
|
||||||
|
return (0, None);
|
||||||
|
}
|
||||||
|
state = Rfc2047State::Encoding;
|
||||||
|
}
|
||||||
|
b'*' => {
|
||||||
|
if charset_end == charset_start {
|
||||||
|
charset_end = read_pos - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b'\n' => {
|
||||||
|
return (0, None);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
Rfc2047State::Encoding => {
|
||||||
|
match ch {
|
||||||
|
b'q' | b'Q' => decode_fnc = Some(decode_quoted_printable),
|
||||||
|
b'b' | b'B' => decode_fnc = Some(decode_base64),
|
||||||
|
_ => {
|
||||||
|
return (0, None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
state = Rfc2047State::Data;
|
||||||
|
}
|
||||||
|
Rfc2047State::Data => {
|
||||||
|
if ch != &b'?' {
|
||||||
|
return (0, None);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
b'\n' => {
|
|
||||||
stream.set_pos(backup_pos);
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !(2..=45).contains(&(charset_end - charset_start)) {
|
if let Some(decode_fnc) = decode_fnc {
|
||||||
stream.set_pos(backup_pos);
|
if let (bytes_read @ 1..=usize::MAX, DecodeResult::Owned(bytes)) =
|
||||||
return None;
|
decode_fnc(stream, read_pos, b"?=", true)
|
||||||
}
|
{
|
||||||
|
return (
|
||||||
let (success, bytes) = match stream.next() {
|
(read_pos - start_pos) + bytes_read,
|
||||||
Some(b'q') | Some(b'Q') if stream.skip_byte(&b'?') => {
|
if let Some(decoder) = get_charset_decoder(&stream.data[charset_start..charset_end])
|
||||||
stream.decode_quoted_printable(b"?=", true)
|
{
|
||||||
|
decoder(&bytes).into()
|
||||||
|
} else {
|
||||||
|
String::from_utf8(bytes)
|
||||||
|
.unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned())
|
||||||
|
.into()
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
Some(b'b') | Some(b'B') if stream.skip_byte(&b'?') => stream.decode_base64(b"?=", true),
|
|
||||||
_ => (false, None),
|
|
||||||
};
|
|
||||||
|
|
||||||
if !success {
|
|
||||||
stream.set_pos(backup_pos);
|
|
||||||
return None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(decoder) =
|
(0, None)
|
||||||
get_charset_decoder(stream.get_bytes(charset_start, charset_end).unwrap())
|
|
||||||
{
|
|
||||||
decoder(bytes?.as_ref()).into()
|
|
||||||
} else {
|
|
||||||
String::from_utf8(bytes?.into_owned())
|
|
||||||
.unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned())
|
|
||||||
.into()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{
|
use crate::{decoders::encoded_word::decode_rfc2047, parsers::message::MessageStream};
|
||||||
decoders::encoded_word::parse_encoded_word, parsers::message_stream::MessageStream,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn decode_rfc2047() {
|
fn decode_rfc2047_string() {
|
||||||
let inputs = [
|
let inputs = [
|
||||||
(
|
(
|
||||||
"?iso-8859-1?q?this=20is=20some=20text?=".to_string(),
|
"?iso-8859-1?q?this=20is=20some=20text?=".to_string(),
|
||||||
@@ -163,12 +189,12 @@ mod tests {
|
|||||||
for input in inputs {
|
for input in inputs {
|
||||||
let str = input.0.to_string();
|
let str = input.0.to_string();
|
||||||
|
|
||||||
match parse_encoded_word(&MessageStream::new(str.as_bytes())) {
|
match decode_rfc2047(&MessageStream::new(str.as_bytes()), 0) {
|
||||||
Some(string) => {
|
(_, Some(string)) => {
|
||||||
//println!("Decoded '{}'", string);
|
//println!("Decoded '{}'", string);
|
||||||
assert_eq!(string, input.1);
|
assert_eq!(string, input.1);
|
||||||
}
|
}
|
||||||
None => panic!("Failed to decode '{}'", input.0),
|
_ => panic!("Failed to decode '{}'", input.0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,9 +9,19 @@
|
|||||||
* except according to those terms.
|
* except according to those terms.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use crate::parsers::message::MessageStream;
|
||||||
|
|
||||||
pub mod base64;
|
pub mod base64;
|
||||||
pub mod charsets;
|
pub mod charsets;
|
||||||
pub mod encoded_word;
|
pub mod encoded_word;
|
||||||
pub mod hex;
|
pub mod hex;
|
||||||
pub mod html;
|
pub mod html;
|
||||||
pub mod quoted_printable;
|
pub mod quoted_printable;
|
||||||
|
|
||||||
|
pub type DecodeFnc<'x> = fn(&MessageStream<'x>, usize, &[u8], bool) -> (usize, DecodeResult);
|
||||||
|
|
||||||
|
pub enum DecodeResult {
|
||||||
|
Owned(Vec<u8>),
|
||||||
|
Borrowed((usize, usize)),
|
||||||
|
Empty,
|
||||||
|
}
|
||||||
|
|||||||
+134
-134
@@ -9,9 +9,9 @@
|
|||||||
* except according to those terms.
|
* except according to those terms.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use crate::parsers::message::MessageStream;
|
||||||
|
|
||||||
use crate::parsers::message_stream::MessageStream;
|
use super::DecodeResult;
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
enum QuotedPrintableState {
|
enum QuotedPrintableState {
|
||||||
@@ -20,162 +20,152 @@ enum QuotedPrintableState {
|
|||||||
Hex1,
|
Hex1,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait QuotedPrintableDecoder<'x> {
|
pub fn decode_quoted_printable<'x>(
|
||||||
fn decode_quoted_printable(
|
stream: &MessageStream<'x>,
|
||||||
&self,
|
start_pos: usize,
|
||||||
boundary: &[u8],
|
boundary: &[u8],
|
||||||
is_word: bool,
|
is_word: bool,
|
||||||
) -> (bool, Option<Cow<'x, [u8]>>);
|
) -> (usize, DecodeResult) {
|
||||||
}
|
let mut success = boundary.is_empty();
|
||||||
|
|
||||||
impl<'x> QuotedPrintableDecoder<'x> for MessageStream<'x> {
|
let mut bytes_read = 0;
|
||||||
fn decode_quoted_printable(
|
|
||||||
&self,
|
|
||||||
boundary: &[u8],
|
|
||||||
is_word: bool,
|
|
||||||
) -> (bool, Option<Cow<'x, [u8]>>) {
|
|
||||||
let mut success = boundary.is_empty();
|
|
||||||
|
|
||||||
let start_pos = self.pos.get();
|
let mut buf = Vec::with_capacity(stream.data.len() - start_pos);
|
||||||
let mut read_pos = start_pos;
|
|
||||||
|
|
||||||
let mut buf = Vec::with_capacity(self.data.len() - read_pos);
|
let mut state = QuotedPrintableState::None;
|
||||||
|
let mut match_count = 0;
|
||||||
|
let mut hex1 = 0;
|
||||||
|
|
||||||
let mut state = QuotedPrintableState::None;
|
debug_assert!(boundary.is_empty() || boundary.len() >= 2);
|
||||||
let mut match_count = 0;
|
|
||||||
let mut hex1 = 0;
|
|
||||||
|
|
||||||
debug_assert!(boundary.is_empty() || boundary.len() >= 2);
|
for ch in stream.data[start_pos..].iter() {
|
||||||
|
bytes_read += 1;
|
||||||
|
|
||||||
for ch in self.data[read_pos..].iter() {
|
// if success is false, a boundary was provided
|
||||||
read_pos += 1;
|
if !success {
|
||||||
|
let mut is_boundary_end = true;
|
||||||
|
|
||||||
// if success is false, a boundary was provided
|
if *ch == boundary[match_count] {
|
||||||
if !success {
|
match_count += 1;
|
||||||
let mut is_boundary_end = true;
|
if match_count == boundary.len() {
|
||||||
|
let done = if is_word {
|
||||||
if *ch == boundary[match_count] {
|
true
|
||||||
match_count += 1;
|
|
||||||
if match_count == boundary.len() {
|
|
||||||
let done = if is_word {
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
is_boundary_end = self.is_boundary_end(read_pos);
|
|
||||||
is_boundary_end
|
|
||||||
};
|
|
||||||
|
|
||||||
if done {
|
|
||||||
success = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
continue;
|
is_boundary_end =
|
||||||
}
|
crate::parsers::mime::is_boundary_end(stream, start_pos + bytes_read);
|
||||||
}
|
is_boundary_end
|
||||||
|
};
|
||||||
|
|
||||||
if match_count > 0 {
|
if done {
|
||||||
for ch in boundary[..match_count].iter() {
|
success = true;
|
||||||
if *ch != b'\n' || QuotedPrintableState::Eq != state {
|
break;
|
||||||
buf.push(*ch);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
state = QuotedPrintableState::None;
|
|
||||||
|
|
||||||
// is_boundary_end is always true except in the rare event
|
|
||||||
// that a boundary was found without a proper MIME ending (-- or \r\n)
|
|
||||||
if is_boundary_end {
|
|
||||||
if *ch == boundary[0] {
|
|
||||||
// Char matched beginning of boundary, get next char.
|
|
||||||
match_count = 1;
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
// Reset match count and decode character
|
|
||||||
match_count = 0;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// There was a full boundary match but without a proper MIME
|
|
||||||
// ending, reset match count and continue.
|
|
||||||
match_count = 0;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match ch {
|
if match_count > 0 {
|
||||||
b'=' => {
|
for ch in boundary[..match_count].iter() {
|
||||||
if let QuotedPrintableState::None = state {
|
if *ch != b'\n' || QuotedPrintableState::Eq != state {
|
||||||
state = QuotedPrintableState::Eq
|
|
||||||
} else {
|
|
||||||
success = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
b'\n' => {
|
|
||||||
if is_word {
|
|
||||||
success = false;
|
|
||||||
break;
|
|
||||||
} else if QuotedPrintableState::Eq == state {
|
|
||||||
state = QuotedPrintableState::None;
|
|
||||||
} else {
|
|
||||||
buf.push(b'\n');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
b'_' if is_word => {
|
|
||||||
buf.push(b' ');
|
|
||||||
}
|
|
||||||
b'\r' => (),
|
|
||||||
_ => match state {
|
|
||||||
QuotedPrintableState::None => {
|
|
||||||
buf.push(*ch);
|
buf.push(*ch);
|
||||||
}
|
}
|
||||||
QuotedPrintableState::Eq => {
|
}
|
||||||
hex1 = HEX_MAP[*ch as usize];
|
state = QuotedPrintableState::None;
|
||||||
if hex1 != -1 {
|
|
||||||
state = QuotedPrintableState::Hex1;
|
|
||||||
} else {
|
|
||||||
success = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
QuotedPrintableState::Hex1 => {
|
|
||||||
let hex2 = HEX_MAP[*ch as usize];
|
|
||||||
|
|
||||||
state = QuotedPrintableState::None;
|
// is_boundary_end is always true except in the rare event
|
||||||
if hex2 != -1 {
|
// that a boundary was found without a proper MIME ending (-- or \r\n)
|
||||||
let ch = ((hex1 as u8) << 4) | hex2 as u8;
|
if is_boundary_end {
|
||||||
|
if *ch == boundary[0] {
|
||||||
buf.push(ch);
|
// Char matched beginning of boundary, get next char.
|
||||||
} else {
|
match_count = 1;
|
||||||
success = false;
|
continue;
|
||||||
break;
|
} else {
|
||||||
}
|
// Reset match count and decode character
|
||||||
|
match_count = 0;
|
||||||
}
|
}
|
||||||
},
|
} else {
|
||||||
|
// There was a full boundary match but without a proper MIME
|
||||||
|
// ending, reset match count and continue.
|
||||||
|
match_count = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.pos.set(read_pos);
|
match ch {
|
||||||
|
b'=' => {
|
||||||
|
if let QuotedPrintableState::None = state {
|
||||||
|
state = QuotedPrintableState::Eq
|
||||||
|
} else {
|
||||||
|
success = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b'\n' => {
|
||||||
|
if is_word {
|
||||||
|
success = false;
|
||||||
|
break;
|
||||||
|
} else if QuotedPrintableState::Eq == state {
|
||||||
|
state = QuotedPrintableState::None;
|
||||||
|
} else {
|
||||||
|
buf.push(b'\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b'_' if is_word => {
|
||||||
|
buf.push(b' ');
|
||||||
|
}
|
||||||
|
b'\r' => (),
|
||||||
|
_ => match state {
|
||||||
|
QuotedPrintableState::None => {
|
||||||
|
buf.push(*ch);
|
||||||
|
}
|
||||||
|
QuotedPrintableState::Eq => {
|
||||||
|
hex1 = HEX_MAP[*ch as usize];
|
||||||
|
if hex1 != -1 {
|
||||||
|
state = QuotedPrintableState::Hex1;
|
||||||
|
} else {
|
||||||
|
success = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
QuotedPrintableState::Hex1 => {
|
||||||
|
let hex2 = HEX_MAP[*ch as usize];
|
||||||
|
|
||||||
(
|
state = QuotedPrintableState::None;
|
||||||
success,
|
if hex2 != -1 {
|
||||||
if !buf.is_empty() {
|
let ch = ((hex1 as u8) << 4) | hex2 as u8;
|
||||||
buf.shrink_to_fit();
|
|
||||||
Some(buf.into())
|
buf.push(ch);
|
||||||
} else {
|
} else {
|
||||||
None
|
success = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
(
|
||||||
|
if success { bytes_read } else { 0 },
|
||||||
|
if !buf.is_empty() {
|
||||||
|
buf.shrink_to_fit();
|
||||||
|
DecodeResult::Owned(buf)
|
||||||
|
} else {
|
||||||
|
DecodeResult::Empty
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{
|
use crate::{
|
||||||
decoders::quoted_printable::QuotedPrintableDecoder, parsers::message_stream::MessageStream,
|
decoders::{quoted_printable::decode_quoted_printable, DecodeResult},
|
||||||
|
parsers::message::MessageStream,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn decode_quoted_printable() {
|
fn decode_quoted_printable_string() {
|
||||||
let inputs = [
|
let inputs = [
|
||||||
(
|
(
|
||||||
concat!(
|
concat!(
|
||||||
@@ -248,12 +238,22 @@ mod tests {
|
|||||||
let str = input.0.to_string();
|
let str = input.0.to_string();
|
||||||
let stream = MessageStream::new(str.as_bytes());
|
let stream = MessageStream::new(str.as_bytes());
|
||||||
|
|
||||||
let (success, result) = stream.decode_quoted_printable(input.2.as_bytes(), input.3);
|
let (bytes_read, result) =
|
||||||
|
decode_quoted_printable(&stream, 0, input.2.as_bytes(), input.3);
|
||||||
|
|
||||||
assert_eq!(success, !input.1.is_empty(), "Failed for '{:?}'", input.0);
|
assert_eq!(
|
||||||
|
bytes_read > 0,
|
||||||
|
!input.1.is_empty(),
|
||||||
|
"Failed for '{:?}'",
|
||||||
|
input.0
|
||||||
|
);
|
||||||
|
|
||||||
if !input.1.is_empty() {
|
if !input.1.is_empty() {
|
||||||
let result_str = std::str::from_utf8(result.as_ref().unwrap()).unwrap();
|
let bytes = match result {
|
||||||
|
DecodeResult::Owned(v) => v,
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
let result_str = std::str::from_utf8(&bytes).unwrap();
|
||||||
|
|
||||||
/*println!(
|
/*println!(
|
||||||
"Decoded '{}'\n -> to ->\n'{}'\n{}",
|
"Decoded '{}'\n -> to ->\n'{}'\n{}",
|
||||||
@@ -274,13 +274,13 @@ mod tests {
|
|||||||
// MIRI overflow tests
|
// MIRI overflow tests
|
||||||
for input in [b"\x0a\x0a\x00\x0b".to_vec(), b":B\x0a%".to_vec()] {
|
for input in [b"\x0a\x0a\x00\x0b".to_vec(), b":B\x0a%".to_vec()] {
|
||||||
let stream = MessageStream::new(&input[..]);
|
let stream = MessageStream::new(&input[..]);
|
||||||
stream.decode_quoted_printable(b"\n\n", true);
|
decode_quoted_printable(&stream, 0, b"\n\n", true);
|
||||||
let stream = MessageStream::new(&input[..]);
|
let stream = MessageStream::new(&input[..]);
|
||||||
stream.decode_quoted_printable(b"\n\n", false);
|
decode_quoted_printable(&stream, 0, b"\n\n", false);
|
||||||
let stream = MessageStream::new(&input[..]);
|
let stream = MessageStream::new(&input[..]);
|
||||||
stream.decode_quoted_printable(&[], true);
|
decode_quoted_printable(&stream, 0, &[], true);
|
||||||
let stream = MessageStream::new(&input[..]);
|
let stream = MessageStream::new(&input[..]);
|
||||||
stream.decode_quoted_printable(&[], false);
|
decode_quoted_printable(&stream, 0, &[], false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -29,9 +29,9 @@
|
|||||||
//!
|
//!
|
||||||
//! Performance and memory safety were two important factors while designing _mail-parser_:
|
//! Performance and memory safety were two important factors while designing _mail-parser_:
|
||||||
//!
|
//!
|
||||||
//! - **Zero-copy parsing**: Practically all strings returned by this library are `Cow<str>` references to the input raw message.
|
//! - **Zero-copy**: Practically all strings returned by this library are `Cow<str>` references to the input raw message.
|
||||||
//! - **High performance Base64 decoding** based on Chromium's decoder ([the fastest non-SIMD decoder](https://github.com/lemire/fastbase64)).
|
//! - **High performance Base64 decoding** based on Chromium's decoder ([the fastest non-SIMD decoder](https://github.com/lemire/fastbase64)).
|
||||||
//! - **Fast** parsing of message header fields, character set names and HTML entities using [perfect hashing](https://en.wikipedia.org/wiki/Perfect_hash_function).
|
//! - **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.
|
//! - Written in **100% safe** Rust with no external dependencies.
|
||||||
//! - Every function in the library has been [fuzzed](#testing-fuzzing--benchmarking) and
|
//! - Every function in the library has been [fuzzed](#testing-fuzzing--benchmarking) and
|
||||||
//! meticulously [tested with MIRI](#testing-fuzzing--benchmarking).
|
//! meticulously [tested with MIRI](#testing-fuzzing--benchmarking).
|
||||||
@@ -255,7 +255,7 @@
|
|||||||
//! println!("{}", serde_json::to_string_pretty(&message).unwrap());
|
//! println!("{}", serde_json::to_string_pretty(&message).unwrap());
|
||||||
//! println!("{}", serde_yaml::to_string(&message).unwrap());
|
//! println!("{}", serde_yaml::to_string(&message).unwrap());
|
||||||
//!```
|
//!```
|
||||||
|
#[forbid(unsafe_code)]
|
||||||
pub mod decoders;
|
pub mod decoders;
|
||||||
pub mod parsers;
|
pub mod parsers;
|
||||||
|
|
||||||
|
|||||||
@@ -12,8 +12,7 @@
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
decoders::encoded_word::parse_encoded_word, parsers::message_stream::MessageStream, Addr,
|
decoders::encoded_word::decode_rfc2047, parsers::message::MessageStream, Addr, Address, Group,
|
||||||
Address, Group,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
impl<'x> Default for Address<'x> {
|
impl<'x> Default for Address<'x> {
|
||||||
@@ -63,9 +62,7 @@ pub fn add_token<'x>(
|
|||||||
add_trail_space: bool,
|
add_trail_space: bool,
|
||||||
) {
|
) {
|
||||||
if parser.token_start > 0 {
|
if parser.token_start > 0 {
|
||||||
let token = stream
|
let token = String::from_utf8_lossy(&stream.data[parser.token_start - 1..parser.token_end]);
|
||||||
.get_string(parser.token_start - 1, parser.token_end)
|
|
||||||
.unwrap();
|
|
||||||
let mut add_space = false;
|
let mut add_space = false;
|
||||||
let list = match parser.state {
|
let list = match parser.state {
|
||||||
AddressState::Address => &mut parser.mail_tokens,
|
AddressState::Address => &mut parser.mail_tokens,
|
||||||
@@ -227,7 +224,7 @@ pub fn add_group(parser: &mut AddressParser) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
pub fn parse_address<'x>(stream: &mut MessageStream<'x>) -> Address<'x> {
|
||||||
let mut parser = AddressParser {
|
let mut parser = AddressParser {
|
||||||
token_start: 0,
|
token_start: 0,
|
||||||
token_end: 0,
|
token_end: 0,
|
||||||
@@ -249,13 +246,18 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
|||||||
result: Vec::new(),
|
result: Vec::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
while let Some(ch) = stream.next() {
|
let mut iter = stream.data[stream.pos..].iter();
|
||||||
|
|
||||||
|
while let Some(ch) = iter.next() {
|
||||||
|
stream.pos += 1;
|
||||||
|
|
||||||
match ch {
|
match ch {
|
||||||
b'\n' => {
|
b'\n' => {
|
||||||
add_token(&mut parser, stream, false);
|
add_token(&mut parser, stream, false);
|
||||||
match stream.peek() {
|
match stream.data.get(stream.pos) {
|
||||||
Some(b' ' | b'\t') => {
|
Some(b' ' | b'\t') => {
|
||||||
stream.advance(1);
|
iter.next();
|
||||||
|
stream.pos += 1;
|
||||||
if !parser.is_token_start {
|
if !parser.is_token_start {
|
||||||
parser.is_token_start = true;
|
parser.is_token_start = true;
|
||||||
}
|
}
|
||||||
@@ -267,7 +269,7 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
|||||||
b'\\' if parser.state != AddressState::Name => {
|
b'\\' if parser.state != AddressState::Name => {
|
||||||
if parser.token_start > 0 {
|
if parser.token_start > 0 {
|
||||||
if parser.state == AddressState::Quote {
|
if parser.state == AddressState::Quote {
|
||||||
parser.token_end = stream.get_pos() - 1;
|
parser.token_end = stream.pos - 1;
|
||||||
}
|
}
|
||||||
add_token(&mut parser, stream, false);
|
add_token(&mut parser, stream, false);
|
||||||
}
|
}
|
||||||
@@ -308,7 +310,7 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
|||||||
parser.is_token_email = true;
|
parser.is_token_email = true;
|
||||||
}
|
}
|
||||||
b'=' if parser.is_token_start && !parser.is_escaped => {
|
b'=' if parser.is_token_start && !parser.is_escaped => {
|
||||||
if let Some(token) = parse_encoded_word(stream) {
|
if let (d_bytes_read, Some(token)) = decode_rfc2047(stream, stream.pos) {
|
||||||
let add_space = parser.state != AddressState::Quote; // Make borrow-checker happy
|
let add_space = parser.state != AddressState::Quote; // Make borrow-checker happy
|
||||||
add_token(&mut parser, stream, add_space);
|
add_token(&mut parser, stream, add_space);
|
||||||
(if parser.state != AddressState::Comment {
|
(if parser.state != AddressState::Comment {
|
||||||
@@ -317,6 +319,8 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
|||||||
&mut parser.comment_tokens
|
&mut parser.comment_tokens
|
||||||
})
|
})
|
||||||
.push(token.into());
|
.push(token.into());
|
||||||
|
stream.pos += d_bytes_read;
|
||||||
|
iter = stream.data[stream.pos..].iter();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -329,10 +333,10 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
|||||||
}
|
}
|
||||||
if parser.state == AddressState::Quote {
|
if parser.state == AddressState::Quote {
|
||||||
if parser.token_start == 0 {
|
if parser.token_start == 0 {
|
||||||
parser.token_start = stream.get_pos();
|
parser.token_start = stream.pos;
|
||||||
parser.token_end = parser.token_start;
|
parser.token_end = parser.token_start;
|
||||||
} else {
|
} else {
|
||||||
parser.token_end = stream.get_pos();
|
parser.token_end = stream.pos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
@@ -378,10 +382,10 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if parser.token_start == 0 {
|
if parser.token_start == 0 {
|
||||||
parser.token_start = stream.get_pos();
|
parser.token_start = stream.pos;
|
||||||
parser.token_end = parser.token_start;
|
parser.token_end = parser.token_start;
|
||||||
} else {
|
} else {
|
||||||
parser.token_end = stream.get_pos();
|
parser.token_end = stream.pos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -408,7 +412,7 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
|||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_addresses() {
|
fn parse_addresses() {
|
||||||
use crate::parsers::{fields::address::parse_address, message_stream::MessageStream};
|
use crate::parsers::fields::address::parse_address;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
@@ -931,7 +935,7 @@ mod tests {
|
|||||||
for input in inputs {
|
for input in inputs {
|
||||||
println!("Testiong {}", input.0);
|
println!("Testiong {}", input.0);
|
||||||
let str = input.0.to_string();
|
let str = input.0.to_string();
|
||||||
let result = parse_address(&MessageStream::new(str.as_bytes()));
|
let result = parse_address(&mut MessageStream::new(str.as_bytes()));
|
||||||
let expected: Address = serde_yaml::from_str(input.1).unwrap_or(Address::Empty);
|
let expected: Address = serde_yaml::from_str(input.1).unwrap_or(Address::Empty);
|
||||||
|
|
||||||
/*if input.0.len() >= 70 {
|
/*if input.0.len() >= 70 {
|
||||||
|
|||||||
@@ -15,10 +15,8 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
decoders::{
|
decoders::{charsets::map::get_charset_decoder, encoded_word::decode_rfc2047, hex::decode_hex},
|
||||||
charsets::map::get_charset_decoder, encoded_word::parse_encoded_word, hex::decode_hex,
|
parsers::message::MessageStream,
|
||||||
},
|
|
||||||
parsers::message_stream::MessageStream,
|
|
||||||
ContentType,
|
ContentType,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -67,7 +65,9 @@ fn reset_parser(parser: &mut ContentTypeParser) {
|
|||||||
|
|
||||||
fn add_attribute<'x>(parser: &mut ContentTypeParser<'x>, stream: &MessageStream<'x>) -> bool {
|
fn add_attribute<'x>(parser: &mut ContentTypeParser<'x>, stream: &MessageStream<'x>) -> bool {
|
||||||
if parser.token_start > 0 {
|
if parser.token_start > 0 {
|
||||||
let mut attr = stream.get_string(parser.token_start - 1, parser.token_end);
|
let mut attr = Some(String::from_utf8_lossy(
|
||||||
|
&stream.data[parser.token_start - 1..parser.token_end],
|
||||||
|
));
|
||||||
|
|
||||||
if !parser.is_lower_case {
|
if !parser.is_lower_case {
|
||||||
attr.as_mut().unwrap().to_mut().make_ascii_lowercase();
|
attr.as_mut().unwrap().to_mut().make_ascii_lowercase();
|
||||||
@@ -90,9 +90,8 @@ fn add_attribute<'x>(parser: &mut ContentTypeParser<'x>, stream: &MessageStream<
|
|||||||
|
|
||||||
fn add_attribute_parameter<'x>(parser: &mut ContentTypeParser<'x>, stream: &MessageStream<'x>) {
|
fn add_attribute_parameter<'x>(parser: &mut ContentTypeParser<'x>, stream: &MessageStream<'x>) {
|
||||||
if parser.token_start > 0 {
|
if parser.token_start > 0 {
|
||||||
let attr_part = stream
|
let attr_part =
|
||||||
.get_string(parser.token_start - 1, parser.token_end)
|
String::from_utf8_lossy(&stream.data[parser.token_start - 1..parser.token_end]);
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
if parser.attr_charset.is_none() {
|
if parser.attr_charset.is_none() {
|
||||||
parser.attr_charset = attr_part.into();
|
parser.attr_charset = attr_part.into();
|
||||||
@@ -122,18 +121,13 @@ fn add_partial_value<'x>(
|
|||||||
if parser.token_start > 0 {
|
if parser.token_start > 0 {
|
||||||
let in_quote = parser.state == ContentState::AttributeQuotedValue;
|
let in_quote = parser.state == ContentState::AttributeQuotedValue;
|
||||||
|
|
||||||
parser.values.push(
|
parser.values.push(String::from_utf8_lossy(
|
||||||
stream
|
&stream.data[parser.token_start - 1..if in_quote && to_cur_pos {
|
||||||
.get_string(
|
stream.pos - 1
|
||||||
parser.token_start - 1,
|
} else {
|
||||||
if in_quote && to_cur_pos {
|
parser.token_end
|
||||||
stream.get_pos() - 1
|
}],
|
||||||
} else {
|
));
|
||||||
parser.token_end
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
);
|
|
||||||
if !in_quote {
|
if !in_quote {
|
||||||
parser.values.push(" ".into());
|
parser.values.push(" ".into());
|
||||||
}
|
}
|
||||||
@@ -149,7 +143,9 @@ fn add_value<'x>(parser: &mut ContentTypeParser<'x>, stream: &MessageStream<'x>)
|
|||||||
|
|
||||||
let has_values = !parser.values.is_empty();
|
let has_values = !parser.values.is_empty();
|
||||||
let value = if parser.token_start > 0 {
|
let value = if parser.token_start > 0 {
|
||||||
stream.get_string(parser.token_start - 1, parser.token_end)
|
Some(String::from_utf8_lossy(
|
||||||
|
&stream.data[parser.token_start - 1..parser.token_end],
|
||||||
|
))
|
||||||
} else {
|
} else {
|
||||||
if !has_values {
|
if !has_values {
|
||||||
return;
|
return;
|
||||||
@@ -223,11 +219,10 @@ fn add_value<'x>(parser: &mut ContentTypeParser<'x>, stream: &MessageStream<'x>)
|
|||||||
|
|
||||||
fn add_attr_position(parser: &mut ContentTypeParser, stream: &MessageStream) -> bool {
|
fn add_attr_position(parser: &mut ContentTypeParser, stream: &MessageStream) -> bool {
|
||||||
if parser.token_start > 0 {
|
if parser.token_start > 0 {
|
||||||
parser.attr_position = stream
|
parser.attr_position =
|
||||||
.get_string(parser.token_start - 1, parser.token_end)
|
String::from_utf8_lossy(&stream.data[parser.token_start - 1..parser.token_end])
|
||||||
.unwrap()
|
.parse()
|
||||||
.parse()
|
.unwrap_or(0);
|
||||||
.unwrap_or(0);
|
|
||||||
|
|
||||||
reset_parser(parser);
|
reset_parser(parser);
|
||||||
true
|
true
|
||||||
@@ -249,7 +244,7 @@ fn merge_continuations(parser: &mut ContentTypeParser) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<'x>> {
|
pub fn parse_content_type<'x>(stream: &mut MessageStream<'x>) -> Option<ContentType<'x>> {
|
||||||
let mut parser = ContentTypeParser {
|
let mut parser = ContentTypeParser {
|
||||||
state: ContentState::Type,
|
state: ContentState::Type,
|
||||||
state_stack: Vec::new(),
|
state_stack: Vec::new(),
|
||||||
@@ -275,7 +270,10 @@ pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<
|
|||||||
token_end: 0,
|
token_end: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
while let Some(ch) = stream.next() {
|
let mut iter = stream.data[stream.pos..].iter();
|
||||||
|
|
||||||
|
while let Some(ch) = iter.next() {
|
||||||
|
stream.pos += 1;
|
||||||
match ch {
|
match ch {
|
||||||
b' ' | b'\t' => {
|
b' ' | b'\t' => {
|
||||||
if !parser.is_token_start {
|
if !parser.is_token_start {
|
||||||
@@ -283,10 +281,10 @@ pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<
|
|||||||
}
|
}
|
||||||
if let ContentState::AttributeQuotedValue = parser.state {
|
if let ContentState::AttributeQuotedValue = parser.state {
|
||||||
if parser.token_start == 0 {
|
if parser.token_start == 0 {
|
||||||
parser.token_start = stream.get_pos();
|
parser.token_start = stream.pos;
|
||||||
parser.token_end = parser.token_start;
|
parser.token_end = parser.token_start;
|
||||||
} else {
|
} else {
|
||||||
parser.token_end = stream.get_pos();
|
parser.token_end = stream.pos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
@@ -312,10 +310,11 @@ pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<
|
|||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
match stream.peek() {
|
match stream.data.get(stream.pos) {
|
||||||
Some(b' ' | b'\t') => {
|
Some(b' ' | b'\t') => {
|
||||||
parser.state = ContentState::AttributeName;
|
parser.state = ContentState::AttributeName;
|
||||||
stream.advance(1);
|
stream.pos += 1;
|
||||||
|
iter.next();
|
||||||
|
|
||||||
if !parser.is_token_start {
|
if !parser.is_token_start {
|
||||||
parser.is_token_start = true;
|
parser.is_token_start = true;
|
||||||
@@ -326,6 +325,7 @@ pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<
|
|||||||
if parser.continuations.is_some() {
|
if parser.continuations.is_some() {
|
||||||
merge_continuations(&mut parser);
|
merge_continuations(&mut parser);
|
||||||
}
|
}
|
||||||
|
|
||||||
return if let Some(content_type) = parser.c_type {
|
return if let Some(content_type) = parser.c_type {
|
||||||
Some(ContentType {
|
Some(ContentType {
|
||||||
c_type: content_type,
|
c_type: content_type,
|
||||||
@@ -395,9 +395,11 @@ pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<
|
|||||||
ContentState::AttributeValue | ContentState::AttributeQuotedValue
|
ContentState::AttributeValue | ContentState::AttributeQuotedValue
|
||||||
if parser.is_token_start =>
|
if parser.is_token_start =>
|
||||||
{
|
{
|
||||||
if let Some(token) = parse_encoded_word(stream) {
|
if let (d_bytes_read, Some(token)) = decode_rfc2047(stream, stream.pos) {
|
||||||
add_partial_value(&mut parser, stream, false);
|
add_partial_value(&mut parser, stream, false);
|
||||||
parser.values.push(token.into());
|
parser.values.push(token.into());
|
||||||
|
stream.pos += d_bytes_read;
|
||||||
|
iter = stream.data[stream.pos..].iter();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -487,20 +489,23 @@ pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<
|
|||||||
}
|
}
|
||||||
|
|
||||||
if parser.token_start == 0 {
|
if parser.token_start == 0 {
|
||||||
parser.token_start = stream.get_pos();
|
parser.token_start = stream.pos;
|
||||||
parser.token_end = parser.token_start;
|
parser.token_end = parser.token_start;
|
||||||
} else {
|
} else {
|
||||||
parser.token_end = stream.get_pos();
|
parser.token_end = stream.pos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use crate::parsers::message::MessageStream;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_content_fields() {
|
fn parse_content_fields() {
|
||||||
use crate::parsers::{fields::content_type::ContentType, message_stream::MessageStream};
|
use crate::parsers::fields::content_type::ContentType;
|
||||||
|
|
||||||
use super::parse_content_type;
|
use super::parse_content_type;
|
||||||
|
|
||||||
@@ -1329,7 +1334,7 @@ mod tests {
|
|||||||
for input in inputs {
|
for input in inputs {
|
||||||
let str = input.0.to_string();
|
let str = input.0.to_string();
|
||||||
|
|
||||||
let result = parse_content_type(&MessageStream::new(str.as_bytes()));
|
let result = parse_content_type(&mut MessageStream::new(str.as_bytes()));
|
||||||
let expected: Option<ContentType> = serde_yaml::from_str(input.1).unwrap_or(None);
|
let expected: Option<ContentType> = serde_yaml::from_str(input.1).unwrap_or(None);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
+16
-14
@@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use crate::{parsers::message_stream::MessageStream, DateTime};
|
use crate::{parsers::message::MessageStream, DateTime};
|
||||||
|
|
||||||
impl DateTime {
|
impl DateTime {
|
||||||
/// Returns an ISO-8601 representation of the parsed RFC5322 datetime field
|
/// Returns an ISO-8601 representation of the parsed RFC5322 datetime field
|
||||||
@@ -37,7 +37,7 @@ impl fmt::Display for DateTime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_date(stream: &MessageStream, abort_on_invalid: bool) -> Option<DateTime> {
|
pub fn parse_date(stream: &mut MessageStream) -> Option<DateTime> {
|
||||||
let mut pos = 0;
|
let mut pos = 0;
|
||||||
let mut parts = [0u32; 7];
|
let mut parts = [0u32; 7];
|
||||||
let mut parts_sizes = [
|
let mut parts_sizes = [
|
||||||
@@ -57,13 +57,18 @@ pub fn parse_date(stream: &MessageStream, abort_on_invalid: bool) -> Option<Date
|
|||||||
let mut ignore = true;
|
let mut ignore = true;
|
||||||
let mut comment_count = 0;
|
let mut comment_count = 0;
|
||||||
|
|
||||||
while let Some(ch) = stream.next() {
|
let mut iter = stream.data[stream.pos..].iter();
|
||||||
|
|
||||||
|
while let Some(ch) = iter.next() {
|
||||||
let mut next_part = false;
|
let mut next_part = false;
|
||||||
|
stream.pos += 1;
|
||||||
|
|
||||||
match ch {
|
match ch {
|
||||||
b'\n' => match stream.peek() {
|
b'\n' => match stream.data.get(stream.pos) {
|
||||||
Some(b' ' | b'\t') => {
|
Some(b' ' | b'\t') => {
|
||||||
stream.advance(1);
|
iter.next();
|
||||||
|
stream.pos += 1;
|
||||||
|
|
||||||
if !is_new_token && !ignore && comment_count == 0 {
|
if !is_new_token && !ignore && comment_count == 0 {
|
||||||
next_part = true;
|
next_part = true;
|
||||||
} else {
|
} else {
|
||||||
@@ -78,7 +83,9 @@ pub fn parse_date(stream: &MessageStream, abort_on_invalid: bool) -> Option<Date
|
|||||||
} else if *ch == b'(' {
|
} else if *ch == b'(' {
|
||||||
comment_count += 1;
|
comment_count += 1;
|
||||||
} else if *ch == b'\\' {
|
} else if *ch == b'\\' {
|
||||||
stream.skip_byte(&b')');
|
if let Some(b')') = stream.data.get(stream.pos) {
|
||||||
|
stream.pos += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -131,12 +138,7 @@ pub fn parse_date(stream: &MessageStream, abort_on_invalid: bool) -> Option<Date
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
b',' | b'\r' => (),
|
b',' | b'\r' => (),
|
||||||
_ => {
|
_ => (),
|
||||||
if abort_on_invalid {
|
|
||||||
stream.rewind(1);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if next_part {
|
if next_part {
|
||||||
@@ -193,7 +195,7 @@ pub static MONTH_MAP: &[u8; 31] = &[
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::parsers::{fields::date::parse_date, message_stream::MessageStream};
|
use crate::parsers::{fields::date::parse_date, message::MessageStream};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_dates() {
|
fn parse_dates() {
|
||||||
@@ -259,7 +261,7 @@ mod tests {
|
|||||||
|
|
||||||
for input in inputs {
|
for input in inputs {
|
||||||
let str = input.0.to_string();
|
let str = input.0.to_string();
|
||||||
match parse_date(&MessageStream::new(str.as_bytes()), false) {
|
match parse_date(&mut MessageStream::new(str.as_bytes())) {
|
||||||
Some(date) => {
|
Some(date) => {
|
||||||
//println!("{} -> {}", input.0.escape_debug(), date.to_iso8601());
|
//println!("{} -> {}", input.0.escape_debug(), date.to_iso8601());
|
||||||
assert_eq!(input.1, date.to_iso8601());
|
assert_eq!(input.1, date.to_iso8601());
|
||||||
|
|||||||
+21
-11
@@ -11,22 +11,28 @@
|
|||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use crate::parsers::message_stream::MessageStream;
|
use crate::parsers::message::MessageStream;
|
||||||
|
|
||||||
pub fn parse_id<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'x, str>>> {
|
pub fn parse_id<'x>(stream: &mut MessageStream<'x>) -> Option<Vec<Cow<'x, str>>> {
|
||||||
let mut token_start: usize = 0;
|
let mut token_start: usize = 0;
|
||||||
let mut token_end: usize = 0;
|
let mut token_end: usize = 0;
|
||||||
let mut is_id_part = false;
|
let mut is_id_part = false;
|
||||||
let mut ids = Vec::new();
|
let mut ids = Vec::new();
|
||||||
|
|
||||||
while let Some(ch) = stream.next() {
|
let mut iter = stream.data[stream.pos..].iter();
|
||||||
|
|
||||||
|
while let Some(ch) = iter.next() {
|
||||||
|
stream.pos += 1;
|
||||||
match ch {
|
match ch {
|
||||||
b'\n' => match stream.peek() {
|
b'\n' => match stream.data.get(stream.pos) {
|
||||||
Some(b' ' | b'\t') => {
|
Some(b' ' | b'\t') => {
|
||||||
stream.advance(1);
|
iter.next();
|
||||||
|
stream.pos += 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
_ => return if !ids.is_empty() { Some(ids) } else { None },
|
_ => {
|
||||||
|
return if !ids.is_empty() { Some(ids) } else { None };
|
||||||
|
}
|
||||||
},
|
},
|
||||||
b'<' => {
|
b'<' => {
|
||||||
is_id_part = true;
|
is_id_part = true;
|
||||||
@@ -35,7 +41,9 @@ pub fn parse_id<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'x, str>>> {
|
|||||||
b'>' => {
|
b'>' => {
|
||||||
is_id_part = false;
|
is_id_part = false;
|
||||||
if token_start > 0 {
|
if token_start > 0 {
|
||||||
ids.push(stream.get_string(token_start - 1, token_end).unwrap());
|
ids.push(String::from_utf8_lossy(
|
||||||
|
&stream.data[token_start - 1..token_end],
|
||||||
|
));
|
||||||
token_start = 0;
|
token_start = 0;
|
||||||
} else {
|
} else {
|
||||||
continue;
|
continue;
|
||||||
@@ -46,17 +54,19 @@ pub fn parse_id<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'x, str>>> {
|
|||||||
}
|
}
|
||||||
if is_id_part {
|
if is_id_part {
|
||||||
if token_start == 0 {
|
if token_start == 0 {
|
||||||
token_start = stream.get_pos();
|
token_start = stream.pos;
|
||||||
}
|
}
|
||||||
token_end = stream.get_pos();
|
token_end = stream.pos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::parsers::{fields::id::parse_id, message_stream::MessageStream};
|
use crate::parsers::fields::id::parse_id;
|
||||||
|
use crate::parsers::message::MessageStream;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_message_ids() {
|
fn parse_message_ids() {
|
||||||
@@ -95,7 +105,7 @@ mod tests {
|
|||||||
let str = input.0.to_string();
|
let str = input.0.to_string();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
input.1,
|
input.1,
|
||||||
parse_id(&MessageStream::new(str.as_bytes())).unwrap(),
|
parse_id(&mut MessageStream::new(str.as_bytes())).unwrap(),
|
||||||
"Failed to parse '{:?}'",
|
"Failed to parse '{:?}'",
|
||||||
input.0
|
input.0
|
||||||
);
|
);
|
||||||
|
|||||||
+21
-15
@@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use crate::{decoders::encoded_word::parse_encoded_word, parsers::message_stream::MessageStream};
|
use crate::{decoders::encoded_word::decode_rfc2047, parsers::message::MessageStream};
|
||||||
|
|
||||||
struct ListParser<'x> {
|
struct ListParser<'x> {
|
||||||
token_start: usize,
|
token_start: usize,
|
||||||
@@ -26,11 +26,9 @@ fn add_token<'x>(parser: &mut ListParser<'x>, stream: &MessageStream<'x>, add_sp
|
|||||||
if !parser.tokens.is_empty() {
|
if !parser.tokens.is_empty() {
|
||||||
parser.tokens.push(" ".into());
|
parser.tokens.push(" ".into());
|
||||||
}
|
}
|
||||||
parser.tokens.push(
|
parser.tokens.push(String::from_utf8_lossy(
|
||||||
stream
|
&stream.data[parser.token_start - 1..parser.token_end],
|
||||||
.get_string(parser.token_start - 1, parser.token_end)
|
));
|
||||||
.unwrap(),
|
|
||||||
);
|
|
||||||
|
|
||||||
if add_space {
|
if add_space {
|
||||||
parser.tokens.push(" ".into());
|
parser.tokens.push(" ".into());
|
||||||
@@ -53,7 +51,7 @@ fn add_tokens_to_list(parser: &mut ListParser) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_comma_separared<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'x, str>>> {
|
pub fn parse_comma_separared<'x>(stream: &mut MessageStream<'x>) -> Option<Vec<Cow<'x, str>>> {
|
||||||
let mut parser = ListParser {
|
let mut parser = ListParser {
|
||||||
token_start: 0,
|
token_start: 0,
|
||||||
token_end: 0,
|
token_end: 0,
|
||||||
@@ -62,21 +60,26 @@ pub fn parse_comma_separared<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'
|
|||||||
list: Vec::new(),
|
list: Vec::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
while let Some(ch) = stream.next() {
|
let mut iter = stream.data[stream.pos..].iter();
|
||||||
|
|
||||||
|
while let Some(ch) = iter.next() {
|
||||||
|
stream.pos += 1;
|
||||||
match ch {
|
match ch {
|
||||||
b'\n' => {
|
b'\n' => {
|
||||||
add_token(&mut parser, stream, false);
|
add_token(&mut parser, stream, false);
|
||||||
|
|
||||||
match stream.peek() {
|
match stream.data.get(stream.pos) {
|
||||||
Some(b' ' | b'\t') => {
|
Some(b' ' | b'\t') => {
|
||||||
if !parser.is_token_start {
|
if !parser.is_token_start {
|
||||||
parser.is_token_start = true;
|
parser.is_token_start = true;
|
||||||
}
|
}
|
||||||
stream.advance(1);
|
iter.next();
|
||||||
|
stream.pos += 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
add_tokens_to_list(&mut parser);
|
add_tokens_to_list(&mut parser);
|
||||||
|
|
||||||
return if !parser.list.is_empty() {
|
return if !parser.list.is_empty() {
|
||||||
parser.list.into()
|
parser.list.into()
|
||||||
} else {
|
} else {
|
||||||
@@ -92,9 +95,11 @@ pub fn parse_comma_separared<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
b'=' if parser.is_token_start => {
|
b'=' if parser.is_token_start => {
|
||||||
if let Some(token) = parse_encoded_word(stream) {
|
if let (bytes_read, Some(token)) = decode_rfc2047(stream, stream.pos) {
|
||||||
add_token(&mut parser, stream, true);
|
add_token(&mut parser, stream, true);
|
||||||
parser.tokens.push(token.into());
|
parser.tokens.push(token.into());
|
||||||
|
stream.pos += bytes_read;
|
||||||
|
iter = stream.data[stream.pos..].iter();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -112,10 +117,10 @@ pub fn parse_comma_separared<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'
|
|||||||
}
|
}
|
||||||
|
|
||||||
if parser.token_start == 0 {
|
if parser.token_start == 0 {
|
||||||
parser.token_start = stream.get_pos();
|
parser.token_start = stream.pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.token_end = stream.get_pos();
|
parser.token_end = stream.pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
@@ -123,7 +128,8 @@ pub fn parse_comma_separared<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::parsers::{fields::list::parse_comma_separared, message_stream::MessageStream};
|
use crate::parsers::fields::list::parse_comma_separared;
|
||||||
|
use crate::parsers::message::MessageStream;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_comma_separated_text() {
|
fn parse_comma_separated_text() {
|
||||||
@@ -167,7 +173,7 @@ mod tests {
|
|||||||
for input in inputs {
|
for input in inputs {
|
||||||
let str = input.0.to_string();
|
let str = input.0.to_string();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse_comma_separared(&MessageStream::new(str.as_bytes()),).unwrap(),
|
parse_comma_separared(&mut MessageStream::new(str.as_bytes()),).unwrap(),
|
||||||
input.1
|
input.1
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
+154
-148
@@ -21,7 +21,7 @@ use self::{
|
|||||||
unstructured::parse_unstructured,
|
unstructured::parse_unstructured,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::message_stream::MessageStream;
|
use super::message::MessageStream;
|
||||||
|
|
||||||
pub mod address;
|
pub mod address;
|
||||||
pub mod content_type;
|
pub mod content_type;
|
||||||
@@ -35,166 +35,166 @@ pub trait MessageField<'x>: FieldSet<'x> + MimeFieldGet<'x> {}
|
|||||||
impl<'x, T: FieldSet<'x> + MimeFieldGet<'x>> MessageField<'x> for T {}
|
impl<'x, T: FieldSet<'x> + MimeFieldGet<'x>> MessageField<'x> for T {}
|
||||||
|
|
||||||
pub trait FieldSet<'x> {
|
pub trait FieldSet<'x> {
|
||||||
fn set_date(&mut self, stream: &MessageStream<'x>);
|
fn set_date(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_sender(&mut self, stream: &MessageStream<'x>);
|
fn set_sender(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_received(&mut self, stream: &MessageStream<'x>);
|
fn set_received(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_references(&mut self, stream: &MessageStream<'x>);
|
fn set_references(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_cc(&mut self, stream: &MessageStream<'x>);
|
fn set_cc(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_comments(&mut self, stream: &MessageStream<'x>);
|
fn set_comments(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_resent_cc(&mut self, stream: &MessageStream<'x>);
|
fn set_resent_cc(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_content_id(&mut self, stream: &MessageStream<'x>);
|
fn set_content_id(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_resent_message_id(&mut self, stream: &MessageStream<'x>);
|
fn set_resent_message_id(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_reply_to(&mut self, stream: &MessageStream<'x>);
|
fn set_reply_to(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_resent_to(&mut self, stream: &MessageStream<'x>);
|
fn set_resent_to(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_resent_bcc(&mut self, stream: &MessageStream<'x>);
|
fn set_resent_bcc(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_subject(&mut self, stream: &MessageStream<'x>);
|
fn set_subject(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_keywords(&mut self, stream: &MessageStream<'x>);
|
fn set_keywords(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_list_help(&mut self, stream: &MessageStream<'x>);
|
fn set_list_help(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_list_id(&mut self, stream: &MessageStream<'x>);
|
fn set_list_id(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_list_owner(&mut self, stream: &MessageStream<'x>);
|
fn set_list_owner(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_resent_date(&mut self, stream: &MessageStream<'x>);
|
fn set_resent_date(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_to(&mut self, stream: &MessageStream<'x>);
|
fn set_to(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_bcc(&mut self, stream: &MessageStream<'x>);
|
fn set_bcc(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_from(&mut self, stream: &MessageStream<'x>);
|
fn set_from(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_content_transfer_encoding(&mut self, stream: &MessageStream<'x>);
|
fn set_content_transfer_encoding(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_return_path(&mut self, stream: &MessageStream<'x>);
|
fn set_return_path(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_list_archive(&mut self, stream: &MessageStream<'x>);
|
fn set_list_archive(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_resent_sender(&mut self, stream: &MessageStream<'x>);
|
fn set_resent_sender(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_list_subscribe(&mut self, stream: &MessageStream<'x>);
|
fn set_list_subscribe(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_message_id(&mut self, stream: &MessageStream<'x>);
|
fn set_message_id(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_content_type(&mut self, stream: &MessageStream<'x>);
|
fn set_content_type(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_list_post(&mut self, stream: &MessageStream<'x>);
|
fn set_list_post(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_in_reply_to(&mut self, stream: &MessageStream<'x>);
|
fn set_in_reply_to(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_content_description(&mut self, stream: &MessageStream<'x>);
|
fn set_content_description(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_resent_from(&mut self, stream: &MessageStream<'x>);
|
fn set_resent_from(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_content_disposition(&mut self, stream: &MessageStream<'x>);
|
fn set_content_disposition(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_list_unsubscribe(&mut self, stream: &MessageStream<'x>);
|
fn set_list_unsubscribe(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_mime_version(&mut self, stream: &MessageStream<'x>);
|
fn set_mime_version(&mut self, stream: &mut MessageStream<'x>);
|
||||||
fn set_unsupported(&mut self, stream: &MessageStream<'x>, name: &'x [u8]);
|
fn set_unsupported(&mut self, stream: &mut MessageStream<'x>, name: &'x [u8]);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'x> FieldSet<'x> for MessageHeader<'x> {
|
impl<'x> FieldSet<'x> for MessageHeader<'x> {
|
||||||
fn set_date(&mut self, stream: &MessageStream<'x>) {
|
fn set_date(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.date = self::date::parse_date(stream, false);
|
self.date = self::date::parse_date(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_sender(&mut self, stream: &MessageStream<'x>) {
|
fn set_sender(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.sender = parse_address(stream);
|
self.sender = parse_address(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_received(&mut self, stream: &MessageStream<'x>) {
|
fn set_received(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
append_line(&mut self.received, parse_raw(stream));
|
append_line(&mut self.received, parse_raw(stream));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_references(&mut self, stream: &MessageStream<'x>) {
|
fn set_references(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.references = parse_id(stream);
|
self.references = parse_id(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_cc(&mut self, stream: &MessageStream<'x>) {
|
fn set_cc(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.cc = parse_address(stream);
|
self.cc = parse_address(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_comments(&mut self, stream: &MessageStream<'x>) {
|
fn set_comments(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
append_line(&mut self.comments, parse_unstructured(stream));
|
append_line(&mut self.comments, parse_unstructured(stream));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_resent_cc(&mut self, stream: &MessageStream<'x>) {
|
fn set_resent_cc(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
append_address_line(&mut self.resent_cc, parse_address(stream));
|
append_address_line(&mut self.resent_cc, parse_address(stream));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_resent_message_id(&mut self, stream: &MessageStream<'x>) {
|
fn set_resent_message_id(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
append_line_many(&mut self.resent_message_id, parse_id(stream));
|
append_line_many(&mut self.resent_message_id, parse_id(stream));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_reply_to(&mut self, stream: &MessageStream<'x>) {
|
fn set_reply_to(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.reply_to = parse_address(stream);
|
self.reply_to = parse_address(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_resent_to(&mut self, stream: &MessageStream<'x>) {
|
fn set_resent_to(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
append_address_line(&mut self.resent_to, parse_address(stream));
|
append_address_line(&mut self.resent_to, parse_address(stream));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_resent_bcc(&mut self, stream: &MessageStream<'x>) {
|
fn set_resent_bcc(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
append_address_line(&mut self.resent_bcc, parse_address(stream));
|
append_address_line(&mut self.resent_bcc, parse_address(stream));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_subject(&mut self, stream: &MessageStream<'x>) {
|
fn set_subject(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.subject = parse_unstructured(stream);
|
self.subject = parse_unstructured(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_keywords(&mut self, stream: &MessageStream<'x>) {
|
fn set_keywords(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
append_line_many(&mut self.keywords, parse_comma_separared(stream));
|
append_line_many(&mut self.keywords, parse_comma_separared(stream));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_list_help(&mut self, stream: &MessageStream<'x>) {
|
fn set_list_help(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.list_help = parse_address(stream);
|
self.list_help = parse_address(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_list_id(&mut self, stream: &MessageStream<'x>) {
|
fn set_list_id(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.list_id = parse_address(stream);
|
self.list_id = parse_address(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_list_owner(&mut self, stream: &MessageStream<'x>) {
|
fn set_list_owner(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.list_owner = parse_address(stream);
|
self.list_owner = parse_address(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_resent_date(&mut self, stream: &MessageStream<'x>) {
|
fn set_resent_date(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
append_date_line(&mut self.resent_date, self::date::parse_date(stream, false));
|
append_date_line(&mut self.resent_date, self::date::parse_date(stream));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_to(&mut self, stream: &MessageStream<'x>) {
|
fn set_to(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.to = parse_address(stream);
|
self.to = parse_address(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_bcc(&mut self, stream: &MessageStream<'x>) {
|
fn set_bcc(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.bcc = parse_address(stream);
|
self.bcc = parse_address(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_from(&mut self, stream: &MessageStream<'x>) {
|
fn set_from(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.from = parse_address(stream);
|
self.from = parse_address(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_return_path(&mut self, stream: &MessageStream<'x>) {
|
fn set_return_path(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
append_line_many(&mut self.return_path, parse_id(stream));
|
append_line_many(&mut self.return_path, parse_id(stream));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_list_archive(&mut self, stream: &MessageStream<'x>) {
|
fn set_list_archive(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.list_archive = parse_address(stream);
|
self.list_archive = parse_address(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_resent_sender(&mut self, stream: &MessageStream<'x>) {
|
fn set_resent_sender(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
append_address_line(&mut self.resent_sender, parse_address(stream));
|
append_address_line(&mut self.resent_sender, parse_address(stream));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_list_subscribe(&mut self, stream: &MessageStream<'x>) {
|
fn set_list_subscribe(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.list_subscribe = parse_address(stream);
|
self.list_subscribe = parse_address(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_message_id(&mut self, stream: &MessageStream<'x>) {
|
fn set_message_id(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.message_id = parse_id(stream).map(|mut v| v.pop().unwrap());
|
self.message_id = parse_id(stream).map(|mut v| v.pop().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_list_post(&mut self, stream: &MessageStream<'x>) {
|
fn set_list_post(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.list_post = parse_address(stream);
|
self.list_post = parse_address(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_in_reply_to(&mut self, stream: &MessageStream<'x>) {
|
fn set_in_reply_to(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.reply_to = parse_address(stream);
|
self.reply_to = parse_address(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_resent_from(&mut self, stream: &MessageStream<'x>) {
|
fn set_resent_from(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
append_address_line(&mut self.resent_from, parse_address(stream));
|
append_address_line(&mut self.resent_from, parse_address(stream));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_list_unsubscribe(&mut self, stream: &MessageStream<'x>) {
|
fn set_list_unsubscribe(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.list_unsubscribe = parse_address(stream);
|
self.list_unsubscribe = parse_address(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_mime_version(&mut self, stream: &MessageStream<'x>) {
|
fn set_mime_version(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.mime_version = parse_raw(stream);
|
self.mime_version = parse_raw(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_unsupported(&mut self, stream: &MessageStream<'x>, name: &'x [u8]) {
|
fn set_unsupported(&mut self, stream: &mut MessageStream<'x>, name: &'x [u8]) {
|
||||||
if let Ok(name) = std::str::from_utf8(name) {
|
if let Ok(name) = std::str::from_utf8(name) {
|
||||||
if let Some(value) = parse_unstructured(stream) {
|
if let Some(value) = parse_unstructured(stream) {
|
||||||
if let Some(arr) = self.others.get_mut(name) {
|
if let Some(arr) = self.others.get_mut(name) {
|
||||||
@@ -206,319 +206,325 @@ impl<'x> FieldSet<'x> for MessageHeader<'x> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_content_id(&mut self, stream: &MessageStream<'x>) {
|
fn set_content_id(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.content_id = parse_id(stream).map(|mut v| v.pop().unwrap());
|
self.content_id = parse_id(stream).map(|mut v| v.pop().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_content_transfer_encoding(&mut self, stream: &MessageStream<'x>) {
|
fn set_content_transfer_encoding(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.content_transfer_encoding = parse_unstructured(stream);
|
self.content_transfer_encoding = parse_unstructured(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_content_type(&mut self, stream: &MessageStream<'x>) {
|
fn set_content_type(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.content_type = self::content_type::parse_content_type(stream);
|
self.content_type = self::content_type::parse_content_type(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_content_description(&mut self, stream: &MessageStream<'x>) {
|
fn set_content_description(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.content_description = parse_unstructured(stream);
|
self.content_description = parse_unstructured(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_content_disposition(&mut self, stream: &MessageStream<'x>) {
|
fn set_content_disposition(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.content_disposition = self::content_type::parse_content_type(stream);
|
self.content_disposition = self::content_type::parse_content_type(stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'x> FieldSet<'x> for MimeHeader<'x> {
|
impl<'x> FieldSet<'x> for MimeHeader<'x> {
|
||||||
fn set_date(&mut self, stream: &MessageStream<'x>) {
|
fn set_date(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_sender(&mut self, stream: &MessageStream<'x>) {
|
fn set_sender(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_received(&mut self, stream: &MessageStream<'x>) {
|
fn set_received(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_references(&mut self, stream: &MessageStream<'x>) {
|
fn set_references(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_cc(&mut self, stream: &MessageStream<'x>) {
|
fn set_cc(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_comments(&mut self, stream: &MessageStream<'x>) {
|
fn set_comments(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_resent_cc(&mut self, stream: &MessageStream<'x>) {
|
fn set_resent_cc(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_resent_message_id(&mut self, stream: &MessageStream<'x>) {
|
fn set_resent_message_id(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_reply_to(&mut self, stream: &MessageStream<'x>) {
|
fn set_reply_to(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_resent_to(&mut self, stream: &MessageStream<'x>) {
|
fn set_resent_to(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_resent_bcc(&mut self, stream: &MessageStream<'x>) {
|
fn set_resent_bcc(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_subject(&mut self, stream: &MessageStream<'x>) {
|
fn set_subject(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_keywords(&mut self, stream: &MessageStream<'x>) {
|
fn set_keywords(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_list_help(&mut self, stream: &MessageStream<'x>) {
|
fn set_list_help(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_list_id(&mut self, stream: &MessageStream<'x>) {
|
fn set_list_id(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_list_owner(&mut self, stream: &MessageStream<'x>) {
|
fn set_list_owner(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_resent_date(&mut self, stream: &MessageStream<'x>) {
|
fn set_resent_date(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_to(&mut self, stream: &MessageStream<'x>) {
|
fn set_to(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_bcc(&mut self, stream: &MessageStream<'x>) {
|
fn set_bcc(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_from(&mut self, stream: &MessageStream<'x>) {
|
fn set_from(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_return_path(&mut self, stream: &MessageStream<'x>) {
|
fn set_return_path(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_list_archive(&mut self, stream: &MessageStream<'x>) {
|
fn set_list_archive(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_resent_sender(&mut self, stream: &MessageStream<'x>) {
|
fn set_resent_sender(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_list_subscribe(&mut self, stream: &MessageStream<'x>) {
|
fn set_list_subscribe(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_message_id(&mut self, stream: &MessageStream<'x>) {
|
fn set_message_id(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_list_post(&mut self, stream: &MessageStream<'x>) {
|
fn set_list_post(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_in_reply_to(&mut self, stream: &MessageStream<'x>) {
|
fn set_in_reply_to(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_resent_from(&mut self, stream: &MessageStream<'x>) {
|
fn set_resent_from(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_list_unsubscribe(&mut self, stream: &MessageStream<'x>) {
|
fn set_list_unsubscribe(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_mime_version(&mut self, stream: &MessageStream<'x>) {
|
fn set_mime_version(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_unsupported(&mut self, stream: &MessageStream<'x>, _name: &'x [u8]) {
|
fn set_unsupported(&mut self, stream: &mut MessageStream<'x>, _name: &'x [u8]) {
|
||||||
parse_and_ignore(stream);
|
parse_and_ignore(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_content_id(&mut self, stream: &MessageStream<'x>) {
|
fn set_content_id(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.content_id = parse_id(stream).map(|mut v| v.pop().unwrap());
|
self.content_id = parse_id(stream).map(|mut v| v.pop().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_content_transfer_encoding(&mut self, stream: &MessageStream<'x>) {
|
fn set_content_transfer_encoding(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.content_transfer_encoding = parse_unstructured(stream);
|
self.content_transfer_encoding = parse_unstructured(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_content_type(&mut self, stream: &MessageStream<'x>) {
|
fn set_content_type(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.content_type = self::content_type::parse_content_type(stream);
|
self.content_type = self::content_type::parse_content_type(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_content_description(&mut self, stream: &MessageStream<'x>) {
|
fn set_content_description(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.content_description = parse_unstructured(stream);
|
self.content_description = parse_unstructured(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_content_disposition(&mut self, stream: &MessageStream<'x>) {
|
fn set_content_disposition(&mut self, stream: &mut MessageStream<'x>) {
|
||||||
self.content_disposition = self::content_type::parse_content_type(stream);
|
self.content_disposition = self::content_type::parse_content_type(stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_date<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_date<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_date(stream);
|
header.set_date(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_sender<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_sender<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_sender(stream);
|
header.set_sender(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_received<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_received<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_received(stream);
|
header.set_received(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_references<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_references<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_references(stream);
|
header.set_references(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_cc<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_cc<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_cc(stream);
|
header.set_cc(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_comments<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_comments<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_comments(stream);
|
header.set_comments(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_resent_cc<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_resent_cc<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_resent_cc(stream);
|
header.set_resent_cc(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_content_id<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_content_id<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_content_id(stream);
|
header.set_content_id(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_resent_message_id<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_resent_message_id<'x>(
|
||||||
|
header: &mut dyn MessageField<'x>,
|
||||||
|
stream: &mut MessageStream<'x>,
|
||||||
|
) {
|
||||||
header.set_resent_message_id(stream);
|
header.set_resent_message_id(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_reply_to<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_reply_to<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_reply_to(stream);
|
header.set_reply_to(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_resent_to<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_resent_to<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_resent_to(stream);
|
header.set_resent_to(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_resent_bcc<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_resent_bcc<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_resent_bcc(stream);
|
header.set_resent_bcc(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_subject<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_subject<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_subject(stream);
|
header.set_subject(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_keywords<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_keywords<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_keywords(stream);
|
header.set_keywords(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_list_help<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_list_help<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_list_help(stream);
|
header.set_list_help(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_list_id<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_list_id<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_list_id(stream);
|
header.set_list_id(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_list_owner<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_list_owner<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_list_owner(stream);
|
header.set_list_owner(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_resent_date<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_resent_date<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_resent_date(stream);
|
header.set_resent_date(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_to<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_to<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_to(stream);
|
header.set_to(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_bcc<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_bcc<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_bcc(stream);
|
header.set_bcc(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_from<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_from<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_from(stream);
|
header.set_from(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_content_transfer_encoding<'x>(
|
pub fn parse_content_transfer_encoding<'x>(
|
||||||
header: &mut dyn MessageField<'x>,
|
header: &mut dyn MessageField<'x>,
|
||||||
stream: &MessageStream<'x>,
|
stream: &mut MessageStream<'x>,
|
||||||
) {
|
) {
|
||||||
header.set_content_transfer_encoding(stream);
|
header.set_content_transfer_encoding(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_return_path<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_return_path<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_return_path(stream);
|
header.set_return_path(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_list_archive<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_list_archive<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_list_archive(stream);
|
header.set_list_archive(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_resent_sender<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_resent_sender<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_resent_sender(stream);
|
header.set_resent_sender(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_list_subscribe<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_list_subscribe<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_list_subscribe(stream);
|
header.set_list_subscribe(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_message_id<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_message_id<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_message_id(stream);
|
header.set_message_id(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_content_type<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_content_type<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_content_type(stream);
|
header.set_content_type(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_list_post<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_list_post<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_list_post(stream);
|
header.set_list_post(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_in_reply_to<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_in_reply_to<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_in_reply_to(stream);
|
header.set_in_reply_to(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_content_description<'x>(
|
pub fn parse_content_description<'x>(
|
||||||
header: &mut dyn MessageField<'x>,
|
header: &mut dyn MessageField<'x>,
|
||||||
stream: &MessageStream<'x>,
|
stream: &mut MessageStream<'x>,
|
||||||
) {
|
) {
|
||||||
header.set_content_description(stream);
|
header.set_content_description(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_resent_from<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_resent_from<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_resent_from(stream);
|
header.set_resent_from(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_content_disposition<'x>(
|
pub fn parse_content_disposition<'x>(
|
||||||
header: &mut dyn MessageField<'x>,
|
header: &mut dyn MessageField<'x>,
|
||||||
stream: &MessageStream<'x>,
|
stream: &mut MessageStream<'x>,
|
||||||
) {
|
) {
|
||||||
header.set_content_disposition(stream);
|
header.set_content_disposition(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_list_unsubscribe<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_list_unsubscribe<'x>(
|
||||||
|
header: &mut dyn MessageField<'x>,
|
||||||
|
stream: &mut MessageStream<'x>,
|
||||||
|
) {
|
||||||
header.set_list_unsubscribe(stream);
|
header.set_list_unsubscribe(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_mime_version<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
pub fn parse_mime_version<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||||
header.set_mime_version(stream);
|
header.set_mime_version(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -569,11 +575,11 @@ fn append_line_many<'x>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_no_op<'x>(_header: &mut dyn MessageField<'x>, _stream: &MessageStream<'x>) {}
|
pub fn parse_no_op<'x>(_header: &mut dyn MessageField<'x>, _stream: &mut MessageStream<'x>) {}
|
||||||
|
|
||||||
pub fn parse_unsupported<'x>(
|
pub fn parse_unsupported<'x>(
|
||||||
header: &mut dyn MessageField<'x>,
|
header: &mut dyn MessageField<'x>,
|
||||||
stream: &MessageStream<'x>,
|
stream: &mut MessageStream<'x>,
|
||||||
|
|
||||||
name: &'x [u8],
|
name: &'x [u8],
|
||||||
) {
|
) {
|
||||||
|
|||||||
+25
-18
@@ -11,25 +11,26 @@
|
|||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use crate::parsers::message_stream::MessageStream;
|
use crate::parsers::message::MessageStream;
|
||||||
|
|
||||||
pub fn parse_raw<'x>(stream: &MessageStream<'x>) -> Option<Cow<'x, str>> {
|
pub fn parse_raw<'x>(stream: &mut MessageStream<'x>) -> Option<Cow<'x, str>> {
|
||||||
let mut token_start: usize = 0;
|
let mut token_start: usize = 0;
|
||||||
let mut token_end: usize = 0;
|
let mut token_end: usize = 0;
|
||||||
|
|
||||||
while let Some(ch) = stream.next() {
|
let mut iter = stream.data[stream.pos..].iter();
|
||||||
|
|
||||||
|
while let Some(ch) = iter.next() {
|
||||||
|
stream.pos += 1;
|
||||||
match ch {
|
match ch {
|
||||||
b'\n' => match stream.peek() {
|
b'\n' => match stream.data.get(stream.pos) {
|
||||||
Some(b' ' | b'\t') => {
|
Some(b' ' | b'\t') => {
|
||||||
stream.advance(1);
|
iter.next();
|
||||||
|
stream.pos += 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return if token_start > 0 {
|
return if token_start > 0 {
|
||||||
stream
|
String::from_utf8_lossy(&stream.data[token_start - 1..token_end]).into()
|
||||||
.get_string(token_start - 1, token_end)
|
|
||||||
.unwrap()
|
|
||||||
.into()
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
@@ -40,24 +41,29 @@ pub fn parse_raw<'x>(stream: &MessageStream<'x>) -> Option<Cow<'x, str>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if token_start == 0 {
|
if token_start == 0 {
|
||||||
token_start = stream.get_pos();
|
token_start = stream.pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
token_end = stream.get_pos();
|
token_end = stream.pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_and_ignore(stream: &MessageStream) {
|
pub fn parse_and_ignore(stream: &mut MessageStream) {
|
||||||
while let Some(ch) = stream.next() {
|
let mut iter = stream.data[stream.pos..].iter();
|
||||||
|
|
||||||
|
while let Some(ch) = iter.next() {
|
||||||
|
stream.pos += 1;
|
||||||
|
|
||||||
if ch == &b'\n' {
|
if ch == &b'\n' {
|
||||||
match stream.peek() {
|
match stream.data.get(stream.pos) {
|
||||||
Some(b' ' | b'\t') => {
|
Some(b' ' | b'\t') => {
|
||||||
stream.advance(1);
|
iter.next();
|
||||||
|
stream.pos += 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
_ => return,
|
_ => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,7 +71,8 @@ pub fn parse_and_ignore(stream: &MessageStream) {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::parsers::{fields::raw::parse_raw, message_stream::MessageStream};
|
use crate::parsers::fields::raw::parse_raw;
|
||||||
|
use crate::parsers::message::MessageStream;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_raw_text() {
|
fn parse_raw_text() {
|
||||||
@@ -89,7 +96,7 @@ mod tests {
|
|||||||
for input in inputs {
|
for input in inputs {
|
||||||
let str = input.0.to_string();
|
let str = input.0.to_string();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse_raw(&MessageStream::new(str.as_bytes())).unwrap(),
|
parse_raw(&mut MessageStream::new(str.as_bytes())).unwrap(),
|
||||||
input.1,
|
input.1,
|
||||||
"Failed for '{:?}'",
|
"Failed for '{:?}'",
|
||||||
input.0
|
input.0
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use crate::{decoders::encoded_word::parse_encoded_word, parsers::message_stream::MessageStream};
|
use crate::{decoders::encoded_word::decode_rfc2047, parsers::message::MessageStream};
|
||||||
struct UnstructuredParser<'x> {
|
struct UnstructuredParser<'x> {
|
||||||
token_start: usize,
|
token_start: usize,
|
||||||
token_end: usize,
|
token_end: usize,
|
||||||
@@ -24,11 +24,9 @@ fn add_token<'x>(parser: &mut UnstructuredParser<'x>, stream: &MessageStream<'x>
|
|||||||
if !parser.tokens.is_empty() {
|
if !parser.tokens.is_empty() {
|
||||||
parser.tokens.push(" ".into());
|
parser.tokens.push(" ".into());
|
||||||
}
|
}
|
||||||
parser.tokens.push(
|
parser.tokens.push(String::from_utf8_lossy(
|
||||||
stream
|
&stream.data[parser.token_start - 1..parser.token_end],
|
||||||
.get_string(parser.token_start - 1, parser.token_end)
|
));
|
||||||
.unwrap(),
|
|
||||||
);
|
|
||||||
|
|
||||||
if add_space {
|
if add_space {
|
||||||
parser.tokens.push(" ".into());
|
parser.tokens.push(" ".into());
|
||||||
@@ -39,7 +37,7 @@ fn add_token<'x>(parser: &mut UnstructuredParser<'x>, stream: &MessageStream<'x>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_unstructured<'x>(stream: &MessageStream<'x>) -> Option<Cow<'x, str>> {
|
pub fn parse_unstructured<'x>(stream: &mut MessageStream<'x>) -> Option<Cow<'x, str>> {
|
||||||
let mut parser = UnstructuredParser {
|
let mut parser = UnstructuredParser {
|
||||||
token_start: 0,
|
token_start: 0,
|
||||||
token_end: 0,
|
token_end: 0,
|
||||||
@@ -47,17 +45,21 @@ pub fn parse_unstructured<'x>(stream: &MessageStream<'x>) -> Option<Cow<'x, str>
|
|||||||
tokens: Vec::new(),
|
tokens: Vec::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
while let Some(ch) = stream.next() {
|
let mut iter = stream.data[stream.pos..].iter();
|
||||||
|
|
||||||
|
while let Some(ch) = iter.next() {
|
||||||
|
stream.pos += 1;
|
||||||
match ch {
|
match ch {
|
||||||
b'\n' => {
|
b'\n' => {
|
||||||
add_token(&mut parser, stream, false);
|
add_token(&mut parser, stream, false);
|
||||||
|
|
||||||
match stream.peek() {
|
match stream.data.get(stream.pos) {
|
||||||
Some(b' ' | b'\t') => {
|
Some(b' ' | b'\t') => {
|
||||||
if !parser.is_token_start {
|
if !parser.is_token_start {
|
||||||
parser.is_token_start = true;
|
parser.is_token_start = true;
|
||||||
}
|
}
|
||||||
stream.advance(1);
|
iter.next();
|
||||||
|
stream.pos += 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
@@ -76,9 +78,11 @@ pub fn parse_unstructured<'x>(stream: &MessageStream<'x>) -> Option<Cow<'x, str>
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
b'=' if parser.is_token_start => {
|
b'=' if parser.is_token_start => {
|
||||||
if let Some(token) = parse_encoded_word(stream) {
|
if let (bytes_read, Some(token)) = decode_rfc2047(stream, stream.pos) {
|
||||||
add_token(&mut parser, stream, true);
|
add_token(&mut parser, stream, true);
|
||||||
parser.tokens.push(token.into());
|
parser.tokens.push(token.into());
|
||||||
|
stream.pos += bytes_read;
|
||||||
|
iter = stream.data[stream.pos..].iter();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -91,10 +95,10 @@ pub fn parse_unstructured<'x>(stream: &MessageStream<'x>) -> Option<Cow<'x, str>
|
|||||||
}
|
}
|
||||||
|
|
||||||
if parser.token_start == 0 {
|
if parser.token_start == 0 {
|
||||||
parser.token_start = stream.get_pos();
|
parser.token_start = stream.pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.token_end = stream.get_pos();
|
parser.token_end = stream.pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
@@ -105,9 +109,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_unstructured_text() {
|
fn parse_unstructured_text() {
|
||||||
use crate::parsers::{
|
use crate::parsers::fields::unstructured::parse_unstructured;
|
||||||
fields::unstructured::parse_unstructured, message_stream::MessageStream,
|
use crate::parsers::message::MessageStream;
|
||||||
};
|
|
||||||
|
|
||||||
let inputs = [
|
let inputs = [
|
||||||
("Saying Hello\n", "Saying Hello", true),
|
("Saying Hello\n", "Saying Hello", true),
|
||||||
@@ -181,7 +184,7 @@ mod tests {
|
|||||||
for input in inputs {
|
for input in inputs {
|
||||||
let str = input.0.to_string();
|
let str = input.0.to_string();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse_unstructured(&MessageStream::new(str.as_bytes()),).unwrap(),
|
parse_unstructured(&mut MessageStream::new(str.as_bytes()),).unwrap(),
|
||||||
input.1,
|
input.1,
|
||||||
"Failed to parse '{:?}'",
|
"Failed to parse '{:?}'",
|
||||||
input.0
|
input.0
|
||||||
|
|||||||
+22
-13
@@ -9,9 +9,12 @@
|
|||||||
* except according to those terms.
|
* except according to those terms.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use crate::{parsers::message_stream::MessageStream, MessageHeader, MimeHeader};
|
use crate::{MessageHeader, MimeHeader};
|
||||||
|
|
||||||
use super::fields::{parse_unsupported, MessageField};
|
use super::{
|
||||||
|
fields::{parse_unsupported, MessageField},
|
||||||
|
message::MessageStream,
|
||||||
|
};
|
||||||
|
|
||||||
impl<'x> MessageHeader<'x> {
|
impl<'x> MessageHeader<'x> {
|
||||||
pub fn new() -> MessageHeader<'x> {
|
pub fn new() -> MessageHeader<'x> {
|
||||||
@@ -46,13 +49,16 @@ impl<'x> MimeHeader<'x> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub enum HeaderParserResult<'x> {
|
pub enum HeaderParserResult<'x> {
|
||||||
Supported(fn(&mut dyn MessageField<'x>, &MessageStream<'x>)),
|
Supported(fn(&mut dyn MessageField<'x>, &mut MessageStream<'x>)),
|
||||||
Unsupported(&'x [u8]),
|
Unsupported(&'x [u8]),
|
||||||
Lf,
|
Lf,
|
||||||
Eof,
|
Eof,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_headers<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) -> bool {
|
pub fn parse_headers<'x>(
|
||||||
|
header: &mut dyn MessageField<'x>,
|
||||||
|
stream: &mut MessageStream<'x>,
|
||||||
|
) -> bool {
|
||||||
loop {
|
loop {
|
||||||
match parse_header_name(stream) {
|
match parse_header_name(stream) {
|
||||||
HeaderParserResult::Supported(fnc) => fnc(header, stream),
|
HeaderParserResult::Supported(fnc) => fnc(header, stream),
|
||||||
@@ -63,18 +69,20 @@ pub fn parse_headers<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStre
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_header_name<'x>(stream: &MessageStream<'x>) -> HeaderParserResult<'x> {
|
pub fn parse_header_name<'x>(stream: &mut MessageStream<'x>) -> HeaderParserResult<'x> {
|
||||||
let mut token_start: usize = 0;
|
let mut token_start: usize = 0;
|
||||||
let mut token_end: usize = 0;
|
let mut token_end: usize = 0;
|
||||||
let mut token_len: usize = 0;
|
let mut token_len: usize = 0;
|
||||||
let mut token_hash: usize = 0;
|
let mut token_hash: usize = 0;
|
||||||
let mut last_ch: u8 = 0;
|
let mut last_ch: u8 = 0;
|
||||||
|
|
||||||
while let Some(ch) = stream.next() {
|
for ch in stream.data[stream.pos..].iter() {
|
||||||
|
stream.pos += 1;
|
||||||
|
|
||||||
match ch {
|
match ch {
|
||||||
b':' => {
|
b':' => {
|
||||||
if token_start != 0 {
|
if token_start != 0 {
|
||||||
let field = stream.get_bytes(token_start - 1, token_end).unwrap();
|
let field = &stream.data[token_start - 1..token_end];
|
||||||
|
|
||||||
if (2..=25).contains(&token_len) {
|
if (2..=25).contains(&token_len) {
|
||||||
token_hash +=
|
token_hash +=
|
||||||
@@ -92,16 +100,16 @@ pub fn parse_header_name<'x>(stream: &MessageStream<'x>) -> HeaderParserResult<'
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
b'\n' => {
|
b'\n' => {
|
||||||
stream.rewind(1);
|
stream.pos -= 1;
|
||||||
return HeaderParserResult::Lf;
|
return HeaderParserResult::Lf;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
if !(*ch).is_ascii_whitespace() {
|
if !(*ch).is_ascii_whitespace() {
|
||||||
if token_start == 0 {
|
if token_start == 0 {
|
||||||
token_start = stream.get_pos();
|
token_start = stream.pos;
|
||||||
token_end = token_start;
|
token_end = token_start;
|
||||||
} else {
|
} else {
|
||||||
token_end = stream.get_pos();
|
token_end = stream.pos;
|
||||||
last_ch = *ch;
|
last_ch = *ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,6 +121,7 @@ pub fn parse_header_name<'x>(stream: &MessageStream<'x>) -> HeaderParserResult<'
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
HeaderParserResult::Eof
|
HeaderParserResult::Eof
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,7 +130,7 @@ mod tests {
|
|||||||
use crate::parsers::{
|
use crate::parsers::{
|
||||||
fields::{parse_from, parse_mime_version, parse_received, parse_subject},
|
fields::{parse_from, parse_mime_version, parse_received, parse_subject},
|
||||||
header::parse_header_name,
|
header::parse_header_name,
|
||||||
message_stream::MessageStream,
|
message::MessageStream,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::HeaderParserResult;
|
use super::HeaderParserResult;
|
||||||
@@ -152,7 +161,7 @@ mod tests {
|
|||||||
|
|
||||||
for input in inputs {
|
for input in inputs {
|
||||||
let str = input.0.to_string();
|
let str = input.0.to_string();
|
||||||
match parse_header_name(&MessageStream::new(str.as_bytes())) {
|
match parse_header_name(&mut MessageStream::new(str.as_bytes())) {
|
||||||
HeaderParserResult::Supported(f) => {
|
HeaderParserResult::Supported(f) => {
|
||||||
if let HeaderParserResult::Supported(val) = input.1 {
|
if let HeaderParserResult::Supported(val) = input.1 {
|
||||||
if f as usize == val as usize {
|
if f as usize == val as usize {
|
||||||
@@ -188,7 +197,7 @@ static HDR_HASH: &[u8] = &[
|
|||||||
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<'x, 'y> fn(&mut dyn MessageField<'x>, &MessageStream<'x>); 58] = &[
|
static HDR_FNCS: &[for<'x, 'y> fn(&mut dyn MessageField<'x>, &mut MessageStream<'x>); 58] = &[
|
||||||
super::fields::parse_date,
|
super::fields::parse_date,
|
||||||
super::fields::parse_no_op,
|
super::fields::parse_no_op,
|
||||||
super::fields::parse_sender,
|
super::fields::parse_sender,
|
||||||
|
|||||||
+110
-71
@@ -13,15 +13,20 @@ use std::borrow::Cow;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
decoders::{
|
decoders::{
|
||||||
base64::Base64Decoder,
|
base64::decode_base64,
|
||||||
charsets::map::get_charset_decoder,
|
charsets::map::get_charset_decoder,
|
||||||
html::{html_to_text, text_to_html},
|
html::{html_to_text, text_to_html},
|
||||||
quoted_printable::QuotedPrintableDecoder,
|
quoted_printable::decode_quoted_printable,
|
||||||
|
DecodeFnc, DecodeResult,
|
||||||
},
|
},
|
||||||
BinaryPart, ContentType, InlinePart, Message, MessageHeader, MessagePart, MimeHeader, TextPart,
|
BinaryPart, ContentType, InlinePart, Message, MessageHeader, MessagePart, MimeHeader, TextPart,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{fields::MessageField, header::parse_headers, message_stream::MessageStream};
|
use super::{
|
||||||
|
fields::MessageField,
|
||||||
|
header::parse_headers,
|
||||||
|
mime::{get_bytes_to_boundary, seek_next_part, skip_crlf, skip_multipart_end},
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
enum MimeType {
|
enum MimeType {
|
||||||
@@ -37,19 +42,36 @@ enum MimeType {
|
|||||||
Other,
|
Other,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
fn result_to_string<'x>(
|
||||||
fn bytes_to_string<'x>(bytes: Cow<'x, [u8]>, content_type: Option<&ContentType>) -> Cow<'x, str> {
|
result: DecodeResult,
|
||||||
if let Some(content_type) = content_type {
|
data: &'x [u8],
|
||||||
if let Some(charset) = content_type.get_attribute("charset") {
|
content_type: Option<&ContentType>,
|
||||||
if let Some(decoder_fnc) = get_charset_decoder(charset.as_bytes()) {
|
) -> Cow<'x, str> {
|
||||||
return decoder_fnc(bytes.as_ref()).into();
|
match (
|
||||||
}
|
result,
|
||||||
|
content_type.and_then(|ct| {
|
||||||
|
ct.get_attribute("charset")
|
||||||
|
.and_then(|c| get_charset_decoder(c.as_bytes()))
|
||||||
|
}),
|
||||||
|
) {
|
||||||
|
(DecodeResult::Owned(vec), Some(charset_decoder)) => charset_decoder(&vec).into(),
|
||||||
|
(DecodeResult::Owned(vec), None) => String::from_utf8(vec)
|
||||||
|
.unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned())
|
||||||
|
.into(),
|
||||||
|
(DecodeResult::Borrowed((from, to)), Some(charset_decoder)) => {
|
||||||
|
charset_decoder(&data[from..to]).into()
|
||||||
}
|
}
|
||||||
|
(DecodeResult::Borrowed((from, to)), None) => String::from_utf8_lossy(&data[from..to]),
|
||||||
|
(DecodeResult::Empty, _) => "\n".to_string().into(),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String::from_utf8(bytes.into_owned())
|
fn result_to_bytes(result: DecodeResult, data: &[u8]) -> Cow<[u8]> {
|
||||||
.unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned())
|
match result {
|
||||||
.into()
|
DecodeResult::Owned(vec) => Cow::Owned(vec),
|
||||||
|
DecodeResult::Borrowed((from, to)) => Cow::Borrowed(&data[from..to]),
|
||||||
|
DecodeResult::Empty => Cow::from(vec![b'?']),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
@@ -115,6 +137,17 @@ impl MessageParserState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct MessageStream<'x> {
|
||||||
|
pub data: &'x [u8],
|
||||||
|
pub pos: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'x> MessageStream<'x> {
|
||||||
|
pub fn new(data: &'x [u8]) -> MessageStream<'x> {
|
||||||
|
MessageStream { data, pos: 0 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'x> Message<'x> {
|
impl<'x> Message<'x> {
|
||||||
fn new() -> Message<'x> {
|
fn new() -> Message<'x> {
|
||||||
Message {
|
Message {
|
||||||
@@ -136,7 +169,7 @@ impl<'x> Message<'x> {
|
|||||||
/// To make sure parsing was successfull use `is_empty()`.
|
/// To make sure parsing was successfull use `is_empty()`.
|
||||||
///
|
///
|
||||||
pub fn parse(bytes: &'x [u8]) -> Message<'x> {
|
pub fn parse(bytes: &'x [u8]) -> Message<'x> {
|
||||||
let stream = MessageStream::new(bytes);
|
let mut stream = MessageStream::new(bytes);
|
||||||
|
|
||||||
let mut message = Message::new();
|
let mut message = Message::new();
|
||||||
let mut message_stack = Vec::new();
|
let mut message_stack = Vec::new();
|
||||||
@@ -155,7 +188,7 @@ impl<'x> Message<'x> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Parse headers
|
// Parse headers
|
||||||
if !parse_headers(header, &stream) {
|
if !parse_headers(header, &mut stream) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,7 +204,7 @@ impl<'x> Message<'x> {
|
|||||||
{
|
{
|
||||||
let mime_boundary = ("\n--".to_string() + mime_boundary).into_bytes();
|
let mime_boundary = ("\n--".to_string() + mime_boundary).into_bytes();
|
||||||
|
|
||||||
if stream.seek_next_part(mime_boundary.as_ref()) {
|
if seek_next_part(&mut stream, mime_boundary.as_ref()) {
|
||||||
let new_state = MessageParserState {
|
let new_state = MessageParserState {
|
||||||
in_alternative: state.in_alternative
|
in_alternative: state.in_alternative
|
||||||
|| mime_type == MimeType::MultipartAlernative,
|
|| mime_type == MimeType::MultipartAlernative,
|
||||||
@@ -186,7 +219,7 @@ impl<'x> Message<'x> {
|
|||||||
mime_part_header.clear();
|
mime_part_header.clear();
|
||||||
state_stack.push(state);
|
state_stack.push(state);
|
||||||
state = new_state;
|
state = new_state;
|
||||||
stream.skip_crlf();
|
skip_crlf(&mut stream);
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
mime_type = MimeType::TextOther;
|
mime_type = MimeType::TextOther;
|
||||||
@@ -209,67 +242,76 @@ impl<'x> Message<'x> {
|
|||||||
state_stack.push(state);
|
state_stack.push(state);
|
||||||
message = Message::new();
|
message = Message::new();
|
||||||
state = new_state;
|
state = new_state;
|
||||||
stream.skip_crlf();
|
skip_crlf(&mut stream);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
stream.skip_crlf();
|
skip_crlf(&mut stream);
|
||||||
|
|
||||||
let (success, mut bytes) = match header.get_content_transfer_encoding() {
|
let (is_binary, decode_fnc): (bool, DecodeFnc) = match header
|
||||||
Some(encoding) if encoding.eq_ignore_ascii_case("base64") => stream.decode_base64(
|
.get_content_transfer_encoding()
|
||||||
state
|
{
|
||||||
.mime_boundary
|
Some(encoding) if encoding.eq_ignore_ascii_case("base64") => (false, decode_base64),
|
||||||
.as_ref()
|
Some(encoding) if encoding.eq_ignore_ascii_case("quoted-printable") => {
|
||||||
.map_or_else(|| &[][..], |b| &b[..]),
|
(false, decode_quoted_printable)
|
||||||
false,
|
}
|
||||||
),
|
_ => (true, get_bytes_to_boundary),
|
||||||
Some(encoding) if encoding.eq_ignore_ascii_case("quoted-printable") => stream
|
};
|
||||||
.decode_quoted_printable(
|
|
||||||
|
let (bytes_read, mut bytes) = decode_fnc(
|
||||||
|
&stream,
|
||||||
|
stream.pos,
|
||||||
|
state
|
||||||
|
.mime_boundary
|
||||||
|
.as_ref()
|
||||||
|
.map_or_else(|| &[][..], |b| &b[..]),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Attempt to recover contents of an invalid message
|
||||||
|
if bytes_read == 0 {
|
||||||
|
if stream.pos >= stream.data.len() || (is_binary && state.mime_boundary.is_none()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get raw MIME part
|
||||||
|
let (bytes_read, r_bytes) = if !is_binary {
|
||||||
|
get_bytes_to_boundary(
|
||||||
|
&stream,
|
||||||
|
stream.pos,
|
||||||
state
|
state
|
||||||
.mime_boundary
|
.mime_boundary
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or_else(|| &[][..], |b| &b[..]),
|
.map_or_else(|| &[][..], |b| &b[..]),
|
||||||
false,
|
false,
|
||||||
),
|
)
|
||||||
_ => stream.get_bytes_to_boundary(
|
} else {
|
||||||
state
|
(0, DecodeResult::Empty)
|
||||||
.mime_boundary
|
};
|
||||||
.as_ref()
|
|
||||||
.map_or_else(|| &[][..], |b| &b[..]),
|
|
||||||
),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Attempt to recover contents of an invalid message
|
if bytes_read == 0 {
|
||||||
if !success {
|
// If there is MIME boundary, ignore it and get raw message
|
||||||
if !stream.is_eof() {
|
if state.mime_boundary.is_some() {
|
||||||
// Get raw MIME part
|
let (bytes_read, r_bytes) =
|
||||||
let (success, r_bytes) = stream.get_bytes_to_boundary(
|
get_bytes_to_boundary(&stream, stream.pos, &[][..], false);
|
||||||
state
|
if bytes_read > 0 {
|
||||||
.mime_boundary
|
bytes = r_bytes;
|
||||||
.as_ref()
|
stream.pos += bytes_read;
|
||||||
.map_or_else(|| &[][..], |b| &b[..]),
|
|
||||||
);
|
|
||||||
if !success {
|
|
||||||
// If there is MIME boundary, ignore it and get raw message
|
|
||||||
if !stream.is_eof() && state.mime_boundary.is_some() {
|
|
||||||
let (_, r_bytes) = stream.get_bytes_to_boundary(&[][..]);
|
|
||||||
if r_bytes.is_some() {
|
|
||||||
bytes = r_bytes;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
bytes = r_bytes;
|
break;
|
||||||
}
|
}
|
||||||
mime_type = MimeType::TextOther;
|
} else {
|
||||||
is_inline = false;
|
bytes = r_bytes;
|
||||||
is_text = true;
|
stream.pos += bytes_read;
|
||||||
} else if bytes.is_none() {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
mime_type = MimeType::TextOther;
|
||||||
|
is_inline = false;
|
||||||
|
is_text = true;
|
||||||
|
} else {
|
||||||
|
stream.pos += bytes_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
let is_inline = is_inline
|
let is_inline = is_inline
|
||||||
@@ -309,10 +351,7 @@ impl<'x> Message<'x> {
|
|||||||
|
|
||||||
if is_text {
|
if is_text {
|
||||||
let text_part = TextPart {
|
let text_part = TextPart {
|
||||||
contents: bytes_to_string(
|
contents: result_to_string(bytes, stream.data, header.get_content_type()),
|
||||||
bytes.unwrap_or_else(|| vec![b'\n'].into()),
|
|
||||||
header.get_content_type(),
|
|
||||||
),
|
|
||||||
header: if !mime_part_header.is_empty() {
|
header: if !mime_part_header.is_empty() {
|
||||||
Some(std::mem::take(&mut mime_part_header))
|
Some(std::mem::take(&mut mime_part_header))
|
||||||
} else {
|
} else {
|
||||||
@@ -348,7 +387,7 @@ impl<'x> Message<'x> {
|
|||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
},
|
},
|
||||||
contents: bytes.unwrap_or_else(|| Cow::from(vec![b'?'])),
|
contents: result_to_bytes(bytes, stream.data),
|
||||||
};
|
};
|
||||||
|
|
||||||
if add_to_html {
|
if add_to_html {
|
||||||
@@ -388,7 +427,7 @@ impl<'x> Message<'x> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if stream.skip_multipart_end() {
|
if skip_multipart_end(&mut stream) {
|
||||||
// End of MIME part reached
|
// End of MIME part reached
|
||||||
|
|
||||||
if MimeType::MultipartAlernative == state.mime_type
|
if MimeType::MultipartAlernative == state.mime_type
|
||||||
@@ -436,19 +475,19 @@ impl<'x> Message<'x> {
|
|||||||
|
|
||||||
if let Some(ref mime_boundary) = state.mime_boundary {
|
if let Some(ref mime_boundary) = state.mime_boundary {
|
||||||
// Ancestor has a MIME boundary, seek it.
|
// Ancestor has a MIME boundary, seek it.
|
||||||
if stream.seek_next_part(mime_boundary) {
|
if seek_next_part(&mut stream, mime_boundary) {
|
||||||
continue 'inner;
|
continue 'inner;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break 'outer;
|
break 'outer;
|
||||||
} else {
|
} else {
|
||||||
stream.skip_crlf();
|
skip_crlf(&mut stream);
|
||||||
// Headers of next part expected next, break inner look.
|
// Headers of next part expected next, break inner look.
|
||||||
break 'inner;
|
break 'inner;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if stream.is_eof() {
|
} else if stream.pos >= stream.data.len() {
|
||||||
break 'outer;
|
break 'outer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,206 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
|
|
||||||
* file at the top-level directory of this distribution.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
||||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
|
||||||
* option. This file may not be copied, modified, or distributed
|
|
||||||
* except according to those terms.
|
|
||||||
*/
|
|
||||||
|
|
||||||
use std::{borrow::Cow, cell::Cell};
|
|
||||||
|
|
||||||
pub struct MessageStream<'x> {
|
|
||||||
pub data: &'x [u8],
|
|
||||||
pub pos: Cell<usize>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'x> MessageStream<'x> {
|
|
||||||
pub fn new(data: &'x [u8]) -> MessageStream<'x> {
|
|
||||||
MessageStream {
|
|
||||||
data,
|
|
||||||
pos: 0.into(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn get_bytes(&self, from: usize, to: usize) -> Option<&'x [u8]> {
|
|
||||||
self.data.get(from..to)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_string(&self, from: usize, to: usize) -> Option<Cow<'x, str>> {
|
|
||||||
String::from_utf8_lossy(self.data.get(from..to)?).into()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn is_eof(&self) -> bool {
|
|
||||||
self.pos.get() >= self.data.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn set_pos(&self, pos: usize) {
|
|
||||||
self.pos.set(pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn get_pos(&self) -> usize {
|
|
||||||
self.pos.get()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn next(&self) -> Option<&'x u8> {
|
|
||||||
let pos = self.pos.get();
|
|
||||||
let result = self.data.get(pos);
|
|
||||||
self.pos.set(pos + 1);
|
|
||||||
result
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn peek(&self) -> Option<&'x u8> {
|
|
||||||
self.data.get(self.pos.get())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn advance(&self, pos: usize) {
|
|
||||||
self.pos.set(self.pos.get() + pos)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn seek_next_part(&self, boundary: &[u8]) -> bool {
|
|
||||||
if !boundary.is_empty() {
|
|
||||||
let mut pos = self.pos.get();
|
|
||||||
|
|
||||||
let mut match_count = 0;
|
|
||||||
|
|
||||||
for ch in self.data[pos..].iter() {
|
|
||||||
pos += 1;
|
|
||||||
|
|
||||||
if ch == &boundary[match_count] {
|
|
||||||
match_count += 1;
|
|
||||||
if match_count == boundary.len() {
|
|
||||||
self.pos.set(pos);
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
} else if match_count > 0 {
|
|
||||||
if ch == &boundary[0] {
|
|
||||||
match_count = 1;
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
match_count = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_bytes_to_boundary(&self, boundary: &[u8]) -> (bool, Option<Cow<'x, [u8]>>) {
|
|
||||||
let mut pos = self.pos.get();
|
|
||||||
|
|
||||||
let start_pos = pos;
|
|
||||||
|
|
||||||
return if !boundary.is_empty() {
|
|
||||||
let mut match_count = 0;
|
|
||||||
|
|
||||||
for ch in self.data[pos..].iter() {
|
|
||||||
pos += 1;
|
|
||||||
|
|
||||||
if ch == &boundary[match_count] {
|
|
||||||
match_count += 1;
|
|
||||||
if match_count == boundary.len() {
|
|
||||||
if self.is_boundary_end(pos) {
|
|
||||||
let match_pos = pos - match_count;
|
|
||||||
self.pos.set(pos);
|
|
||||||
|
|
||||||
return (
|
|
||||||
true,
|
|
||||||
if start_pos < match_pos {
|
|
||||||
Cow::from(&self.data[start_pos..match_pos]).into()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
},
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
match_count = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
} else if match_count > 0 {
|
|
||||||
if ch == &boundary[0] {
|
|
||||||
match_count = 1;
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
match_count = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
(false, None)
|
|
||||||
} else if pos < self.data.len() {
|
|
||||||
self.pos.set(self.data.len());
|
|
||||||
(true, Cow::from(&self.data[start_pos..]).into())
|
|
||||||
} else {
|
|
||||||
(false, None)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn skip_crlf(&self) {
|
|
||||||
let mut pos = self.pos.get();
|
|
||||||
|
|
||||||
for ch in self.data[pos..].iter() {
|
|
||||||
match ch {
|
|
||||||
b'\r' | b' ' | b'\t' => pos += 1,
|
|
||||||
b'\n' => {
|
|
||||||
self.pos.set(pos + 1);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
_ => break,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn skip_byte(&self, ch: &u8) -> bool {
|
|
||||||
let pos = self.pos.get();
|
|
||||||
|
|
||||||
if self.data.get(pos) == Some(ch) {
|
|
||||||
self.pos.set(pos + 1);
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn is_boundary_end(&self, pos: usize) -> bool {
|
|
||||||
matches!(
|
|
||||||
self.data.get(pos..),
|
|
||||||
Some([b'\n' | b'\r' | b' ' | b'\t', ..]) | Some([b'-', b'-', ..]) | Some([]) | None
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn skip_multipart_end(&self) -> bool {
|
|
||||||
let pos = self.pos.get();
|
|
||||||
|
|
||||||
match self.data.get(pos..pos + 2) {
|
|
||||||
Some(b"--") => {
|
|
||||||
if let Some(byte) = self.data.get(pos + 2) {
|
|
||||||
if !(*byte).is_ascii_whitespace() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.pos.set(pos + 2);
|
|
||||||
true
|
|
||||||
}
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn rewind(&self, r: usize) {
|
|
||||||
self.pos.set(self.pos.get() - r);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
/*
|
||||||
|
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
|
||||||
|
* file at the top-level directory of this distribution.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||||
|
* option. This file may not be copied, modified, or distributed
|
||||||
|
* except according to those terms.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use crate::decoders::DecodeResult;
|
||||||
|
|
||||||
|
use super::message::MessageStream;
|
||||||
|
|
||||||
|
pub fn seek_next_part(stream: &mut MessageStream, boundary: &[u8]) -> bool {
|
||||||
|
if !boundary.is_empty() {
|
||||||
|
let mut pos = stream.pos;
|
||||||
|
|
||||||
|
let mut match_count = 0;
|
||||||
|
|
||||||
|
for ch in stream.data[pos..].iter() {
|
||||||
|
pos += 1;
|
||||||
|
|
||||||
|
if ch == &boundary[match_count] {
|
||||||
|
match_count += 1;
|
||||||
|
if match_count == boundary.len() {
|
||||||
|
stream.pos = pos;
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} else if match_count > 0 {
|
||||||
|
if ch == &boundary[0] {
|
||||||
|
match_count = 1;
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
match_count = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_bytes_to_boundary<'x>(
|
||||||
|
stream: &MessageStream<'x>,
|
||||||
|
start_pos: usize,
|
||||||
|
boundary: &[u8],
|
||||||
|
_is_word: bool,
|
||||||
|
) -> (usize, DecodeResult) {
|
||||||
|
let mut read_pos = start_pos;
|
||||||
|
|
||||||
|
return if !boundary.is_empty() {
|
||||||
|
let mut match_count = 0;
|
||||||
|
|
||||||
|
for ch in stream.data[read_pos..].iter() {
|
||||||
|
read_pos += 1;
|
||||||
|
|
||||||
|
if ch == &boundary[match_count] {
|
||||||
|
match_count += 1;
|
||||||
|
if match_count == boundary.len() {
|
||||||
|
if is_boundary_end(stream, read_pos) {
|
||||||
|
let match_pos = read_pos - match_count;
|
||||||
|
|
||||||
|
return (
|
||||||
|
read_pos - start_pos,
|
||||||
|
if start_pos < match_pos {
|
||||||
|
DecodeResult::Borrowed((start_pos, match_pos))
|
||||||
|
} else {
|
||||||
|
DecodeResult::Empty
|
||||||
|
},
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
match_count = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
} else if match_count > 0 {
|
||||||
|
if ch == &boundary[0] {
|
||||||
|
match_count = 1;
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
match_count = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(0, DecodeResult::Empty)
|
||||||
|
} else if start_pos < stream.data.len() {
|
||||||
|
(
|
||||||
|
stream.data.len() - start_pos,
|
||||||
|
DecodeResult::Borrowed((start_pos, stream.data.len())),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
(0, DecodeResult::Empty)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn is_boundary_end(stream: &MessageStream, pos: usize) -> bool {
|
||||||
|
matches!(
|
||||||
|
stream.data.get(pos..),
|
||||||
|
Some([b'\n' | b'\r' | b' ' | b'\t', ..]) | Some([b'-', b'-', ..]) | Some([]) | None
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn skip_multipart_end(stream: &mut MessageStream) -> bool {
|
||||||
|
match stream.data.get(stream.pos..stream.pos + 2) {
|
||||||
|
Some(b"--") => {
|
||||||
|
if let Some(byte) = stream.data.get(stream.pos + 2) {
|
||||||
|
if !(*byte).is_ascii_whitespace() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stream.pos += 2;
|
||||||
|
true
|
||||||
|
}
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn skip_crlf(stream: &mut MessageStream) {
|
||||||
|
for ch in stream.data[stream.pos..].iter() {
|
||||||
|
match ch {
|
||||||
|
b'\r' | b' ' | b'\t' => stream.pos += 1,
|
||||||
|
b'\n' => {
|
||||||
|
stream.pos += 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -12,4 +12,4 @@
|
|||||||
pub mod fields;
|
pub mod fields;
|
||||||
pub mod header;
|
pub mod header;
|
||||||
pub mod message;
|
pub mod message;
|
||||||
pub mod message_stream;
|
pub mod mime;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ use mail_parser::*;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_api() {
|
fn test_api() {
|
||||||
let mut input = concat!(
|
let input = concat!(
|
||||||
"From: Art Vandelay <art@vandelay.com> (Vandelay Industries)\n",
|
"From: Art Vandelay <art@vandelay.com> (Vandelay Industries)\n",
|
||||||
"To: \"Colleagues\": \"James Smythe\" <james@vandelay.com>; Friends:\n",
|
"To: \"Colleagues\": \"James Smythe\" <james@vandelay.com>; Friends:\n",
|
||||||
" jane@example.com, =?UTF-8?Q?John_Sm=C3=AEth?= <john@example.com>;\n",
|
" jane@example.com, =?UTF-8?Q?John_Sm=C3=AEth?= <john@example.com>;\n",
|
||||||
@@ -48,10 +48,9 @@ fn test_api() {
|
|||||||
"--giddyup--\n",
|
"--giddyup--\n",
|
||||||
"--festivus--\n",
|
"--festivus--\n",
|
||||||
)
|
)
|
||||||
.as_bytes()
|
.as_bytes();
|
||||||
.to_vec();
|
|
||||||
|
|
||||||
let message = Message::parse(&mut input[..]);
|
let message = Message::parse(input);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
message.get_from(),
|
message.get_from(),
|
||||||
|
|||||||
+2
-2
@@ -142,7 +142,7 @@ Content-Transfer-Encoding: Quoted-printable
|
|||||||
"c_subtype": "basic"
|
"c_subtype": "basic"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"contents": ".. base64-encoded 8000 Hz single-channel\n mu-law-format audio data goes here ...\n"
|
"contents": "... base64-encoded 8000 Hz single-channel\n mu-law-format audio data goes here ...\n"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -154,7 +154,7 @@ Content-Transfer-Encoding: Quoted-printable
|
|||||||
"c_subtype": "jpeg"
|
"c_subtype": "jpeg"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"contents": ".. base64-encoded image data goes here ...\n"
|
"contents": "... base64-encoded image data goes here ...\n"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
Vendored
+1
-1
@@ -78,7 +78,7 @@ Content-Type: text/plain; charset=utf-8
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"contents": "garbage"
|
"contents": "?garbage"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user