M1456, Implement MIME sniffing initial Step

This commit is contained in:
Kshitij Parajuli 2014-10-19 13:17:53 -04:00 committed by David Zager
parent c5e1b0d32e
commit 7472564bf6
3 changed files with 56 additions and 2 deletions

View file

@ -37,6 +37,7 @@ pub mod data_loader;
pub mod image_cache_task;
pub mod local_image_cache;
pub mod resource_task;
mod sniffer_task;
/// An implementation of the [Fetch spec](http://fetch.spec.whatwg.org/)
pub mod fetch {

View file

@ -8,6 +8,7 @@ use about_loader;
use data_loader;
use file_loader;
use http_loader;
use sniffer_task;
use std::comm::{channel, Receiver, Sender};
use http::headers::content_type::MediaType;
@ -177,7 +178,6 @@ struct ResourceManager {
user_agent: Option<String>,
}
impl ResourceManager {
fn new(from_client: Receiver<ControlMsg>, user_agent: Option<String>) -> ResourceManager {
ResourceManager {
@ -206,6 +206,12 @@ impl ResourceManager {
let mut load_data = load_data;
load_data.headers.user_agent = self.user_agent.clone();
// Create new communication channel, create new sniffer task,
// send all the data to the new sniffer task with the send
// end of the pipe, receive all the data.
let sniffer_task = sniffer_task::new_sniffer_task(start_chan.clone());
let loader = match load_data.url.scheme.as_slice() {
"file" => file_loader::factory,
"http" | "https" => http_loader::factory,
@ -219,7 +225,8 @@ impl ResourceManager {
}
};
debug!("resource_task: loading url: {:s}", load_data.url.serialize());
loader(load_data, start_chan);
loader(load_data, sniffer_task);
}
}

View file

@ -0,0 +1,46 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! A task that sniffs data
use std::comm::{channel, Receiver, Sender, Disconnected};
use std::task::TaskBuilder;
use resource_task::{LoadResponse};
pub type SnifferTask = Sender<LoadResponse>;
pub fn new_sniffer_task(next_rx: Sender<LoadResponse>) -> SnifferTask {
let(sen, rec) = channel();
let builder = TaskBuilder::new().named("SnifferManager");
builder.spawn(proc(){
SnifferManager::new(rec).start(next_rx);
});
sen
}
struct SnifferManager {
data_receiver: Receiver<LoadResponse>,
}
impl SnifferManager {
fn new(data_receiver: Receiver <LoadResponse>) -> SnifferManager {
SnifferManager {
data_receiver: data_receiver,
}
}
}
impl SnifferManager {
fn start(&self, next_rx: Sender<LoadResponse>) {
loop {
match self.data_receiver.try_recv() {
Ok(snif_data) => next_rx.send(snif_data),
Err(e) => {
if e == Disconnected {
break
}
}
}
}
}
}