Fixed raw offsets of multipart/* parts to include MIME epilogue.

This commit is contained in:
Mauro D committed 2022-10-10 07:56:30 +00:00
1 parent f544f729be
commit 7c4db43aca
9 files changed
+351 -17

No files matched your search

+1
View File
@@ -3,6 +3,7 @@ mail-parser 0.6.2
- Base64/QuotedPrintable decoding optimizations.
- Added "ludicrous mode" Cargo option to use some unsafe code for additional performance.
- Fixed support for empty messages.
- Fixed raw offsets of multipart/* parts to include MIME epilogue.
mail-parser 0.6.1
================================
+16 -11
View File
@@ -16,6 +16,7 @@ use crate::{
base64::decode_base64_mime, charsets::map::get_charset_decoder,
quoted_printable::decode_quoted_printable_mime, DecodeFnc,
},
parsers::mime::seek_next_part_offset,
ContentType, Encoding, GetHeader, HeaderValue, Message, MessageAttachment, MessagePart,
MessagePartId, PartType, RfcHeader,
};
@@ -454,25 +455,29 @@ impl<'x> Message<'x> {
}
if let Some(part) = message.parts.get_mut(state.part_id) {
// Update end offset
part.offset_end = seek_crlf(&stream, stream.pos);
// Add headers and substructure to parent part
part.body =
PartType::Multipart(std::mem::take(&mut state.sub_part_ids));
} else {
debug_assert!(false, "Invalid part ID, could not find multipart.");
}
if let Some((prev_state, _)) = state_stack.pop() {
// Restore ancestor's state
state = prev_state;
if let Some((prev_state, _)) = state_stack.pop() {
state = prev_state;
if let Some(ref mime_boundary) = state.mime_boundary {
// Ancestor has a MIME boundary, seek it.
if seek_next_part(&mut stream, mime_boundary) {
continue 'inner;
if let Some(ref mime_boundary) = state.mime_boundary {
// Ancestor has a MIME boundary, seek it.
if let Some(offset) =
seek_next_part_offset(&mut stream, mime_boundary)
{
part.offset_end = offset;
continue 'inner;
}
}
}
// This part has no boundary, update end offset
part.offset_end = seek_crlf(&stream, stream.pos);
} else {
debug_assert!(false, "Invalid part ID, could not find multipart.");
}
break 'outer;
+25
View File
@@ -34,6 +34,31 @@ pub fn seek_next_part(stream: &mut MessageStream, boundary: &[u8]) -> bool {
false
}
pub fn seek_next_part_offset(stream: &mut MessageStream, boundary: &[u8]) -> Option<usize> {
let mut pos = stream.pos;
let mut last_ch = b'\n';
let mut iter = stream.data[stream.pos..].iter().peekable();
let mut offset_pos = stream.pos;
while let Some(&ch) = iter.next() {
pos += 1;
if ch == b'\n' {
offset_pos = if last_ch == b'\r' { pos - 2 } else { pos - 1 };
} else if ch == b'-'
&& matches!(iter.peek(), Some(b'-'))
&& stream.data.get(pos + 1..pos + 1 + boundary.len()) == Some(boundary)
{
stream.pos = pos + boundary.len() + 1;
return offset_pos.into();
}
last_ch = ch;
}
None
}
pub fn get_mime_part<'x>(
stream: &mut MessageStream<'x>,
boundary: &[u8],
+1 -1
View File
@@ -69,7 +69,7 @@
},
"offset_header": 53,
"offset_body": 102,
"offset_end": 151
"offset_end": 181
},
{
"headers": [
+39
View File
@@ -0,0 +1,39 @@
From: Whomever <whoever@example.com>
To: Someone <someone@example.com>
Date: Sat, 10 Oct 2009 00:30:04 +0200
Subject: whatever
Content-Type: multipart/mixed; boundary=outer
This is a multi-part message in MIME format.
--outer
Content-Type: multipart/alternative; boundary=inner
This is a nested multi-part message in MIME format.
--inner
Content-Type: text/plain; charset="us-ascii"
Hello
--inner
Content-Type: text/html; charset="us-ascii"
<html><body>Hello</body></html>
--inner--
This is the end of the inner MIME multipart.
--outer
Content-Type: message/rfc822
From: Someone Else
Subject: Hello, this is an elaborate request for you to finally say hello
already!
Please say Hello
--outer--
This is the end of the outer MIME multipart.
+264
View File
@@ -0,0 +1,264 @@
{
"html_body": [
3
],
"text_body": [
2
],
"attachments": [
4
],
"parts": [
{
"headers": [
{
"name": {
"Rfc": "from"
},
"value": {
"Address": {
"name": "Whomever",
"address": "whoever@example.com"
}
},
"offset_start": 5,
"offset_end": 37
},
{
"name": {
"Rfc": "to"
},
"value": {
"Address": {
"name": "Someone",
"address": "someone@example.com"
}
},
"offset_start": 40,
"offset_end": 71
},
{
"name": {
"Rfc": "date"
},
"value": {
"DateTime": {
"year": 2009,
"month": 10,
"day": 10,
"hour": 0,
"minute": 30,
"second": 4,
"tz_before_gmt": false,
"tz_hour": 2,
"tz_minute": 0
}
},
"offset_start": 76,
"offset_end": 109
},
{
"name": {
"Rfc": "subject"
},
"value": {
"Text": "whatever"
},
"offset_start": 117,
"offset_end": 127
},
{
"name": {
"Rfc": "content_type"
},
"value": {
"ContentType": {
"c_type": "multipart",
"c_subtype": "mixed",
"attributes": [
[
"boundary",
"outer"
]
]
}
},
"offset_start": 140,
"offset_end": 173
}
],
"is_encoding_problem": false,
"body": {
"Multipart": [
1,
4
]
},
"offset_header": 0,
"offset_body": 174,
"offset_end": 754
},
{
"headers": [
{
"name": {
"Rfc": "content_type"
},
"value": {
"ContentType": {
"c_type": "multipart",
"c_subtype": "alternative",
"attributes": [
[
"boundary",
"inner"
]
]
}
},
"offset_start": 241,
"offset_end": 280
}
],
"is_encoding_problem": false,
"body": {
"Multipart": [
2,
3
]
},
"offset_header": 228,
"offset_body": 281,
"offset_end": 537
},
{
"headers": [
{
"name": {
"Rfc": "content_type"
},
"value": {
"ContentType": {
"c_type": "text",
"c_subtype": "plain",
"attributes": [
[
"charset",
"us-ascii"
]
]
}
},
"offset_start": 355,
"offset_end": 387
}
],
"is_encoding_problem": false,
"body": {
"Text": "Hello\n"
},
"offset_header": 342,
"offset_body": 388,
"offset_end": 394
},
{
"headers": [
{
"name": {
"Rfc": "content_type"
},
"value": {
"ContentType": {
"c_type": "text",
"c_subtype": "html",
"attributes": [
[
"charset",
"us-ascii"
]
]
}
},
"offset_start": 416,
"offset_end": 447
}
],
"is_encoding_problem": false,
"body": {
"Html": "<html><body>Hello</body></html>\n"
},
"offset_header": 403,
"offset_body": 448,
"offset_end": 480
},
{
"headers": [
{
"name": {
"Rfc": "content_type"
},
"value": {
"ContentType": {
"c_type": "message",
"c_subtype": "rfc822",
"attributes": null
}
},
"offset_start": 559,
"offset_end": 575
}
],
"is_encoding_problem": false,
"body": {
"Message": {
"html_body": [
0
],
"text_body": [
0
],
"attachments": [],
"parts": [
{
"headers": [
{
"name": {
"Rfc": "from"
},
"value": {
"Address": {
"name": "Someone Else",
"address": null
}
},
"offset_start": 581,
"offset_end": 595
},
{
"name": {
"Rfc": "subject"
},
"value": {
"Text": "Hello, this is an elaborate request for you to finally say hello already!"
},
"offset_start": 603,
"offset_end": 679
}
],
"is_encoding_problem": false,
"body": {
"Text": "Please say Hello\n"
},
"offset_header": 576,
"offset_body": 680,
"offset_end": 697
}
]
}
},
"offset_header": 546,
"offset_body": 576,
"offset_end": 697
}
]
}
+1 -1
View File
@@ -231,7 +231,7 @@
},
"offset_header": 246,
"offset_body": 343,
"offset_end": 565
"offset_end": 564
},
{
"headers": [
+3 -3
View File
@@ -232,7 +232,7 @@
},
"offset_header": 295,
"offset_body": 424,
"offset_end": 710
"offset_end": 728
},
{
"headers": [
@@ -290,7 +290,7 @@
},
"offset_header": 501,
"offset_body": 555,
"offset_end": 701
"offset_end": 700
},
{
"headers": [
@@ -323,7 +323,7 @@
},
"offset_header": 562,
"offset_body": 616,
"offset_end": 662
"offset_end": 672
},
{
"headers": [],
+1 -1
View File
@@ -224,7 +224,7 @@
},
"offset_header": 296,
"offset_body": 425,
"offset_end": 558
"offset_end": 576
},
{
"headers": [