Skip to content

Commit

Permalink
fixup! Issue #7382 Use descriptive enums instead of booleans for MIME…
Browse files Browse the repository at this point in the history
…Classifier::classifer
  • Loading branch information
leaexplores committed Sep 12, 2015
1 parent 5c80731 commit ff608de
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
21 changes: 13 additions & 8 deletions components/net/mime_classifier.rs
Expand Up @@ -26,6 +26,7 @@ pub enum ApacheBugFlag {
OFF
}

#[derive(PartialEq)]
pub enum NoSniffFlag {
ON,
OFF
Expand Down Expand Up @@ -81,14 +82,18 @@ impl MIMEClassifier {
//some sort of iterator over the classifiers might be better?
fn sniff_unknown_type(&self, no_sniff_flag: NoSniffFlag, data: &[u8]) ->
Option<(String, String)> {
match no_sniff_flag {
NoSniffFlag::OFF => self.scriptable_classifier.classify(data),
_ => None
}.or_else(|| self.plaintext_classifier.classify(data))
.or_else(|| self.image_classifier.classify(data))
.or_else(|| self.audio_video_classifier.classify(data))
.or_else(|| self.archive_classifier.classify(data))
.or_else(|| self.binary_or_plaintext.classify(data))
let should_sniff_scriptable = no_sniff_flag == NoSniffFlag::OFF;
let sniffed = if should_sniff_scriptable {
self.scriptable_classifier.classify(data)
} else {
None
};

sniffed.or_else(|| self.plaintext_classifier.classify(data))
.or_else(|| self.image_classifier.classify(data))
.or_else(|| self.audio_video_classifier.classify(data))
.or_else(|| self.archive_classifier.classify(data))
.or_else(|| self.binary_or_plaintext.classify(data))
}

fn sniff_text_or_data(&self, data: &[u8]) -> Option<(String, String)> {
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/net/mime_classifier.rs
Expand Up @@ -529,3 +529,13 @@ fn test_sniff_octet_stream_apache_flag_on() {
ApacheBugFlag::ON);
}

#[test]
fn test_sniff_mp4_video_apache_flag_on() {
test_sniff_with_flags(&PathBuf::from("video/mp4/test.mp4"),
"video",
"mp4",
None,
NoSniffFlag::OFF,
ApacheBugFlag::ON);
}

0 comments on commit ff608de

Please sign in to comment.