Refactor resource loader protocol to send metadata first

This commit is contained in:
Keegan McAllister 2013-10-09 16:23:07 -07:00
parent cb67a50a95
commit 48af4e53a9
6 changed files with 81 additions and 65 deletions

View file

@ -2,7 +2,7 @@
* 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/. */
use resource_task::{ProgressMsg, Payload, Done, UrlChange, LoaderTask};
use resource_task::{Metadata, Payload, Done, LoadResponse, LoaderTask, start_sending};
use std::cell::Cell;
use std::vec;
@ -13,15 +13,15 @@ use http::headers::HeaderEnum;
use std::rt::io::Reader;
pub fn factory() -> LoaderTask {
let f: LoaderTask = |url, progress_chan| {
let f: LoaderTask = |url, start_chan| {
let url = Cell::new(url);
let progress_chan = Cell::new(progress_chan);
spawn(|| load(url.take(), progress_chan.take()))
let start_chan = Cell::new(start_chan);
spawn(|| load(url.take(), start_chan.take()))
};
f
}
fn load(url: Url, progress_chan: Chan<ProgressMsg>) {
fn load(url: Url, start_chan: Chan<LoadResponse>) {
assert!("http" == url.scheme);
info!("requesting %s", url.to_str());
@ -30,7 +30,7 @@ fn load(url: Url, progress_chan: Chan<ProgressMsg>) {
let mut response = match request.read_response() {
Ok(r) => r,
Err(_) => {
progress_chan.send(Done(Err(())));
start_sending(start_chan, Metadata::default(url)).send(Done(Err(())));
return;
}
};
@ -52,12 +52,15 @@ fn load(url: Url, progress_chan: Chan<ProgressMsg>) {
match redirect {
Some(url) => {
info!("redirecting to %s", url.to_str());
progress_chan.send(UrlChange(url.clone()));
return load(url, progress_chan);
return load(url, start_chan);
}
None => ()
}
let mut metadata = Metadata::default(url);
// We will set other fields here.
let progress_chan = start_sending(start_chan, metadata);
loop {
let mut buf = vec::with_capacity(1024);