aboutsummaryrefslogtreecommitdiffstats
path: root/src/servo-net/http_loader.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/servo-net/http_loader.rs')
-rw-r--r--src/servo-net/http_loader.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/servo-net/http_loader.rs b/src/servo-net/http_loader.rs
new file mode 100644
index 00000000000..72e9e0acd30
--- /dev/null
+++ b/src/servo-net/http_loader.rs
@@ -0,0 +1,48 @@
+/* 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/. */
+
+use resource_task::{Payload, Done, LoaderTask};
+
+use core::comm::SharedChan;
+use core::task;
+use http_client::uv_http_request;
+use http_client;
+
+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(())));
+ }
+ }
+ };
+ f
+}