Skip to content

Commit

Permalink
Add rust-http to the build and begin using it to resolve http urls
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Sep 3, 2013
1 parent 6fe337e commit 232bdae
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 43 deletions.
1 change: 1 addition & 0 deletions configure
Expand Up @@ -480,6 +480,7 @@ CFG_SUBMODULES="\
support/css/rust-cssparser \
support/geom/rust-geom \
support/harfbuzz/rust-harfbuzz \
support/http/rust-http \
support/hubbub/libhubbub \
support/hubbub/rust-hubbub \
support/layers/rust-layers \
Expand Down
90 changes: 53 additions & 37 deletions src/components/net/http_loader.rs
Expand Up @@ -2,47 +2,63 @@
* 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::{Payload, Done, LoaderTask};
use resource_task::{ProgressMsg, Payload, Done, LoaderTask};

use std::comm::SharedChan;
use std::task;
use http_client::uv_http_request;
use http_client;
use std::cell::Cell;
use std::vec;
use extra::url::Url;
use http::client::RequestWriter;
use http::method::Get;
use http::headers::HeaderEnum;
use std::rt::io::Reader;
use std::rt::io::net::ip::SocketAddr;

pub fn factory() -> LoaderTask {
let f: LoaderTask = |url, progress_chan| {
assert!(url.scheme == ~"http");

let progress_chan = SharedChan::new(progress_chan);
do task::spawn {
debug!("http_loader: requesting via http: %?", url.clone());
let mut request = uv_http_request(url.clone());
let errored = @mut false;
let url = url.clone();
{
let progress_chan = progress_chan.clone();
do request.begin |event| {
let url = url.clone();
match event {
http_client::Status(*) => { }
http_client::Payload(data) => {
debug!("http_loader: got data from %?", url);
let data = data.take();
progress_chan.send(Payload(data));
}
http_client::Error(*) => {
debug!("http_loader: error loading %?", url);
*errored = true;
progress_chan.send(Done(Err(())));
}
}
}
}

if !*errored {
progress_chan.send(Done(Ok(())));
}
}
let url = Cell::new(url);
let progress_chan = Cell::new(progress_chan);
spawn(|| load(url.take(), progress_chan.take()))
};
f
}

fn load(url: Url, progress_chan: Chan<ProgressMsg>) {
assert!(url.scheme == ~"http");

info!("requesting %s", url.to_str());

let mut request = ~RequestWriter::new(Get, url.clone());
request.remote_addr = Some(url_to_socket_addr(&url));
let mut response = match request.read_response() {
Ok(r) => r,
Err(_) => {
progress_chan.send(Done(Err(())));
return;
}
};

loop {
for header in response.headers.iter() {
info!(" - %s: %s", header.header_name(), header.header_value());
}

let mut buf = vec::with_capacity(1024);
unsafe { vec::raw::set_len(&mut buf, 1024) };
match response.read(buf) {
Some(len) => {
unsafe { vec::raw::set_len(&mut buf, len) };
}
None => {
progress_chan.send(Done(Ok(())));
return;
}
}
progress_chan.send(Payload(buf));
}
}

// FIXME: Quick hack to convert ip addresses to SocketAddr
fn url_to_socket_addr(url: &Url) -> SocketAddr {
let host_and_port = fmt!("%s:%s", url.host, url.port.clone().unwrap_or_default(~"80"));
FromStr::from_str(host_and_port).expect("couldn't parse host as IP address")
}
4 changes: 2 additions & 2 deletions src/components/net/net.rc
Expand Up @@ -9,7 +9,7 @@
#[crate_type = "lib"];

extern mod geom;
//extern mod http_client;
extern mod http;
extern mod servo_util (name = "util");
extern mod stb_image;
extern mod extra;
Expand All @@ -25,7 +25,7 @@ pub mod image {
}

pub mod file_loader;
//pub mod http_loader;
pub mod http_loader;
pub mod image_cache_task;
pub mod local_image_cache;
pub mod resource_task;
Expand Down
6 changes: 3 additions & 3 deletions src/components/net/resource_task.rs
Expand Up @@ -5,7 +5,7 @@
//! A task that takes a URL and streams back the binary data.

use file_loader;
//use http_loader;
use http_loader;

use std::cell::Cell;
use std::comm::{Chan, Port, SharedChan};
Expand Down Expand Up @@ -43,10 +43,10 @@ pub type LoaderTask = ~fn(url: Url, Chan<ProgressMsg>);
/// Create a ResourceTask with the default loaders
pub fn ResourceTask() -> ResourceTask {
let file_loader_factory: LoaderTaskFactory = file_loader::factory;
//let http_loader_factory: LoaderTaskFactory = http_loader::factory;
let http_loader_factory: LoaderTaskFactory = http_loader::factory;
let loaders = ~[
(~"file", file_loader_factory),
//(~"http", http_loader_factory)
(~"http", http_loader_factory)
];
create_resource_task_with_loaders(loaders)
}
Expand Down
2 changes: 1 addition & 1 deletion src/support/http/rust-http
Submodule rust-http updated from c8c703 to b96039

0 comments on commit 232bdae

Please sign in to comment.