Create type alias for MIME type.

This commit is contained in:
Corey Farwell 2016-07-02 14:50:46 -04:00
parent 307844a8c1
commit 749fe986aa

View file

@ -48,14 +48,16 @@ pub enum NoSniffFlag {
OFF OFF
} }
pub type MimeType = (String, String);
impl MIMEClassifier { impl MIMEClassifier {
//Performs MIME Type Sniffing Algorithm (sections 7 and 8) //Performs MIME Type Sniffing Algorithm (sections 7 and 8)
pub fn classify(&self, pub fn classify(&self,
context: LoadContext, context: LoadContext,
no_sniff_flag: NoSniffFlag, no_sniff_flag: NoSniffFlag,
apache_bug_flag: ApacheBugFlag, apache_bug_flag: ApacheBugFlag,
supplied_type: &Option<(String, String)>, supplied_type: &Option<MimeType>,
data: &[u8]) -> (String, String) { data: &[u8]) -> MimeType {
let supplied_type_or_octet_stream = supplied_type.clone() let supplied_type_or_octet_stream = supplied_type.clone()
.unwrap_or(("application".to_owned(), .unwrap_or(("application".to_owned(),
"octet-stream".to_owned())); "octet-stream".to_owned()));
@ -177,7 +179,7 @@ impl MIMEClassifier {
} }
//some sort of iterator over the classifiers might be better? //some sort of iterator over the classifiers might be better?
fn sniff_unknown_type(&self, no_sniff_flag: NoSniffFlag, data: &[u8]) -> (String, String) { fn sniff_unknown_type(&self, no_sniff_flag: NoSniffFlag, data: &[u8]) -> MimeType {
let should_sniff_scriptable = no_sniff_flag == NoSniffFlag::OFF; let should_sniff_scriptable = no_sniff_flag == NoSniffFlag::OFF;
let sniffed = if should_sniff_scriptable { let sniffed = if should_sniff_scriptable {
self.scriptable_classifier.classify(data) self.scriptable_classifier.classify(data)
@ -193,7 +195,7 @@ impl MIMEClassifier {
.expect("BinaryOrPlaintextClassifier always succeeds") .expect("BinaryOrPlaintextClassifier always succeeds")
} }
fn sniff_text_or_data(&self, data: &[u8]) -> (String, String) { fn sniff_text_or_data(&self, data: &[u8]) -> MimeType {
self.binary_or_plaintext.classify(data).expect("BinaryOrPlaintextClassifier always succeeds") self.binary_or_plaintext.classify(data).expect("BinaryOrPlaintextClassifier always succeeds")
} }
@ -243,20 +245,20 @@ impl MIMEClassifier {
} }
} }
fn maybe_get_media_type(supplied_type: &Option<(String, String)>) -> Option<MediaType> { fn maybe_get_media_type(supplied_type: &Option<MimeType>) -> Option<MediaType> {
supplied_type.as_ref().and_then(|&(ref media_type, ref media_subtype)| { supplied_type.as_ref().and_then(|&(ref media_type, ref media_subtype)| {
MIMEClassifier::get_media_type(media_type, media_subtype) MIMEClassifier::get_media_type(media_type, media_subtype)
}) })
} }
} }
pub fn as_string_option(tup: Option<(&'static str, &'static str)>) -> Option<(String, String)> { pub fn as_string_option(tup: Option<(&'static str, &'static str)>) -> Option<MimeType> {
tup.map(|(a, b)| (a.to_owned(), b.to_owned())) tup.map(|(a, b)| (a.to_owned(), b.to_owned()))
} }
//Interface used for composite types //Interface used for composite types
trait MIMEChecker { trait MIMEChecker {
fn classify(&self, data: &[u8]) -> Option<(String, String)>; fn classify(&self, data: &[u8]) -> Option<MimeType>;
/// Validate the MIME checker configuration /// Validate the MIME checker configuration
fn validate(&self) -> Result<(), String>; fn validate(&self) -> Result<(), String>;
} }
@ -322,7 +324,7 @@ impl ByteMatcher {
} }
impl MIMEChecker for ByteMatcher { impl MIMEChecker for ByteMatcher {
fn classify(&self, data: &[u8]) -> Option<(String, String)> { fn classify(&self, data: &[u8]) -> Option<MimeType> {
self.matches(data).map(|_| { self.matches(data).map(|_| {
(self.content_type.0.to_owned(), self.content_type.1.to_owned()) (self.content_type.0.to_owned(), self.content_type.1.to_owned())
}) })
@ -358,7 +360,7 @@ struct TagTerminatedByteMatcher {
} }
impl MIMEChecker for TagTerminatedByteMatcher { impl MIMEChecker for TagTerminatedByteMatcher {
fn classify(&self, data: &[u8]) -> Option<(String, String)> { fn classify(&self, data: &[u8]) -> Option<MimeType> {
self.matcher.matches(data).and_then(|j| self.matcher.matches(data).and_then(|j|
if j < data.len() && (data[j] == b' ' || data[j] == b'>') { if j < data.len() && (data[j] == b' ' || data[j] == b'>') {
Some((self.matcher.content_type.0.to_owned(), Some((self.matcher.content_type.0.to_owned(),
@ -399,7 +401,7 @@ impl Mp4Matcher {
} }
impl MIMEChecker for Mp4Matcher { impl MIMEChecker for Mp4Matcher {
fn classify(&self, data: &[u8]) -> Option<(String, String)> { fn classify(&self, data: &[u8]) -> Option<MimeType> {
if self.matches(data) { if self.matches(data) {
Some(("video".to_owned(), "mp4".to_owned())) Some(("video".to_owned(), "mp4".to_owned()))
} else { } else {
@ -432,7 +434,7 @@ impl BinaryOrPlaintextClassifier {
} }
} }
impl MIMEChecker for BinaryOrPlaintextClassifier { impl MIMEChecker for BinaryOrPlaintextClassifier {
fn classify(&self, data: &[u8]) -> Option<(String, String)> { fn classify(&self, data: &[u8]) -> Option<MimeType> {
as_string_option(Some(self.classify_impl(data))) as_string_option(Some(self.classify_impl(data)))
} }
@ -532,7 +534,7 @@ impl GroupedClassifier {
} }
} }
impl MIMEChecker for GroupedClassifier { impl MIMEChecker for GroupedClassifier {
fn classify(&self, data: &[u8]) -> Option<(String, String)> { fn classify(&self, data: &[u8]) -> Option<MimeType> {
self.byte_matchers self.byte_matchers
.iter() .iter()
.filter_map(|matcher| matcher.classify(data)) .filter_map(|matcher| matcher.classify(data))
@ -643,7 +645,7 @@ impl FeedsClassifier {
} }
impl MIMEChecker for FeedsClassifier { impl MIMEChecker for FeedsClassifier {
fn classify(&self, data: &[u8]) -> Option<(String, String)> { fn classify(&self, data: &[u8]) -> Option<MimeType> {
as_string_option(self.classify_impl(data)) as_string_option(self.classify_impl(data))
} }