mirror of
https://github.com/stalwartlabs/mail-parser.git
synced 2026-12-04 00:01:34 +00:00
Support for Content-Type attributes spanning multiple lines.
This commit is contained in:
@@ -53,6 +53,7 @@ struct ContentTypeParser<'x> {
|
||||
is_continuation: bool,
|
||||
is_encoded_attribute: bool,
|
||||
is_escaped: bool,
|
||||
remove_crlf: bool,
|
||||
is_lower_case: bool,
|
||||
is_token_start: bool,
|
||||
}
|
||||
@@ -143,9 +144,22 @@ fn add_value<'x>(parser: &mut ContentTypeParser<'x>, stream: &MessageStream<'x>)
|
||||
|
||||
let has_values = !parser.values.is_empty();
|
||||
let value = if parser.token_start > 0 {
|
||||
Some(String::from_utf8_lossy(
|
||||
&stream.data[parser.token_start - 1..parser.token_end],
|
||||
))
|
||||
let value = &stream.data[parser.token_start - 1..parser.token_end];
|
||||
Some(if !parser.remove_crlf {
|
||||
String::from_utf8_lossy(value)
|
||||
} else {
|
||||
parser.remove_crlf = false;
|
||||
match String::from_utf8(
|
||||
value
|
||||
.iter()
|
||||
.filter(|&&ch| ch != b'\r' && ch != b'\n')
|
||||
.copied()
|
||||
.collect::<Vec<_>>(),
|
||||
) {
|
||||
Ok(value) => value.into(),
|
||||
Err(err) => String::from_utf8_lossy(err.as_bytes()).into_owned().into(),
|
||||
}
|
||||
})
|
||||
} else {
|
||||
if !has_values {
|
||||
return;
|
||||
@@ -265,6 +279,7 @@ pub fn parse_content_type<'x>(stream: &mut MessageStream<'x>) -> HeaderValue<'x>
|
||||
is_lower_case: true,
|
||||
is_token_start: true,
|
||||
is_escaped: false,
|
||||
remove_crlf: false,
|
||||
|
||||
token_start: 0,
|
||||
token_end: 0,
|
||||
@@ -300,46 +315,52 @@ pub fn parse_content_type<'x>(stream: &mut MessageStream<'x>) -> HeaderValue<'x>
|
||||
}
|
||||
}
|
||||
b'\n' => {
|
||||
let next_is_space = matches!(stream.data.get(stream.pos), Some(b' ' | b'\t'));
|
||||
match parser.state {
|
||||
ContentState::Type | ContentState::AttributeName | ContentState::SubType => {
|
||||
add_attribute(&mut parser, stream);
|
||||
}
|
||||
ContentState::AttributeValue | ContentState::AttributeQuotedValue => {
|
||||
ContentState::AttributeValue => {
|
||||
add_value(&mut parser, stream);
|
||||
}
|
||||
ContentState::AttributeQuotedValue => {
|
||||
if next_is_space {
|
||||
parser.remove_crlf = true;
|
||||
continue;
|
||||
} else {
|
||||
add_value(&mut parser, stream);
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
match stream.data.get(stream.pos) {
|
||||
Some(b' ' | b'\t') => {
|
||||
parser.state = ContentState::AttributeName;
|
||||
stream.pos += 1;
|
||||
iter.next();
|
||||
if next_is_space {
|
||||
parser.state = ContentState::AttributeName;
|
||||
stream.pos += 1;
|
||||
iter.next();
|
||||
|
||||
if !parser.is_token_start {
|
||||
parser.is_token_start = true;
|
||||
}
|
||||
continue;
|
||||
if !parser.is_token_start {
|
||||
parser.is_token_start = true;
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
if parser.continuations.is_some() {
|
||||
merge_continuations(&mut parser);
|
||||
}
|
||||
_ => {
|
||||
if parser.continuations.is_some() {
|
||||
merge_continuations(&mut parser);
|
||||
}
|
||||
|
||||
return if let Some(content_type) = parser.c_type {
|
||||
HeaderValue::ContentType(ContentType {
|
||||
c_type: content_type,
|
||||
c_subtype: parser.c_subtype.take(),
|
||||
attributes: if !parser.attributes.is_empty() {
|
||||
Some(parser.attributes)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
})
|
||||
} else {
|
||||
HeaderValue::Empty
|
||||
};
|
||||
}
|
||||
return if let Some(content_type) = parser.c_type {
|
||||
HeaderValue::ContentType(ContentType {
|
||||
c_type: content_type,
|
||||
c_subtype: parser.c_subtype.take(),
|
||||
attributes: if !parser.attributes.is_empty() {
|
||||
Some(parser.attributes)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
})
|
||||
} else {
|
||||
HeaderValue::Empty
|
||||
};
|
||||
}
|
||||
}
|
||||
b'/' if parser.state == ContentState::Type => {
|
||||
@@ -661,6 +682,17 @@ mod tests {
|
||||
" boundary: simple boundary\n"
|
||||
),
|
||||
),
|
||||
(
|
||||
"multipart/mixed; boundary=\"foo\n bar\"\n",
|
||||
concat!(
|
||||
"---\n",
|
||||
"ContentType:\n",
|
||||
" c_type: multipart\n",
|
||||
" c_subtype: mixed\n",
|
||||
" attributes:\n",
|
||||
" boundary: \"foo bar\"\n"
|
||||
),
|
||||
),
|
||||
(
|
||||
"multipart/alternative; boundary=boundary42\n",
|
||||
concat!(
|
||||
@@ -1103,7 +1135,7 @@ mod tests {
|
||||
" c_type: inva\n",
|
||||
" c_subtype: lid\n",
|
||||
" attributes:\n",
|
||||
" name: \" \"\n"
|
||||
" name: \" \"\n"
|
||||
),
|
||||
),
|
||||
(
|
||||
@@ -1115,7 +1147,7 @@ mod tests {
|
||||
" c_subtype: lid\n",
|
||||
" attributes:\n",
|
||||
" test: test\n",
|
||||
" name: \" \"\n"
|
||||
" name: \" \"\n"
|
||||
),
|
||||
),
|
||||
(
|
||||
|
||||
@@ -772,16 +772,16 @@ mod tests {
|
||||
continue;
|
||||
}
|
||||
|
||||
/*if !file_name
|
||||
if !file_name
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.path()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.contains("011")
|
||||
.contains("012")
|
||||
{
|
||||
continue;
|
||||
}*/
|
||||
}
|
||||
|
||||
println!("{:}", file_name.as_ref().unwrap().path().display());
|
||||
let mut input = fs::read(file_name.as_ref().unwrap().path()).unwrap();
|
||||
|
||||
Vendored
+291
@@ -0,0 +1,291 @@
|
||||
From user@domain Fri Feb 22 17:06:23 2008
|
||||
From: user@domain.org
|
||||
Date: Sat, 24 Mar 2007 23:00:00 +0200
|
||||
Mime-Version: 1.0
|
||||
Content-Type: multipart/mixed; boundary="foo
|
||||
bar"
|
||||
|
||||
Root MIME prologue
|
||||
|
||||
--foo bar
|
||||
Content-Type: text/x-myown; charset=us-ascii
|
||||
|
||||
hello
|
||||
|
||||
--foo bar
|
||||
Content-Type: message/rfc822
|
||||
|
||||
From: sub@domain.org
|
||||
Date: Sun, 12 Aug 2012 12:34:56 +0300
|
||||
Subject: submsg
|
||||
Content-Type: multipart/alternative; boundary="sub1"
|
||||
|
||||
Sub MIME prologue
|
||||
--sub1
|
||||
Content-Type: text/html
|
||||
|
||||
<p>Hello world</p>
|
||||
|
||||
--sub1
|
||||
Content-Type: text/plain
|
||||
|
||||
Hello another world
|
||||
|
||||
--sub1--
|
||||
Sub MIME epilogue
|
||||
|
||||
--foo bar--
|
||||
Root MIME epilogue
|
||||
---- EXPECTED STRUCTURE ----{
|
||||
"html_body": [],
|
||||
"text_body": [],
|
||||
"attachments": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"parts": [
|
||||
{
|
||||
"headers_rfc": {
|
||||
"from": {
|
||||
"Address": {
|
||||
"name": null,
|
||||
"address": "user@domain.org"
|
||||
}
|
||||
},
|
||||
"date": {
|
||||
"DateTime": {
|
||||
"year": 2007,
|
||||
"month": 3,
|
||||
"day": 24,
|
||||
"hour": 23,
|
||||
"minute": 0,
|
||||
"second": 0,
|
||||
"tz_before_gmt": false,
|
||||
"tz_hour": 2,
|
||||
"tz_minute": 0
|
||||
}
|
||||
},
|
||||
"mime_version": {
|
||||
"Text": "1.0"
|
||||
},
|
||||
"content_type": {
|
||||
"ContentType": {
|
||||
"c_type": "multipart",
|
||||
"c_subtype": "mixed",
|
||||
"attributes": {
|
||||
"boundary": "foo bar"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"headers_raw": [
|
||||
[
|
||||
{
|
||||
"Other": "From user@domain Fri Feb 22 17"
|
||||
},
|
||||
{
|
||||
"start": 32,
|
||||
"end": 43
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"Rfc": "from"
|
||||
},
|
||||
{
|
||||
"start": 48,
|
||||
"end": 65
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"Rfc": "date"
|
||||
},
|
||||
{
|
||||
"start": 70,
|
||||
"end": 103
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"Rfc": "mime_version"
|
||||
},
|
||||
{
|
||||
"start": 116,
|
||||
"end": 121
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"Rfc": "content_type"
|
||||
},
|
||||
{
|
||||
"start": 134,
|
||||
"end": 172
|
||||
}
|
||||
]
|
||||
],
|
||||
"is_encoding_problem": false,
|
||||
"body": {
|
||||
"Multipart": [
|
||||
1,
|
||||
2
|
||||
]
|
||||
},
|
||||
"offset_header": 0,
|
||||
"offset_body": 173,
|
||||
"offset_end": 589
|
||||
},
|
||||
{
|
||||
"headers_rfc": {
|
||||
"content_type": {
|
||||
"ContentType": {
|
||||
"c_type": "text",
|
||||
"c_subtype": "x-myown",
|
||||
"attributes": {
|
||||
"charset": "us-ascii"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"headers_raw": [
|
||||
[
|
||||
{
|
||||
"Rfc": "content_type"
|
||||
},
|
||||
{
|
||||
"start": 216,
|
||||
"end": 248
|
||||
}
|
||||
]
|
||||
],
|
||||
"is_encoding_problem": false,
|
||||
"body": {
|
||||
"Text": "hello\n"
|
||||
},
|
||||
"offset_header": 203,
|
||||
"offset_body": 249,
|
||||
"offset_end": 256
|
||||
},
|
||||
{
|
||||
"headers_rfc": {
|
||||
"content_type": {
|
||||
"ContentType": {
|
||||
"c_type": "message",
|
||||
"c_subtype": "rfc822",
|
||||
"attributes": null
|
||||
}
|
||||
}
|
||||
},
|
||||
"headers_raw": [
|
||||
[
|
||||
{
|
||||
"Rfc": "content_type"
|
||||
},
|
||||
{
|
||||
"start": 279,
|
||||
"end": 295
|
||||
}
|
||||
]
|
||||
],
|
||||
"is_encoding_problem": false,
|
||||
"body": {
|
||||
"Message": {
|
||||
"html_body": [
|
||||
1
|
||||
],
|
||||
"text_body": [
|
||||
2
|
||||
],
|
||||
"attachments": [],
|
||||
"parts": [
|
||||
{
|
||||
"headers_rfc": {
|
||||
"date": {
|
||||
"DateTime": {
|
||||
"year": 2012,
|
||||
"month": 8,
|
||||
"day": 12,
|
||||
"hour": 12,
|
||||
"minute": 34,
|
||||
"second": 56,
|
||||
"tz_before_gmt": false,
|
||||
"tz_hour": 3,
|
||||
"tz_minute": 0
|
||||
}
|
||||
},
|
||||
"from": {
|
||||
"Address": {
|
||||
"name": null,
|
||||
"address": "sub@domain.org"
|
||||
}
|
||||
},
|
||||
"subject": {
|
||||
"Text": "submsg"
|
||||
},
|
||||
"content_type": {
|
||||
"ContentType": {
|
||||
"c_type": "multipart",
|
||||
"c_subtype": "alternative",
|
||||
"attributes": {
|
||||
"boundary": "sub1"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"is_encoding_problem": false,
|
||||
"body": {
|
||||
"Multipart": [
|
||||
1,
|
||||
2
|
||||
]
|
||||
},
|
||||
"offset_header": 296,
|
||||
"offset_body": 425,
|
||||
"offset_end": 558
|
||||
},
|
||||
{
|
||||
"headers_rfc": {
|
||||
"content_type": {
|
||||
"ContentType": {
|
||||
"c_type": "text",
|
||||
"c_subtype": "html",
|
||||
"attributes": null
|
||||
}
|
||||
}
|
||||
},
|
||||
"is_encoding_problem": false,
|
||||
"body": {
|
||||
"Html": "<p>Hello world</p>\n"
|
||||
},
|
||||
"offset_header": 450,
|
||||
"offset_body": 475,
|
||||
"offset_end": 495
|
||||
},
|
||||
{
|
||||
"headers_rfc": {
|
||||
"content_type": {
|
||||
"ContentType": {
|
||||
"c_type": "text",
|
||||
"c_subtype": "plain",
|
||||
"attributes": null
|
||||
}
|
||||
}
|
||||
},
|
||||
"is_encoding_problem": false,
|
||||
"body": {
|
||||
"Text": "Hello another world\n"
|
||||
},
|
||||
"offset_header": 502,
|
||||
"offset_body": 528,
|
||||
"offset_end": 549
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"offset_header": 266,
|
||||
"offset_body": 296,
|
||||
"offset_end": 577
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user