aboutsummaryrefslogtreecommitdiffstats
path: root/components/net/resource_task.rs
diff options
context:
space:
mode:
authorbors-servo <metajack+bors@gmail.com>2015-09-18 07:22:59 -0600
committerbors-servo <metajack+bors@gmail.com>2015-09-18 07:22:59 -0600
commit8a8204ffc8fa287dde2321c40d12b191b51960da (patch)
treef471d6cf869b72818540ff587276c7fd8b3c14bb /components/net/resource_task.rs
parenteae3eaf97474febb2c50a7a4d392594cbc8d2da2 (diff)
parentcc44448b0922f184b22cdb2ecc29af37496c52b0 (diff)
downloadservo-8a8204ffc8fa287dde2321c40d12b191b51960da.tar.gz
servo-8a8204ffc8fa287dde2321c40d12b191b51960da.zip
Auto merge of #7447 - ddrmanxbxfr:master, r=jdm
Issue #7382 Use descriptive enums instead of booleans for MIMEClassifier::classifer Hi guys i've done a small pass of refactor in the MIMEClassifier implementation. (See issue #7382 ) - Moved the predicates to separate functions - Added a mimetype enum so we can compare them easily after calling MIMEClassifier::get_media_type I hope it follows rust good pratices (care it's my first time doing rust). Improvements and tips are welcome :). Thanks for looking at it. <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7447) <!-- Reviewable:end -->
Diffstat (limited to 'components/net/resource_task.rs')
-rw-r--r--components/net/resource_task.rs33
1 files changed, 22 insertions, 11 deletions
diff --git a/components/net/resource_task.rs b/components/net/resource_task.rs
index bf3d99c9f8b..1da1cd49a7b 100644
--- a/components/net/resource_task.rs
+++ b/components/net/resource_task.rs
@@ -10,8 +10,7 @@ use cookie_storage::CookieStorage;
use data_loader;
use file_loader;
use http_loader::{self, create_http_connector, Connector};
-use mime_classifier::MIMEClassifier;
-
+use mime_classifier::{ApacheBugFlag, MIMEClassifier, NoSniffFlag};
use net_traits::ProgressMsg::Done;
use net_traits::{ControlMsg, LoadData, LoadResponse, LoadConsumer, CookieSource};
use net_traits::{Metadata, ProgressMsg, ResourceTask, AsyncResponseTarget, ResponseAction};
@@ -29,7 +28,9 @@ use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use std::borrow::ToOwned;
use std::boxed::FnBox;
-use std::sync::Arc;
+
+use std::sync::{Arc};
+
use std::sync::mpsc::{channel, Sender};
pub enum ProgressSender {
@@ -72,21 +73,20 @@ pub fn start_sending_sniffed_opt(start_chan: LoadConsumer, mut metadata: Metadat
-> Result<ProgressSender, ()> {
if opts::get().sniff_mime_types {
// TODO: should be calculated in the resource loader, from pull requeset #4094
- let mut nosniff = false;
- let mut check_for_apache_bug = false;
+ let mut no_sniff = NoSniffFlag::OFF;
+ let mut check_for_apache_bug = ApacheBugFlag::OFF;
if let Some(ref headers) = metadata.headers {
if let Some(ref raw_content_type) = headers.get_raw("content-type") {
if raw_content_type.len() > 0 {
let ref last_raw_content_type = raw_content_type[raw_content_type.len() - 1];
- check_for_apache_bug = last_raw_content_type == b"text/plain"
- || last_raw_content_type == b"text/plain; charset=ISO-8859-1"
- || last_raw_content_type == b"text/plain; charset=iso-8859-1"
- || last_raw_content_type == b"text/plain; charset=UTF-8";
+ check_for_apache_bug = apache_bug_predicate(last_raw_content_type)
}
}
if let Some(ref raw_content_type_options) = headers.get_raw("X-content-type-options") {
- nosniff = raw_content_type_options.iter().any(|ref opt| *opt == b"nosniff");
+ if raw_content_type_options.iter().any(|ref opt| *opt == b"nosniff") {
+ no_sniff = NoSniffFlag::ON
+ }
}
}
@@ -94,7 +94,7 @@ pub fn start_sending_sniffed_opt(start_chan: LoadConsumer, mut metadata: Metadat
metadata.content_type.map(|ContentType(Mime(toplevel, sublevel, _))| {
(format!("{}", toplevel), format!("{}", sublevel))
});
- metadata.content_type = classifier.classify(nosniff, check_for_apache_bug, &supplied_type,
+ metadata.content_type = classifier.classify(no_sniff, check_for_apache_bug, &supplied_type,
&partial_body).map(|(toplevel, sublevel)| {
let mime_tp: TopLevel = toplevel.parse().unwrap();
let mime_sb: SubLevel = sublevel.parse().unwrap();
@@ -106,6 +106,17 @@ pub fn start_sending_sniffed_opt(start_chan: LoadConsumer, mut metadata: Metadat
start_sending_opt(start_chan, metadata)
}
+fn apache_bug_predicate(last_raw_content_type: &[u8]) -> ApacheBugFlag {
+ if last_raw_content_type == b"text/plain"
+ || last_raw_content_type == b"text/plain; charset=ISO-8859-1"
+ || last_raw_content_type == b"text/plain; charset=iso-8859-1"
+ || last_raw_content_type == b"text/plain; charset=UTF-8" {
+ ApacheBugFlag::ON
+ } else {
+ ApacheBugFlag::OFF
+ }
+}
+
/// For use by loaders in responding to a Load message.
pub fn start_sending_opt(start_chan: LoadConsumer, metadata: Metadata) -> Result<ProgressSender, ()> {
match start_chan {