summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
authorWu Young <doomsplayer@gmail.com>2016-12-23 12:48:19 +0800
committerstaktrace <accounts.github@staktrace.com>2016-12-23 09:05:24 -0500
commit6a34ecdd65058ac6852745aaffd9b0e4fbd875ab (patch)
tree08344cdb685b783e9a457ab1b83cbaae462ac833 /src/lib.rs
parent431a4226d70fb55fe39b131c380e48193f056d6d (diff)
add name field to ParsedContentType
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/lib.rs b/src/lib.rs
index f362919..db8e160 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -483,6 +483,8 @@ pub struct ParsedContentType {
/// the body will actually contain the boundary string prefixed by two
/// dashes.
pub boundary: Option<String>,
+ /// The name of the content, if available
+ pub name: Option<String>,
}
/// Helper method to parse a header value as a Content-Type header. The charset
@@ -508,11 +510,21 @@ pub struct ParsedContentType {
/// assert_eq!(ctype.charset, "us-ascii");
/// assert_eq!(ctype.boundary, None);
/// ```
+/// ```
+/// use mailparse::{parse_header, parse_content_type};
+/// let (parsed, _) = parse_header(br#"Content-Type: application/octet-stream;name="=?utf8?B?6L+O5ai255m95a+M576O?=";charset="utf8""#).unwrap();
+/// let ctype = parse_content_type(&parsed.get_value().unwrap()).unwrap();
+/// assert_eq!(ctype.mimetype, "application/octet-stream");
+/// assert_eq!(ctype.charset, "utf8");
+/// assert_eq!(ctype.boundary, None);
+/// assert_eq!(ctype.name, Some("迎娶白富美".to_string()));
+/// ```
pub fn parse_content_type(header: &str) -> Result<ParsedContentType, MailParseError> {
let mut parsed_type = ParsedContentType {
mimetype: "text/plain".to_string(),
charset: "us-ascii".to_string(),
boundary: None,
+ name: None,
};
let mut tokens = header.split(';');
// There must be at least one token produced by split, even if it's empty.
@@ -524,11 +536,14 @@ pub fn parse_content_type(header: &str) -> Result<ParsedContentType, MailParseEr
if value.starts_with('"') && value.ends_with('"') {
value = &value[1..value.len() - 1];
}
- if attr == "charset" {
- parsed_type.charset = String::from(value).to_lowercase();
- } else if attr == "boundary" {
- parsed_type.boundary = Some(String::from(value));
+
+ match &attr[..] {
+ "charset" => parsed_type.charset = String::from(value).to_lowercase(),
+ "boundary" => parsed_type.boundary = Some(String::from(value)),
+ "name" => parsed_type.name = Some(String::from(value)),
+ _ => {}
}
+
} // else invalid token, ignore. We could throw an error but this
// actually happens in some cases that we want to otherwise handle.
}
@@ -640,6 +655,7 @@ pub fn parse_mail(raw_data: &[u8]) -> Result<ParsedMail, MailParseError> {
mimetype: "text/plain".to_string(),
charset: "us-ascii".to_string(),
boundary: None,
+ name: None,
}
}
};