From 1c5addfe7ac577557b2ea25d04e4ce9c1cb55d1d Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sun, 3 Jul 2016 11:07:12 -0700 Subject: [PATCH] servo: Merge #12190 - Minor MIME renaming and aliasing cleanup (from frewsxcv:mime-renaming); r=jdm Source-Repo: https://github.com/servo/servo Source-Revision: 194fb3e199922ef0e1ce7943036800ecac5bbfe3 --- servo/components/net/about_loader.rs | 4 +- servo/components/net/blob_loader.rs | 6 +-- servo/components/net/chrome_loader.rs | 4 +- servo/components/net/data_loader.rs | 6 +-- servo/components/net/file_loader.rs | 6 +-- servo/components/net/filemanager_thread.rs | 6 +-- servo/components/net/http_loader.rs | 8 ++-- servo/components/net/mime_classifier.rs | 56 +++++++++++----------- servo/components/net/resource_thread.rs | 14 +++--- servo/tests/unit/net/data_loader.rs | 4 +- servo/tests/unit/net/mime_classifier.rs | 6 +-- 11 files changed, 61 insertions(+), 59 deletions(-) diff --git a/servo/components/net/about_loader.rs b/servo/components/net/about_loader.rs index 56b4f5d28a53..e5b1756cac38 100644 --- a/servo/components/net/about_loader.rs +++ b/servo/components/net/about_loader.rs @@ -6,7 +6,7 @@ use file_loader; use hyper::header::ContentType; use hyper::http::RawStatus; use hyper::mime::{Mime, SubLevel, TopLevel}; -use mime_classifier::MIMEClassifier; +use mime_classifier::MimeClassifier; use net_traits::ProgressMsg::Done; use net_traits::response::HttpsState; 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, start_chan: LoadConsumer, - classifier: Arc, + classifier: Arc, cancel_listener: CancellationListener) { let url = load_data.url.clone(); match url.path() { diff --git a/servo/components/net/blob_loader.rs b/servo/components/net/blob_loader.rs index 00b7f4ae0831..37665da86e23 100644 --- a/servo/components/net/blob_loader.rs +++ b/servo/components/net/blob_loader.rs @@ -7,7 +7,7 @@ use hyper::header::{DispositionType, ContentDisposition, DispositionParam}; use hyper::header::{Headers, ContentType, ContentLength, Charset}; use hyper::http::RawStatus; use mime::{Mime, Attr}; -use mime_classifier::MIMEClassifier; +use mime_classifier::MimeClassifier; use net_traits::ProgressMsg::Done; use net_traits::blob_url_store::{parse_blob_url, BlobURLStoreEntry, BlobURLStoreError}; use net_traits::response::HttpsState; @@ -22,7 +22,7 @@ use std::sync::{Arc, RwLock}; pub fn load(load_data: LoadData, consumer: LoadConsumer, blob_url_store: Arc>, - classifier: Arc) { // XXX: Move it into net process later + classifier: Arc) { // XXX: Move it into net process later match parse_blob_url(&load_data.url) { None => { @@ -48,7 +48,7 @@ pub fn load(load_data: LoadData, consumer: LoadConsumer, fn load_blob(load_data: &LoadData, start_chan: LoadConsumer, - classifier: Arc, + classifier: Arc, entry: &BlobURLStoreEntry) { let content_type: Mime = entry.type_string.parse().unwrap_or(mime!(Text / Plain)); let charset = content_type.get_param(Attr::Charset); diff --git a/servo/components/net/chrome_loader.rs b/servo/components/net/chrome_loader.rs index 544e9a64f4a9..21bffa5b4b9a 100644 --- a/servo/components/net/chrome_loader.rs +++ b/servo/components/net/chrome_loader.rs @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use file_loader; -use mime_classifier::MIMEClassifier; +use mime_classifier::MimeClassifier; use net_traits::{LoadConsumer, LoadData, NetworkError}; use resource_thread::{CancellationListener, send_error}; use std::fs::canonicalize; @@ -37,7 +37,7 @@ pub fn resolve_chrome_url(url: &Url) -> Result { pub fn factory(mut load_data: LoadData, start_chan: LoadConsumer, - classifier: Arc, + classifier: Arc, cancel_listener: CancellationListener) { let file_url = match resolve_chrome_url(&load_data.url) { Ok(url) => url, diff --git a/servo/components/net/data_loader.rs b/servo/components/net/data_loader.rs index 4e331747c21d..d81cec9a7e01 100644 --- a/servo/components/net/data_loader.rs +++ b/servo/components/net/data_loader.rs @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use hyper::mime::{Mime, TopLevel, SubLevel, Attr, Value}; -use mime_classifier::MIMEClassifier; +use mime_classifier::MimeClassifier; use net_traits::LoadConsumer; use net_traits::ProgressMsg::{Payload, Done}; use net_traits::{LoadData, Metadata, NetworkError}; @@ -15,7 +15,7 @@ use url::{Position, Url}; pub fn factory(load_data: LoadData, senders: LoadConsumer, - classifier: Arc, + classifier: Arc, cancel_listener: CancellationListener) { // NB: we don't spawn a new thread. // Hypothesis: data URLs are too small for parallel base64 etc. to be worth it. @@ -72,7 +72,7 @@ pub fn decode(url: &Url) -> Result { pub fn load(load_data: LoadData, start_chan: LoadConsumer, - classifier: Arc, + classifier: Arc, cancel_listener: CancellationListener) { let url = load_data.url; diff --git a/servo/components/net/file_loader.rs b/servo/components/net/file_loader.rs index 2274279dda19..271baca74f67 100644 --- a/servo/components/net/file_loader.rs +++ b/servo/components/net/file_loader.rs @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use about_loader; -use mime_classifier::MIMEClassifier; +use mime_classifier::MimeClassifier; use mime_guess::guess_mime_type; use msg::constellation_msg::{PipelineId, ReferrerPolicy}; 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, - senders: LoadConsumer, classifier: Arc, buf: &[u8]) + senders: LoadConsumer, classifier: Arc, buf: &[u8]) -> Result { let mut metadata = Metadata::default(load_data.url); 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, senders: LoadConsumer, - classifier: Arc, + classifier: Arc, cancel_listener: CancellationListener) { assert!(load_data.url.scheme() == "file"); spawn_named("file_loader".to_owned(), move || { diff --git a/servo/components/net/filemanager_thread.rs b/servo/components/net/filemanager_thread.rs index 9c1cb851dc47..246d3254e420 100644 --- a/servo/components/net/filemanager_thread.rs +++ b/servo/components/net/filemanager_thread.rs @@ -4,7 +4,7 @@ use blob_loader; use ipc_channel::ipc::{self, IpcReceiver, IpcSender}; -use mime_classifier::MIMEClassifier; +use mime_classifier::MimeClassifier; use mime_guess::guess_mime_type_opt; use net_traits::blob_url_store::{BlobURLStoreEntry, BlobURLStoreError, BlobURLStoreMsg}; use net_traits::filemanager_thread::{FileManagerThreadMsg, FileManagerResult, FilterPattern}; @@ -90,7 +90,7 @@ impl FileManagerThreadFactory for IpcSender { receiver: IpcReceiver, idmap: HashMap, - classifier: Arc, + classifier: Arc, blob_url_store: Arc>, ui: &'static UI, } @@ -100,7 +100,7 @@ impl FileManager { FileManager { receiver: recv, idmap: HashMap::new(), - classifier: Arc::new(MIMEClassifier::new()), + classifier: Arc::new(MimeClassifier::new()), blob_url_store: Arc::new(RwLock::new(BlobURLStore::new())), ui: ui } diff --git a/servo/components/net/http_loader.rs b/servo/components/net/http_loader.rs index 108cc9b4da15..6a4de828b24c 100644 --- a/servo/components/net/http_loader.rs +++ b/servo/components/net/http_loader.rs @@ -26,7 +26,7 @@ use hyper::net::Fresh; use hyper::status::{StatusClass, StatusCode}; use ipc_channel::ipc; use log; -use mime_classifier::MIMEClassifier; +use mime_classifier::MimeClassifier; use msg::constellation_msg::{PipelineId, ReferrerPolicy}; use net_traits::ProgressMsg::{Done, Payload}; use net_traits::hosts::replace_hosts; @@ -62,7 +62,7 @@ pub fn factory(user_agent: String, connector: Arc>) -> Box, + Arc, CancellationListener) + Send> { box move |load_data: LoadData, senders, classifier, cancel_listener| { 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, start_chan: LoadConsumer, - classifier: Arc, + classifier: Arc, connector: Arc>, http_state: HttpState, devtools_chan: Option>, @@ -1093,7 +1093,7 @@ fn send_data(context: LoadContext, reader: &mut R, start_chan: LoadConsumer, metadata: Metadata, - classifier: Arc, + classifier: Arc, cancel_listener: &CancellationListener) { let (progress_chan, mut chunk) = { let buf = match read_block(reader) { diff --git a/servo/components/net/mime_classifier.rs b/servo/components/net/mime_classifier.rs index 6107130d3876..73dc8232fb67 100644 --- a/servo/components/net/mime_classifier.rs +++ b/servo/components/net/mime_classifier.rs @@ -5,7 +5,7 @@ use net_traits::LoadContext; use std::borrow::ToOwned; -pub struct MIMEClassifier { +pub struct MimeClassifier { image_classifier: GroupedClassifier, audio_video_classifier: GroupedClassifier, scriptable_classifier: GroupedClassifier, @@ -48,14 +48,16 @@ pub enum NoSniffFlag { OFF } -impl MIMEClassifier { +pub type MimeType = (String, String); + +impl MimeClassifier { //Performs MIME Type Sniffing Algorithm (sections 7 and 8) pub fn classify(&self, context: LoadContext, no_sniff_flag: NoSniffFlag, apache_bug_flag: ApacheBugFlag, - supplied_type: &Option<(String, String)>, - data: &[u8]) -> (String, String) { + supplied_type: &Option, + data: &[u8]) -> MimeType { let supplied_type_or_octet_stream = supplied_type.clone() .unwrap_or(("application".to_owned(), "octet-stream".to_owned())); @@ -64,14 +66,14 @@ impl MIMEClassifier { None => self.sniff_unknown_type(no_sniff_flag, data), Some(ref 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) } else { match no_sniff_flag { NoSniffFlag::ON => supplied_type.clone(), NoSniffFlag::OFF => match apache_bug_flag { 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) { Some(MediaType::Html) => self.feeds_classifier.classify(data), Some(MediaType::Image) => self.image_classifier.classify(data), @@ -85,14 +87,14 @@ impl MIMEClassifier { }, LoadContext::Image => { // 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, _ => self.image_classifier.classify(data), }.unwrap_or(supplied_type_or_octet_stream) }, LoadContext::AudioVideo => { // 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, _ => self.audio_video_classifier.classify(data), }.unwrap_or(supplied_type_or_octet_stream) @@ -129,7 +131,7 @@ impl MIMEClassifier { }, LoadContext::Font => { // 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, _ => self.font_classifier.classify(data), }.unwrap_or(supplied_type_or_octet_stream) @@ -151,8 +153,8 @@ impl MIMEClassifier { } } - pub fn new() -> MIMEClassifier { - MIMEClassifier { + pub fn new() -> MimeClassifier { + MimeClassifier { image_classifier: GroupedClassifier::image_classifer(), audio_video_classifier: GroupedClassifier::audio_video_classifier(), scriptable_classifier: GroupedClassifier::scriptable_classifier(), @@ -177,7 +179,7 @@ impl MIMEClassifier { } //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 sniffed = if should_sniff_scriptable { self.scriptable_classifier.classify(data) @@ -193,7 +195,7 @@ impl MIMEClassifier { .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") } @@ -230,33 +232,33 @@ impl MIMEClassifier { fn get_media_type(media_type: &str, media_subtype: &str) -> Option { - if MIMEClassifier::is_xml(media_type, media_subtype) { + if MimeClassifier::is_xml(media_type, media_subtype) { Some(MediaType::Xml) - } else if MIMEClassifier::is_html(media_type, media_subtype) { + } else if MimeClassifier::is_html(media_type, media_subtype) { Some(MediaType::Html) - } else if MIMEClassifier::is_image(media_type) { + } else if MimeClassifier::is_image(media_type) { 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) } else { None } } - fn maybe_get_media_type(supplied_type: &Option<(String, String)>) -> Option { + fn maybe_get_media_type(supplied_type: &Option) -> Option { 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 { tup.map(|(a, b)| (a.to_owned(), b.to_owned())) } //Interface used for composite types trait MIMEChecker { - fn classify(&self, data: &[u8]) -> Option<(String, String)>; + fn classify(&self, data: &[u8]) -> Option; /// Validate the MIME checker configuration fn validate(&self) -> Result<(), String>; } @@ -322,7 +324,7 @@ impl ByteMatcher { } impl MIMEChecker for ByteMatcher { - fn classify(&self, data: &[u8]) -> Option<(String, String)> { + fn classify(&self, data: &[u8]) -> Option { self.matches(data).map(|_| { (self.content_type.0.to_owned(), self.content_type.1.to_owned()) }) @@ -358,7 +360,7 @@ struct TagTerminatedByteMatcher { } impl MIMEChecker for TagTerminatedByteMatcher { - fn classify(&self, data: &[u8]) -> Option<(String, String)> { + fn classify(&self, data: &[u8]) -> Option { self.matcher.matches(data).and_then(|j| if j < data.len() && (data[j] == b' ' || data[j] == b'>') { Some((self.matcher.content_type.0.to_owned(), @@ -399,7 +401,7 @@ impl Mp4Matcher { } impl MIMEChecker for Mp4Matcher { - fn classify(&self, data: &[u8]) -> Option<(String, String)> { + fn classify(&self, data: &[u8]) -> Option { if self.matches(data) { Some(("video".to_owned(), "mp4".to_owned())) } else { @@ -432,7 +434,7 @@ impl BinaryOrPlaintextClassifier { } } impl MIMEChecker for BinaryOrPlaintextClassifier { - fn classify(&self, data: &[u8]) -> Option<(String, String)> { + fn classify(&self, data: &[u8]) -> Option { as_string_option(Some(self.classify_impl(data))) } @@ -532,7 +534,7 @@ impl GroupedClassifier { } } impl MIMEChecker for GroupedClassifier { - fn classify(&self, data: &[u8]) -> Option<(String, String)> { + fn classify(&self, data: &[u8]) -> Option { self.byte_matchers .iter() .filter_map(|matcher| matcher.classify(data)) @@ -643,7 +645,7 @@ impl FeedsClassifier { } impl MIMEChecker for FeedsClassifier { - fn classify(&self, data: &[u8]) -> Option<(String, String)> { + fn classify(&self, data: &[u8]) -> Option { as_string_option(self.classify_impl(data)) } diff --git a/servo/components/net/resource_thread.rs b/servo/components/net/resource_thread.rs index f25840de7765..56caf7ce886a 100644 --- a/servo/components/net/resource_thread.rs +++ b/servo/components/net/resource_thread.rs @@ -21,7 +21,7 @@ use hyper::client::pool::Pool; use hyper::header::{ContentType, Header, SetCookie}; use hyper::mime::{Mime, SubLevel, TopLevel}; 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::ProgressMsg::Done; 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. pub fn start_sending_sniffed(start_chan: LoadConsumer, metadata: Metadata, - classifier: Arc, partial_body: &[u8], + classifier: Arc, partial_body: &[u8], context: LoadContext) -> ProgressSender { 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. pub fn start_sending_sniffed_opt(start_chan: LoadConsumer, mut metadata: Metadata, - classifier: Arc, partial_body: &[u8], + classifier: Arc, partial_body: &[u8], context: LoadContext) -> Result { if PREFS.get("network.mime.sniff").as_boolean().unwrap_or(false) { @@ -459,7 +459,7 @@ pub struct AuthCache { pub struct CoreResourceManager { user_agent: String, - mime_classifier: Arc, + mime_classifier: Arc, devtools_chan: Option>, profiler_chan: ProfilerChan, cancel_load_map: HashMap>, @@ -472,7 +472,7 @@ impl CoreResourceManager { profiler_chan: ProfilerChan) -> CoreResourceManager { CoreResourceManager { user_agent: user_agent, - mime_classifier: Arc::new(MIMEClassifier::new()), + mime_classifier: Arc::new(MimeClassifier::new()), devtools_chan: devtools_channel, profiler_chan: profiler_chan, cancel_load_map: HashMap::new(), @@ -510,10 +510,10 @@ impl CoreResourceManager { id_sender: Option>, resource_thread: CoreResourceThread, resource_grp: &ResourceGroup) { - fn from_factory(factory: fn(LoadData, LoadConsumer, Arc, CancellationListener)) + fn from_factory(factory: fn(LoadData, LoadConsumer, Arc, CancellationListener)) -> Box, + Arc, CancellationListener) + Send> { box move |load_data, senders, classifier, cancel_listener| { factory(load_data, senders, classifier, cancel_listener) diff --git a/servo/tests/unit/net/data_loader.rs b/servo/tests/unit/net/data_loader.rs index 3c423809cc21..959575be4c00 100644 --- a/servo/tests/unit/net/data_loader.rs +++ b/servo/tests/unit/net/data_loader.rs @@ -36,12 +36,12 @@ fn assert_parse(url: &'static str, charset: Option, data: Option>) { use net::data_loader::load; - use net::mime_classifier::MIMEClassifier; + use net::mime_classifier::MimeClassifier; use net::resource_thread::CancellationListener; use std::sync::Arc; 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), Channel(start_chan), classifier, CancellationListener::new(None)); diff --git a/servo/tests/unit/net/mime_classifier.rs b/servo/tests/unit/net/mime_classifier.rs index 91fbb4366038..c5635e06a53b 100644 --- a/servo/tests/unit/net/mime_classifier.rs +++ b/servo/tests/unit/net/mime_classifier.rs @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 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 std::env; use std::fs::File; @@ -52,7 +52,7 @@ fn test_sniff_mp4_matcher_long() { #[test] fn test_validate_classifier() { - let classifier = MIMEClassifier::new(); + let classifier = MimeClassifier::new(); 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/"); filename.push(filename_orig); - let classifier = MIMEClassifier::new(); + let classifier = MimeClassifier::new(); let read_result = read_file(&filename);