Auto merge of #12190 - frewsxcv:mime-renaming, r=jdm

Minor MIME renaming and aliasing cleanup.

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12190)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-07-03 11:07:12 -07:00 committed by GitHub
commit 194fb3e199
11 changed files with 61 additions and 59 deletions

View file

@ -6,7 +6,7 @@ use file_loader;
use hyper::header::ContentType; use hyper::header::ContentType;
use hyper::http::RawStatus; use hyper::http::RawStatus;
use hyper::mime::{Mime, SubLevel, TopLevel}; use hyper::mime::{Mime, SubLevel, TopLevel};
use mime_classifier::MIMEClassifier; use mime_classifier::MimeClassifier;
use net_traits::ProgressMsg::Done; use net_traits::ProgressMsg::Done;
use net_traits::response::HttpsState; use net_traits::response::HttpsState;
use net_traits::{LoadConsumer, LoadData, Metadata, NetworkError}; use net_traits::{LoadConsumer, LoadData, Metadata, NetworkError};
@ -24,7 +24,7 @@ fn url_from_non_relative_scheme(load_data: &mut LoadData, filename: &str) {
pub fn factory(mut load_data: LoadData, pub fn factory(mut load_data: LoadData,
start_chan: LoadConsumer, start_chan: LoadConsumer,
classifier: Arc<MIMEClassifier>, classifier: Arc<MimeClassifier>,
cancel_listener: CancellationListener) { cancel_listener: CancellationListener) {
let url = load_data.url.clone(); let url = load_data.url.clone();
match url.path() { match url.path() {

View file

@ -7,7 +7,7 @@ use hyper::header::{DispositionType, ContentDisposition, DispositionParam};
use hyper::header::{Headers, ContentType, ContentLength, Charset}; use hyper::header::{Headers, ContentType, ContentLength, Charset};
use hyper::http::RawStatus; use hyper::http::RawStatus;
use mime::{Mime, Attr}; use mime::{Mime, Attr};
use mime_classifier::MIMEClassifier; use mime_classifier::MimeClassifier;
use net_traits::ProgressMsg::Done; use net_traits::ProgressMsg::Done;
use net_traits::blob_url_store::{parse_blob_url, BlobURLStoreEntry, BlobURLStoreError}; use net_traits::blob_url_store::{parse_blob_url, BlobURLStoreEntry, BlobURLStoreError};
use net_traits::response::HttpsState; use net_traits::response::HttpsState;
@ -22,7 +22,7 @@ use std::sync::{Arc, RwLock};
pub fn load(load_data: LoadData, consumer: LoadConsumer, pub fn load(load_data: LoadData, consumer: LoadConsumer,
blob_url_store: Arc<RwLock<BlobURLStore>>, blob_url_store: Arc<RwLock<BlobURLStore>>,
classifier: Arc<MIMEClassifier>) { // XXX: Move it into net process later classifier: Arc<MimeClassifier>) { // XXX: Move it into net process later
match parse_blob_url(&load_data.url) { match parse_blob_url(&load_data.url) {
None => { None => {
@ -48,7 +48,7 @@ pub fn load(load_data: LoadData, consumer: LoadConsumer,
fn load_blob(load_data: &LoadData, fn load_blob(load_data: &LoadData,
start_chan: LoadConsumer, start_chan: LoadConsumer,
classifier: Arc<MIMEClassifier>, classifier: Arc<MimeClassifier>,
entry: &BlobURLStoreEntry) { entry: &BlobURLStoreEntry) {
let content_type: Mime = entry.type_string.parse().unwrap_or(mime!(Text / Plain)); let content_type: Mime = entry.type_string.parse().unwrap_or(mime!(Text / Plain));
let charset = content_type.get_param(Attr::Charset); let charset = content_type.get_param(Attr::Charset);

View file

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use file_loader; use file_loader;
use mime_classifier::MIMEClassifier; use mime_classifier::MimeClassifier;
use net_traits::{LoadConsumer, LoadData, NetworkError}; use net_traits::{LoadConsumer, LoadData, NetworkError};
use resource_thread::{CancellationListener, send_error}; use resource_thread::{CancellationListener, send_error};
use std::fs::canonicalize; use std::fs::canonicalize;
@ -37,7 +37,7 @@ pub fn resolve_chrome_url(url: &Url) -> Result<Url, ()> {
pub fn factory(mut load_data: LoadData, pub fn factory(mut load_data: LoadData,
start_chan: LoadConsumer, start_chan: LoadConsumer,
classifier: Arc<MIMEClassifier>, classifier: Arc<MimeClassifier>,
cancel_listener: CancellationListener) { cancel_listener: CancellationListener) {
let file_url = match resolve_chrome_url(&load_data.url) { let file_url = match resolve_chrome_url(&load_data.url) {
Ok(url) => url, Ok(url) => url,

View file

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use hyper::mime::{Mime, TopLevel, SubLevel, Attr, Value}; use hyper::mime::{Mime, TopLevel, SubLevel, Attr, Value};
use mime_classifier::MIMEClassifier; use mime_classifier::MimeClassifier;
use net_traits::LoadConsumer; use net_traits::LoadConsumer;
use net_traits::ProgressMsg::{Payload, Done}; use net_traits::ProgressMsg::{Payload, Done};
use net_traits::{LoadData, Metadata, NetworkError}; use net_traits::{LoadData, Metadata, NetworkError};
@ -15,7 +15,7 @@ use url::{Position, Url};
pub fn factory(load_data: LoadData, pub fn factory(load_data: LoadData,
senders: LoadConsumer, senders: LoadConsumer,
classifier: Arc<MIMEClassifier>, classifier: Arc<MimeClassifier>,
cancel_listener: CancellationListener) { cancel_listener: CancellationListener) {
// NB: we don't spawn a new thread. // NB: we don't spawn a new thread.
// Hypothesis: data URLs are too small for parallel base64 etc. to be worth it. // Hypothesis: data URLs are too small for parallel base64 etc. to be worth it.
@ -72,7 +72,7 @@ pub fn decode(url: &Url) -> Result<DecodeData, DecodeError> {
pub fn load(load_data: LoadData, pub fn load(load_data: LoadData,
start_chan: LoadConsumer, start_chan: LoadConsumer,
classifier: Arc<MIMEClassifier>, classifier: Arc<MimeClassifier>,
cancel_listener: CancellationListener) { cancel_listener: CancellationListener) {
let url = load_data.url; let url = load_data.url;

View file

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use about_loader; use about_loader;
use mime_classifier::MIMEClassifier; use mime_classifier::MimeClassifier;
use mime_guess::guess_mime_type; use mime_guess::guess_mime_type;
use msg::constellation_msg::{PipelineId, ReferrerPolicy}; use msg::constellation_msg::{PipelineId, ReferrerPolicy};
use net_traits::ProgressMsg::{Done, Payload}; use net_traits::ProgressMsg::{Done, Payload};
@ -72,7 +72,7 @@ fn read_all(reader: &mut File, progress_chan: &ProgressSender, cancel_listener:
} }
fn get_progress_chan(load_data: LoadData, file_path: PathBuf, fn get_progress_chan(load_data: LoadData, file_path: PathBuf,
senders: LoadConsumer, classifier: Arc<MIMEClassifier>, buf: &[u8]) senders: LoadConsumer, classifier: Arc<MimeClassifier>, buf: &[u8])
-> Result<ProgressSender, ()> { -> Result<ProgressSender, ()> {
let mut metadata = Metadata::default(load_data.url); let mut metadata = Metadata::default(load_data.url);
let mime_type = guess_mime_type(file_path.as_path()); let mime_type = guess_mime_type(file_path.as_path());
@ -82,7 +82,7 @@ fn get_progress_chan(load_data: LoadData, file_path: PathBuf,
pub fn factory(load_data: LoadData, pub fn factory(load_data: LoadData,
senders: LoadConsumer, senders: LoadConsumer,
classifier: Arc<MIMEClassifier>, classifier: Arc<MimeClassifier>,
cancel_listener: CancellationListener) { cancel_listener: CancellationListener) {
assert!(load_data.url.scheme() == "file"); assert!(load_data.url.scheme() == "file");
spawn_named("file_loader".to_owned(), move || { spawn_named("file_loader".to_owned(), move || {

View file

@ -4,7 +4,7 @@
use blob_loader; use blob_loader;
use ipc_channel::ipc::{self, IpcReceiver, IpcSender}; use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use mime_classifier::MIMEClassifier; use mime_classifier::MimeClassifier;
use mime_guess::guess_mime_type_opt; use mime_guess::guess_mime_type_opt;
use net_traits::blob_url_store::{BlobURLStoreEntry, BlobURLStoreError, BlobURLStoreMsg}; use net_traits::blob_url_store::{BlobURLStoreEntry, BlobURLStoreError, BlobURLStoreMsg};
use net_traits::filemanager_thread::{FileManagerThreadMsg, FileManagerResult, FilterPattern}; use net_traits::filemanager_thread::{FileManagerThreadMsg, FileManagerResult, FilterPattern};
@ -90,7 +90,7 @@ impl<UI: 'static + UIProvider> FileManagerThreadFactory<UI> for IpcSender<FileMa
struct FileManager<UI: 'static + UIProvider> { struct FileManager<UI: 'static + UIProvider> {
receiver: IpcReceiver<FileManagerThreadMsg>, receiver: IpcReceiver<FileManagerThreadMsg>,
idmap: HashMap<Uuid, PathBuf>, idmap: HashMap<Uuid, PathBuf>,
classifier: Arc<MIMEClassifier>, classifier: Arc<MimeClassifier>,
blob_url_store: Arc<RwLock<BlobURLStore>>, blob_url_store: Arc<RwLock<BlobURLStore>>,
ui: &'static UI, ui: &'static UI,
} }
@ -100,7 +100,7 @@ impl<UI: 'static + UIProvider> FileManager<UI> {
FileManager { FileManager {
receiver: recv, receiver: recv,
idmap: HashMap::new(), idmap: HashMap::new(),
classifier: Arc::new(MIMEClassifier::new()), classifier: Arc::new(MimeClassifier::new()),
blob_url_store: Arc::new(RwLock::new(BlobURLStore::new())), blob_url_store: Arc::new(RwLock::new(BlobURLStore::new())),
ui: ui ui: ui
} }

View file

@ -26,7 +26,7 @@ use hyper::net::Fresh;
use hyper::status::{StatusClass, StatusCode}; use hyper::status::{StatusClass, StatusCode};
use ipc_channel::ipc; use ipc_channel::ipc;
use log; use log;
use mime_classifier::MIMEClassifier; use mime_classifier::MimeClassifier;
use msg::constellation_msg::{PipelineId, ReferrerPolicy}; use msg::constellation_msg::{PipelineId, ReferrerPolicy};
use net_traits::ProgressMsg::{Done, Payload}; use net_traits::ProgressMsg::{Done, Payload};
use net_traits::hosts::replace_hosts; use net_traits::hosts::replace_hosts;
@ -62,7 +62,7 @@ pub fn factory(user_agent: String,
connector: Arc<Pool<Connector>>) connector: Arc<Pool<Connector>>)
-> Box<FnBox(LoadData, -> Box<FnBox(LoadData,
LoadConsumer, LoadConsumer,
Arc<MIMEClassifier>, Arc<MimeClassifier>,
CancellationListener) + Send> { CancellationListener) + Send> {
box move |load_data: LoadData, senders, classifier, cancel_listener| { box move |load_data: LoadData, senders, classifier, cancel_listener| {
spawn_named(format!("http_loader for {}", load_data.url), move || { spawn_named(format!("http_loader for {}", load_data.url), move || {
@ -127,7 +127,7 @@ fn precise_time_ms() -> u64 {
fn load_for_consumer(load_data: LoadData, fn load_for_consumer(load_data: LoadData,
start_chan: LoadConsumer, start_chan: LoadConsumer,
classifier: Arc<MIMEClassifier>, classifier: Arc<MimeClassifier>,
connector: Arc<Pool<Connector>>, connector: Arc<Pool<Connector>>,
http_state: HttpState, http_state: HttpState,
devtools_chan: Option<Sender<DevtoolsControlMsg>>, devtools_chan: Option<Sender<DevtoolsControlMsg>>,
@ -1093,7 +1093,7 @@ fn send_data<R: Read>(context: LoadContext,
reader: &mut R, reader: &mut R,
start_chan: LoadConsumer, start_chan: LoadConsumer,
metadata: Metadata, metadata: Metadata,
classifier: Arc<MIMEClassifier>, classifier: Arc<MimeClassifier>,
cancel_listener: &CancellationListener) { cancel_listener: &CancellationListener) {
let (progress_chan, mut chunk) = { let (progress_chan, mut chunk) = {
let buf = match read_block(reader) { let buf = match read_block(reader) {

View file

@ -5,7 +5,7 @@
use net_traits::LoadContext; use net_traits::LoadContext;
use std::borrow::ToOwned; use std::borrow::ToOwned;
pub struct MIMEClassifier { pub struct MimeClassifier {
image_classifier: GroupedClassifier, image_classifier: GroupedClassifier,
audio_video_classifier: GroupedClassifier, audio_video_classifier: GroupedClassifier,
scriptable_classifier: GroupedClassifier, scriptable_classifier: GroupedClassifier,
@ -48,14 +48,16 @@ pub enum NoSniffFlag {
OFF OFF
} }
impl MIMEClassifier { pub type MimeType = (String, String);
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()));
@ -64,14 +66,14 @@ impl MIMEClassifier {
None => self.sniff_unknown_type(no_sniff_flag, data), None => self.sniff_unknown_type(no_sniff_flag, data),
Some(ref supplied_type) => { Some(ref supplied_type) => {
let &(ref media_type, ref media_subtype) = supplied_type; let &(ref media_type, ref media_subtype) = supplied_type;
if MIMEClassifier::is_explicit_unknown(media_type, media_subtype) { if MimeClassifier::is_explicit_unknown(media_type, media_subtype) {
self.sniff_unknown_type(no_sniff_flag, data) self.sniff_unknown_type(no_sniff_flag, data)
} else { } else {
match no_sniff_flag { match no_sniff_flag {
NoSniffFlag::ON => supplied_type.clone(), NoSniffFlag::ON => supplied_type.clone(),
NoSniffFlag::OFF => match apache_bug_flag { NoSniffFlag::OFF => match apache_bug_flag {
ApacheBugFlag::ON => self.sniff_text_or_data(data), ApacheBugFlag::ON => self.sniff_text_or_data(data),
ApacheBugFlag::OFF => match MIMEClassifier::get_media_type(media_type, ApacheBugFlag::OFF => match MimeClassifier::get_media_type(media_type,
media_subtype) { media_subtype) {
Some(MediaType::Html) => self.feeds_classifier.classify(data), Some(MediaType::Html) => self.feeds_classifier.classify(data),
Some(MediaType::Image) => self.image_classifier.classify(data), Some(MediaType::Image) => self.image_classifier.classify(data),
@ -85,14 +87,14 @@ impl MIMEClassifier {
}, },
LoadContext::Image => { LoadContext::Image => {
// Section 8.2 Sniffing an image context // Section 8.2 Sniffing an image context
match MIMEClassifier::maybe_get_media_type(supplied_type) { match MimeClassifier::maybe_get_media_type(supplied_type) {
Some(MediaType::Xml) => None, Some(MediaType::Xml) => None,
_ => self.image_classifier.classify(data), _ => self.image_classifier.classify(data),
}.unwrap_or(supplied_type_or_octet_stream) }.unwrap_or(supplied_type_or_octet_stream)
}, },
LoadContext::AudioVideo => { LoadContext::AudioVideo => {
// Section 8.3 Sniffing an image context // Section 8.3 Sniffing an image context
match MIMEClassifier::maybe_get_media_type(supplied_type) { match MimeClassifier::maybe_get_media_type(supplied_type) {
Some(MediaType::Xml) => None, Some(MediaType::Xml) => None,
_ => self.audio_video_classifier.classify(data), _ => self.audio_video_classifier.classify(data),
}.unwrap_or(supplied_type_or_octet_stream) }.unwrap_or(supplied_type_or_octet_stream)
@ -129,7 +131,7 @@ impl MIMEClassifier {
}, },
LoadContext::Font => { LoadContext::Font => {
// 8.7 Sniffing in a font context // 8.7 Sniffing in a font context
match MIMEClassifier::maybe_get_media_type(supplied_type) { match MimeClassifier::maybe_get_media_type(supplied_type) {
Some(MediaType::Xml) => None, Some(MediaType::Xml) => None,
_ => self.font_classifier.classify(data), _ => self.font_classifier.classify(data),
}.unwrap_or(supplied_type_or_octet_stream) }.unwrap_or(supplied_type_or_octet_stream)
@ -151,8 +153,8 @@ impl MIMEClassifier {
} }
} }
pub fn new() -> MIMEClassifier { pub fn new() -> MimeClassifier {
MIMEClassifier { MimeClassifier {
image_classifier: GroupedClassifier::image_classifer(), image_classifier: GroupedClassifier::image_classifer(),
audio_video_classifier: GroupedClassifier::audio_video_classifier(), audio_video_classifier: GroupedClassifier::audio_video_classifier(),
scriptable_classifier: GroupedClassifier::scriptable_classifier(), scriptable_classifier: GroupedClassifier::scriptable_classifier(),
@ -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")
} }
@ -230,33 +232,33 @@ impl MIMEClassifier {
fn get_media_type(media_type: &str, fn get_media_type(media_type: &str,
media_subtype: &str) -> Option<MediaType> { media_subtype: &str) -> Option<MediaType> {
if MIMEClassifier::is_xml(media_type, media_subtype) { if MimeClassifier::is_xml(media_type, media_subtype) {
Some(MediaType::Xml) Some(MediaType::Xml)
} else if MIMEClassifier::is_html(media_type, media_subtype) { } else if MimeClassifier::is_html(media_type, media_subtype) {
Some(MediaType::Html) Some(MediaType::Html)
} else if MIMEClassifier::is_image(media_type) { } else if MimeClassifier::is_image(media_type) {
Some(MediaType::Image) Some(MediaType::Image)
} else if MIMEClassifier::is_audio_video(media_type, media_subtype) { } else if MimeClassifier::is_audio_video(media_type, media_subtype) {
Some(MediaType::AudioVideo) Some(MediaType::AudioVideo)
} else { } else {
None None
} }
} }
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))
} }

View file

@ -21,7 +21,7 @@ use hyper::client::pool::Pool;
use hyper::header::{ContentType, Header, SetCookie}; use hyper::header::{ContentType, Header, SetCookie};
use hyper::mime::{Mime, SubLevel, TopLevel}; use hyper::mime::{Mime, SubLevel, TopLevel};
use ipc_channel::ipc::{self, IpcReceiver, IpcSender, IpcReceiverSet}; use ipc_channel::ipc::{self, IpcReceiver, IpcSender, IpcReceiverSet};
use mime_classifier::{ApacheBugFlag, MIMEClassifier, NoSniffFlag}; use mime_classifier::{ApacheBugFlag, MimeClassifier, NoSniffFlag};
use net_traits::LoadContext; use net_traits::LoadContext;
use net_traits::ProgressMsg::Done; use net_traits::ProgressMsg::Done;
use net_traits::filemanager_thread::FileManagerThreadMsg; use net_traits::filemanager_thread::FileManagerThreadMsg;
@ -95,7 +95,7 @@ pub fn send_error(url: Url, err: NetworkError, start_chan: LoadConsumer) {
/// For use by loaders in responding to a Load message that allows content sniffing. /// For use by loaders in responding to a Load message that allows content sniffing.
pub fn start_sending_sniffed(start_chan: LoadConsumer, metadata: Metadata, pub fn start_sending_sniffed(start_chan: LoadConsumer, metadata: Metadata,
classifier: Arc<MIMEClassifier>, partial_body: &[u8], classifier: Arc<MimeClassifier>, partial_body: &[u8],
context: LoadContext) context: LoadContext)
-> ProgressSender { -> ProgressSender {
start_sending_sniffed_opt(start_chan, metadata, classifier, partial_body, context).ok().unwrap() start_sending_sniffed_opt(start_chan, metadata, classifier, partial_body, context).ok().unwrap()
@ -103,7 +103,7 @@ pub fn start_sending_sniffed(start_chan: LoadConsumer, metadata: Metadata,
/// For use by loaders in responding to a Load message that allows content sniffing. /// For use by loaders in responding to a Load message that allows content sniffing.
pub fn start_sending_sniffed_opt(start_chan: LoadConsumer, mut metadata: Metadata, pub fn start_sending_sniffed_opt(start_chan: LoadConsumer, mut metadata: Metadata,
classifier: Arc<MIMEClassifier>, partial_body: &[u8], classifier: Arc<MimeClassifier>, partial_body: &[u8],
context: LoadContext) context: LoadContext)
-> Result<ProgressSender, ()> { -> Result<ProgressSender, ()> {
if PREFS.get("network.mime.sniff").as_boolean().unwrap_or(false) { if PREFS.get("network.mime.sniff").as_boolean().unwrap_or(false) {
@ -459,7 +459,7 @@ pub struct AuthCache {
pub struct CoreResourceManager { pub struct CoreResourceManager {
user_agent: String, user_agent: String,
mime_classifier: Arc<MIMEClassifier>, mime_classifier: Arc<MimeClassifier>,
devtools_chan: Option<Sender<DevtoolsControlMsg>>, devtools_chan: Option<Sender<DevtoolsControlMsg>>,
profiler_chan: ProfilerChan, profiler_chan: ProfilerChan,
cancel_load_map: HashMap<ResourceId, Sender<()>>, cancel_load_map: HashMap<ResourceId, Sender<()>>,
@ -472,7 +472,7 @@ impl CoreResourceManager {
profiler_chan: ProfilerChan) -> CoreResourceManager { profiler_chan: ProfilerChan) -> CoreResourceManager {
CoreResourceManager { CoreResourceManager {
user_agent: user_agent, user_agent: user_agent,
mime_classifier: Arc::new(MIMEClassifier::new()), mime_classifier: Arc::new(MimeClassifier::new()),
devtools_chan: devtools_channel, devtools_chan: devtools_channel,
profiler_chan: profiler_chan, profiler_chan: profiler_chan,
cancel_load_map: HashMap::new(), cancel_load_map: HashMap::new(),
@ -510,10 +510,10 @@ impl CoreResourceManager {
id_sender: Option<IpcSender<ResourceId>>, id_sender: Option<IpcSender<ResourceId>>,
resource_thread: CoreResourceThread, resource_thread: CoreResourceThread,
resource_grp: &ResourceGroup) { resource_grp: &ResourceGroup) {
fn from_factory(factory: fn(LoadData, LoadConsumer, Arc<MIMEClassifier>, CancellationListener)) fn from_factory(factory: fn(LoadData, LoadConsumer, Arc<MimeClassifier>, CancellationListener))
-> Box<FnBox(LoadData, -> Box<FnBox(LoadData,
LoadConsumer, LoadConsumer,
Arc<MIMEClassifier>, Arc<MimeClassifier>,
CancellationListener) + Send> { CancellationListener) + Send> {
box move |load_data, senders, classifier, cancel_listener| { box move |load_data, senders, classifier, cancel_listener| {
factory(load_data, senders, classifier, cancel_listener) factory(load_data, senders, classifier, cancel_listener)

View file

@ -36,12 +36,12 @@ fn assert_parse(url: &'static str,
charset: Option<String>, charset: Option<String>,
data: Option<Vec<u8>>) { data: Option<Vec<u8>>) {
use net::data_loader::load; use net::data_loader::load;
use net::mime_classifier::MIMEClassifier; use net::mime_classifier::MimeClassifier;
use net::resource_thread::CancellationListener; use net::resource_thread::CancellationListener;
use std::sync::Arc; use std::sync::Arc;
let (start_chan, start_port) = ipc::channel().unwrap(); let (start_chan, start_port) = ipc::channel().unwrap();
let classifier = Arc::new(MIMEClassifier::new()); let classifier = Arc::new(MimeClassifier::new());
load(LoadData::new(LoadContext::Browsing, Url::parse(url).unwrap(), &DataLoadTest), load(LoadData::new(LoadContext::Browsing, Url::parse(url).unwrap(), &DataLoadTest),
Channel(start_chan), Channel(start_chan),
classifier, CancellationListener::new(None)); classifier, CancellationListener::new(None));

View file

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use net::mime_classifier::as_string_option; use net::mime_classifier::as_string_option;
use net::mime_classifier::{Mp4Matcher, MIMEClassifier, ApacheBugFlag, NoSniffFlag}; use net::mime_classifier::{Mp4Matcher, MimeClassifier, ApacheBugFlag, NoSniffFlag};
use net_traits::LoadContext; use net_traits::LoadContext;
use std::env; use std::env;
use std::fs::File; use std::fs::File;
@ -52,7 +52,7 @@ fn test_sniff_mp4_matcher_long() {
#[test] #[test]
fn test_validate_classifier() { fn test_validate_classifier() {
let classifier = MIMEClassifier::new(); let classifier = MimeClassifier::new();
classifier.validate().expect("Validation error") classifier.validate().expect("Validation error")
} }
@ -69,7 +69,7 @@ fn test_sniff_with_flags(filename_orig: &path::Path,
let mut filename = PathBuf::from("parsable_mime/"); let mut filename = PathBuf::from("parsable_mime/");
filename.push(filename_orig); filename.push(filename_orig);
let classifier = MIMEClassifier::new(); let classifier = MimeClassifier::new();
let read_result = read_file(&filename); let read_result = read_file(&filename);