MIME classifier should use &[T] instead of &Vec<T>

This commit is contained in:
Adrián Arroyo Calle 2015-08-10 15:00:35 +02:00
parent 68f6fdd8bc
commit af21c2457e

View file

@ -21,7 +21,7 @@ impl MIMEClassifier {
no_sniff: bool, no_sniff: bool,
check_for_apache_bug: bool, check_for_apache_bug: bool,
supplied_type: &Option<(String, String)>, supplied_type: &Option<(String, String)>,
data: &Vec<u8>) -> Option<(String, String)> { data: &[u8]) -> Option<(String, String)> {
match *supplied_type{ match *supplied_type{
None => { None => {
@ -83,7 +83,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, sniff_scriptable: bool, data: &Vec<u8>) -> fn sniff_unknown_type(&self, sniff_scriptable: bool, data: &[u8]) ->
Option<(String,String)> { Option<(String,String)> {
if sniff_scriptable { if sniff_scriptable {
self.scriptable_classifier.classify(data) self.scriptable_classifier.classify(data)
@ -96,7 +96,7 @@ impl MIMEClassifier {
.or_else(|| self.binary_or_plaintext.classify(data)) .or_else(|| self.binary_or_plaintext.classify(data))
} }
fn sniff_text_or_data(&self, data: &Vec<u8>) -> Option<(String, String)> { fn sniff_text_or_data(&self, data: &[u8]) -> Option<(String, String)> {
self.binary_or_plaintext.classify(data) self.binary_or_plaintext.classify(data)
} }
fn is_xml(tp: &str, sub_tp: &str) -> bool { fn is_xml(tp: &str, sub_tp: &str) -> bool {
@ -117,7 +117,7 @@ pub fn as_string_option(tup: Option<(&'static str, &'static str)>) -> Option<(St
//Interface used for composite types //Interface used for composite types
trait MIMEChecker { trait MIMEChecker {
fn classify(&self, data: &Vec<u8>)->Option<(String, String)>; fn classify(&self, data: &[u8])->Option<(String, String)>;
} }
trait Matches { trait Matches {
@ -159,7 +159,7 @@ struct ByteMatcher {
} }
impl ByteMatcher { impl ByteMatcher {
fn matches(&self, data: &Vec<u8>) -> Option<usize> { fn matches(&self, data: &[u8]) -> Option<usize> {
if data.len() < self.pattern.len() { if data.len() < self.pattern.len() {
return None; return None;
@ -189,7 +189,7 @@ impl ByteMatcher {
} }
impl MIMEChecker for ByteMatcher { impl MIMEChecker for ByteMatcher {
fn classify(&self, data: &Vec<u8>) -> Option<(String, String)> { fn classify(&self, data: &[u8]) -> Option<(String, String)> {
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())
}) })
@ -201,7 +201,7 @@ struct TagTerminatedByteMatcher {
} }
impl MIMEChecker for TagTerminatedByteMatcher { impl MIMEChecker for TagTerminatedByteMatcher {
fn classify(&self, data: &Vec<u8>) -> Option<(String, String)> { fn classify(&self, data: &[u8]) -> Option<(String, String)> {
let pattern = self.matcher.matches(data); let pattern = self.matcher.matches(data);
let pattern_matches = pattern.map(|j| j < data.len() && (data[j] == b' ' || data[j] == b'>')); let pattern_matches = pattern.map(|j| j < data.len() && (data[j] == b' ' || data[j] == b'>'));
if pattern_matches.unwrap_or(false) { if pattern_matches.unwrap_or(false) {
@ -215,7 +215,7 @@ impl MIMEChecker for TagTerminatedByteMatcher {
pub struct Mp4Matcher; pub struct Mp4Matcher;
impl Mp4Matcher { impl Mp4Matcher {
pub fn matches(&self,data: &Vec<u8>) -> bool { pub fn matches(&self,data: &[u8]) -> bool {
if data.len() < 12 { if data.len() < 12 {
return false; return false;
} }
@ -265,7 +265,7 @@ impl Mp4Matcher {
} }
impl MIMEChecker for Mp4Matcher { impl MIMEChecker for Mp4Matcher {
fn classify(&self, data: &Vec<u8>) -> Option<(String, String)> { fn classify(&self, data: &[u8]) -> Option<(String, String)> {
if self.matches(data) { if self.matches(data) {
Some(("video".to_owned(), "mp4".to_owned())) Some(("video".to_owned(), "mp4".to_owned()))
} else { } else {
@ -277,7 +277,7 @@ impl MIMEChecker for Mp4Matcher {
struct BinaryOrPlaintextClassifier; struct BinaryOrPlaintextClassifier;
impl BinaryOrPlaintextClassifier { impl BinaryOrPlaintextClassifier {
fn classify_impl(&self, data: &Vec<u8>) -> Option<(&'static str, &'static str)> { fn classify_impl(&self, data: &[u8]) -> Option<(&'static str, &'static str)> {
if (data.len() >=2 && if (data.len() >=2 &&
((data[0] == 0xFFu8 && data[1] == 0xFEu8) || ((data[0] == 0xFFu8 && data[1] == 0xFEu8) ||
(data[0] == 0xFEu8 && data[1] == 0xFFu8))) || (data[0] == 0xFEu8 && data[1] == 0xFFu8))) ||
@ -297,7 +297,7 @@ impl BinaryOrPlaintextClassifier {
} }
} }
impl MIMEChecker for BinaryOrPlaintextClassifier { impl MIMEChecker for BinaryOrPlaintextClassifier {
fn classify(&self, data: &Vec<u8>) -> Option<(String, String)> { fn classify(&self, data: &[u8]) -> Option<(String, String)> {
return as_string_option(self.classify_impl(data)); return as_string_option(self.classify_impl(data));
} }
} }
@ -395,7 +395,7 @@ impl GroupedClassifier {
} }
} }
impl MIMEChecker for GroupedClassifier { impl MIMEChecker for GroupedClassifier {
fn classify(&self,data: &Vec<u8>) -> Option<(String, String)> { fn classify(&self,data: &[u8]) -> Option<(String, String)> {
self.byte_matchers self.byte_matchers
.iter() .iter()
.filter_map(|matcher| matcher.classify(data)) .filter_map(|matcher| matcher.classify(data))
@ -405,7 +405,7 @@ impl MIMEChecker for GroupedClassifier {
struct FeedsClassifier; struct FeedsClassifier;
impl FeedsClassifier { impl FeedsClassifier {
fn classify_impl(&self,data: &Vec<u8>) -> Option<(&'static str,&'static str)> { fn classify_impl(&self,data: &[u8]) -> Option<(&'static str,&'static str)> {
let length = data.len(); let length = data.len();
let mut data_iterator = data.iter(); let mut data_iterator = data.iter();
@ -469,7 +469,7 @@ impl FeedsClassifier {
} }
impl MIMEChecker for FeedsClassifier { impl MIMEChecker for FeedsClassifier {
fn classify(&self,data: &Vec<u8>) -> Option<(String, String)> { fn classify(&self,data: &[u8]) -> Option<(String, String)> {
as_string_option(self.classify_impl(data)) as_string_option(self.classify_impl(data))
} }
} }