2023-07-14 19:12:34 +02:00
|
|
|
use nom::{
|
|
|
|
IResult,
|
2023-07-17 17:14:08 +02:00
|
|
|
branch::alt,
|
2023-07-17 11:44:55 +02:00
|
|
|
bytes::complete::{is_not, tag},
|
|
|
|
multi::many0,
|
2023-07-17 17:14:08 +02:00
|
|
|
sequence::{pair, preceded, tuple},
|
2023-07-17 11:44:55 +02:00
|
|
|
combinator::{not, opt, recognize},
|
2023-07-14 19:12:34 +02:00
|
|
|
};
|
|
|
|
|
2023-07-22 16:46:36 +02:00
|
|
|
use crate::mime::r#type;
|
2023-07-22 14:38:43 +02:00
|
|
|
|
2023-07-22 16:46:36 +02:00
|
|
|
pub struct Part<'a> {
|
2023-07-22 20:52:35 +02:00
|
|
|
Multipart(Multipart<MIME>, Vec<Part<'a>>),
|
|
|
|
Message(MIME<Message>, Message, Part<'a>),
|
|
|
|
Text(MIME<Text>, &'a [u8]),
|
|
|
|
Binary(MIME<Binary>, &'a [u8]),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Part<'a> {
|
|
|
|
List(Vec<Part<'a>>),
|
|
|
|
Single(Part<'a>),
|
|
|
|
Leaf(&'a [u8]),
|
2023-07-22 14:38:43 +02:00
|
|
|
}
|
|
|
|
|
2023-07-22 16:46:36 +02:00
|
|
|
pub fn message() -> IResult<&[u8], Part> {
|
2023-07-22 14:38:43 +02:00
|
|
|
}
|
2023-07-14 19:12:34 +02:00
|
|
|
|
2023-07-22 20:52:35 +02:00
|
|
|
pub fn multipart<'a>(m: Multipart<MIME>) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], Part<'a>> {
|
2023-07-22 16:46:36 +02:00
|
|
|
move |input: &[u8]| {
|
2023-07-22 20:52:35 +02:00
|
|
|
let (mut input_loop, _) = preamble(m.ctype.boundary)(input)?;
|
2023-07-22 16:46:36 +02:00
|
|
|
let mut parts: Vec<Part> = vec![];
|
|
|
|
loop {
|
2023-07-22 20:52:35 +02:00
|
|
|
let input = match boundary(m.ctype.boundary)(input_loop) {
|
2023-07-22 16:46:36 +02:00
|
|
|
Err(_) => return Ok((input_loop, parts)),
|
2023-07-22 20:52:35 +02:00
|
|
|
Ok((inp, Delimiter::Last)) => return Ok((inp, Part::List(parts))),
|
2023-07-22 16:46:36 +02:00
|
|
|
Ok((inp, Delimiter::Next)) => inp,
|
|
|
|
};
|
2023-07-18 15:00:38 +02:00
|
|
|
|
2023-07-22 16:46:36 +02:00
|
|
|
// parse mime headers
|
2023-07-22 17:40:32 +02:00
|
|
|
let (input, fields) = header_in_boundaries(ctype.boundary, content)(input)?;
|
|
|
|
let mime = fields.to_mime();
|
2023-07-22 20:52:35 +02:00
|
|
|
|
|
|
|
// parse mime body
|
|
|
|
match mime.part_type {
|
|
|
|
Type::Multipart(m) => multipart(m),
|
|
|
|
Type::Message(m) => message(m),
|
|
|
|
Type::Text(t) | Type::Binary
|
|
|
|
}
|
2023-07-18 15:00:38 +02:00
|
|
|
|
2023-07-22 16:46:36 +02:00
|
|
|
// based on headers, parse part
|
2023-07-14 19:12:34 +02:00
|
|
|
|
2023-07-22 16:46:36 +02:00
|
|
|
let input = match part(bound)(input) {
|
|
|
|
Err(_) => return Ok((input, parts)),
|
|
|
|
Ok((inp, part)) => {
|
|
|
|
parts.push(part);
|
|
|
|
inp
|
|
|
|
}
|
|
|
|
};
|
2023-07-14 19:12:34 +02:00
|
|
|
|
2023-07-22 16:46:36 +02:00
|
|
|
input_loop = input;
|
|
|
|
}
|
|
|
|
}
|
2023-07-18 15:00:38 +02:00
|
|
|
}
|
|
|
|
|
2023-07-22 16:46:36 +02:00
|
|
|
pub fn discrete() -> IResult<&[u8], Part> {
|
2023-07-14 19:12:34 +02:00
|
|
|
}
|
|
|
|
|
2023-07-17 17:14:08 +02:00
|
|
|
pub fn part<'a>(bound: &'a [u8]) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], &'a [u8]> {
|
|
|
|
move |input: &[u8]| {
|
|
|
|
recognize(many0(pair(
|
|
|
|
not(boundary(bound)),
|
|
|
|
alt((is_not(CRLF), obs_crlf)),
|
|
|
|
)))(input)
|
|
|
|
}
|
2023-07-17 11:44:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn preamble<'a>(bound: &'a [u8]) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], &'a [u8]> {
|
|
|
|
move |input: &[u8]| {
|
|
|
|
recognize(many0(tuple((
|
|
|
|
is_not(CRLF),
|
|
|
|
many0(pair(not(boundary(bound)), obs_crlf)),
|
|
|
|
))))(input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 15:00:38 +02:00
|
|
|
// FIXME parse email here
|
|
|
|
|
2023-07-14 19:12:34 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2023-07-17 11:44:55 +02:00
|
|
|
#[test]
|
|
|
|
fn test_preamble() {
|
|
|
|
assert_eq!(
|
|
|
|
preamble(b"hello")(b"blip
|
|
|
|
bloup
|
|
|
|
|
|
|
|
blip
|
|
|
|
bloup--
|
|
|
|
--bim
|
|
|
|
--bim--
|
|
|
|
|
|
|
|
--hello
|
|
|
|
Field: Body
|
|
|
|
"),
|
|
|
|
Ok((
|
|
|
|
&b"\n--hello\nField: Body\n"[..],
|
|
|
|
&b"blip\nbloup\n\nblip\nbloup--\n--bim\n--bim--\n"[..],
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
2023-07-17 17:14:08 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_part() {
|
|
|
|
assert_eq!(
|
|
|
|
part(b"simple boundary")(b"Content-type: text/plain; charset=us-ascii
|
|
|
|
|
|
|
|
This is explicitly typed plain US-ASCII text.
|
|
|
|
It DOES end with a linebreak.
|
|
|
|
|
|
|
|
--simple boundary--
|
|
|
|
"),
|
|
|
|
Ok((
|
|
|
|
&b"\n--simple boundary--\n"[..],
|
|
|
|
&b"Content-type: text/plain; charset=us-ascii\n\nThis is explicitly typed plain US-ASCII text.\nIt DOES end with a linebreak.\n"[..],
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multipart() {
|
|
|
|
assert_eq!(
|
|
|
|
multipart(b"simple boundary")(b"This is the preamble. It is to be ignored, though it
|
|
|
|
is a handy place for composition agents to include an
|
|
|
|
explanatory note to non-MIME conformant readers.
|
|
|
|
|
|
|
|
--simple boundary
|
|
|
|
|
|
|
|
This is implicitly typed plain US-ASCII text.
|
|
|
|
It does NOT end with a linebreak.
|
|
|
|
--simple boundary
|
|
|
|
Content-type: text/plain; charset=us-ascii
|
|
|
|
|
|
|
|
This is explicitly typed plain US-ASCII text.
|
|
|
|
It DOES end with a linebreak.
|
|
|
|
|
|
|
|
--simple boundary--
|
|
|
|
|
|
|
|
This is the epilogue. It is also to be ignored.
|
|
|
|
"),
|
|
|
|
Ok((&b"\nThis is the epilogue. It is also to be ignored.\n"[..],
|
|
|
|
vec![
|
|
|
|
&b"\nThis is implicitly typed plain US-ASCII text.\nIt does NOT end with a linebreak."[..],
|
|
|
|
&b"Content-type: text/plain; charset=us-ascii\n\nThis is explicitly typed plain US-ASCII text.\nIt DOES end with a linebreak.\n"[..],
|
|
|
|
]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
}
|
2023-07-14 19:12:34 +02:00
|
|
|
}
|