aboutsummaryrefslogtreecommitdiffstats
path: root/components/net/file_loader.rs
diff options
context:
space:
mode:
authorJack Moffitt <jack@metajack.im>2014-08-28 09:34:23 -0600
committerJack Moffitt <jack@metajack.im>2014-09-08 20:21:42 -0600
commitc6ab60dbfc6da7b4f800c9e40893c8b58413960c (patch)
treed1d74076cf7fa20e4f77ec7cb82cae98b67362cb /components/net/file_loader.rs
parentdb2f642c32fc5bed445bb6f2e45b0f6f0b4342cf (diff)
downloadservo-c6ab60dbfc6da7b4f800c9e40893c8b58413960c.tar.gz
servo-c6ab60dbfc6da7b4f800c9e40893c8b58413960c.zip
Cargoify servo
Diffstat (limited to 'components/net/file_loader.rs')
-rw-r--r--components/net/file_loader.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/components/net/file_loader.rs b/components/net/file_loader.rs
new file mode 100644
index 00000000000..43c3191c600
--- /dev/null
+++ b/components/net/file_loader.rs
@@ -0,0 +1,50 @@
+/* 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::{ProgressMsg, Metadata, Payload, Done, LoaderTask, start_sending};
+
+use std::io;
+use std::io::File;
+use servo_util::task::spawn_named;
+
+static READ_SIZE: uint = 8192;
+
+fn read_all(reader: &mut io::Stream, progress_chan: &Sender<ProgressMsg>)
+ -> Result<(), String> {
+ loop {
+ let mut buf = vec!();
+ match reader.push_at_least(READ_SIZE, READ_SIZE, &mut buf) {
+ Ok(_) => progress_chan.send(Payload(buf)),
+ Err(e) => match e.kind {
+ io::EndOfFile => {
+ if buf.len() > 0 {
+ progress_chan.send(Payload(buf));
+ }
+ return Ok(());
+ }
+ _ => return Err(e.desc.to_string()),
+ }
+ }
+ }
+}
+
+pub fn factory() -> LoaderTask {
+ let f: LoaderTask = proc(load_data, start_chan) {
+ let url = load_data.url;
+ assert!("file" == url.scheme.as_slice());
+ let progress_chan = start_sending(start_chan, Metadata::default(url.clone()));
+ spawn_named("file_loader", proc() {
+ match File::open_mode(&Path::new(url.serialize_path().unwrap()), io::Open, io::Read) {
+ Ok(ref mut reader) => {
+ let res = read_all(reader as &mut io::Stream, &progress_chan);
+ progress_chan.send(Done(res));
+ }
+ Err(e) => {
+ progress_chan.send(Done(Err(e.desc.to_string())));
+ }
+ };
+ });
+ };
+ f
+}