2013-10-11 12:28:08 +04:00
|
|
|
|
/* 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/. */
|
|
|
|
|
|
2014-09-12 00:45:06 +04:00
|
|
|
|
use resource_task::{Done, Payload, Metadata, LoadData, LoadResponse, start_sending};
|
2013-10-11 12:28:08 +04:00
|
|
|
|
|
2014-03-19 20:35:17 +04:00
|
|
|
|
use serialize::base64::FromBase64;
|
2013-10-11 12:28:08 +04:00
|
|
|
|
|
|
|
|
|
use http::headers::test_utils::from_stream_with_str;
|
|
|
|
|
use http::headers::content_type::MediaType;
|
2014-08-09 07:00:27 +04:00
|
|
|
|
use url::{percent_decode, NonRelativeSchemeData};
|
2014-07-21 23:23:02 +04:00
|
|
|
|
|
2013-10-11 12:28:08 +04:00
|
|
|
|
|
2014-09-12 00:45:06 +04:00
|
|
|
|
pub fn factory(load_data: LoadData, start_chan: Sender<LoadResponse>) {
|
|
|
|
|
// NB: we don't spawn a new task.
|
|
|
|
|
// Hypothesis: data URLs are too small for parallel base64 etc. to be worth it.
|
|
|
|
|
// Should be tested at some point.
|
|
|
|
|
// Left in separate function to allow easy moving to a task, if desired.
|
|
|
|
|
load(load_data, start_chan)
|
2013-10-11 12:28:08 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-28 19:22:05 +04:00
|
|
|
|
fn load(load_data: LoadData, start_chan: Sender<LoadResponse>) {
|
|
|
|
|
let url = load_data.url;
|
2014-06-05 21:58:44 +04:00
|
|
|
|
assert!("data" == url.scheme.as_slice());
|
2013-10-11 12:28:08 +04:00
|
|
|
|
|
|
|
|
|
let mut metadata = Metadata::default(url.clone());
|
|
|
|
|
|
|
|
|
|
// Split out content type and data.
|
2014-07-21 23:23:02 +04:00
|
|
|
|
let mut scheme_data = match url.scheme_data {
|
2014-08-09 07:00:27 +04:00
|
|
|
|
NonRelativeSchemeData(scheme_data) => scheme_data,
|
2014-07-21 23:23:02 +04:00
|
|
|
|
_ => fail!("Expected a non-relative scheme URL.")
|
|
|
|
|
};
|
|
|
|
|
match url.query {
|
|
|
|
|
Some(query) => {
|
|
|
|
|
scheme_data.push_str("?");
|
|
|
|
|
scheme_data.push_str(query.as_slice());
|
|
|
|
|
},
|
|
|
|
|
None => ()
|
|
|
|
|
}
|
|
|
|
|
let parts: Vec<&str> = scheme_data.as_slice().splitn(',', 1).collect();
|
2013-10-11 12:28:08 +04:00
|
|
|
|
if parts.len() != 2 {
|
2014-06-05 21:58:44 +04:00
|
|
|
|
start_sending(start_chan, metadata).send(Done(Err("invalid data uri".to_string())));
|
2013-10-11 12:28:08 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ";base64" must come at the end of the content type, per RFC 2397.
|
|
|
|
|
// rust-http will fail to parse it because there's no =value part.
|
|
|
|
|
let mut is_base64 = false;
|
2014-08-09 07:00:27 +04:00
|
|
|
|
let mut ct_str = parts[0];
|
2013-10-11 12:28:08 +04:00
|
|
|
|
if ct_str.ends_with(";base64") {
|
|
|
|
|
is_base64 = true;
|
|
|
|
|
ct_str = ct_str.slice_to(ct_str.as_bytes().len() - 7);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse the content type using rust-http.
|
|
|
|
|
// FIXME: this can go into an infinite loop! (rust-http #25)
|
|
|
|
|
let content_type: Option<MediaType> = from_stream_with_str(ct_str);
|
|
|
|
|
metadata.set_content_type(&content_type);
|
|
|
|
|
|
|
|
|
|
let progress_chan = start_sending(start_chan, metadata);
|
2014-08-09 07:00:27 +04:00
|
|
|
|
let bytes = percent_decode(parts[1].as_bytes());
|
2013-10-11 12:28:08 +04:00
|
|
|
|
|
|
|
|
|
if is_base64 {
|
2014-07-23 23:10:29 +04:00
|
|
|
|
// FIXME(#2909): It’s unclear what to do with non-alphabet characters,
|
|
|
|
|
// but Acid 3 apparently depends on spaces being ignored.
|
|
|
|
|
let bytes = bytes.move_iter().filter(|&b| b != ' ' as u8).collect::<Vec<u8>>();
|
2014-09-14 17:50:34 +04:00
|
|
|
|
match bytes.as_slice().from_base64() {
|
2014-01-13 07:16:21 +04:00
|
|
|
|
Err(..) => {
|
2014-06-05 21:58:44 +04:00
|
|
|
|
progress_chan.send(Done(Err("non-base64 data uri".to_string())));
|
2013-10-11 12:28:08 +04:00
|
|
|
|
}
|
|
|
|
|
Ok(data) => {
|
2014-07-21 23:23:02 +04:00
|
|
|
|
progress_chan.send(Payload(data));
|
2013-10-11 12:28:08 +04:00
|
|
|
|
progress_chan.send(Done(Ok(())));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-07-21 23:23:02 +04:00
|
|
|
|
progress_chan.send(Payload(bytes));
|
2013-10-11 12:28:08 +04:00
|
|
|
|
progress_chan.send(Done(Ok(())));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
fn assert_parse(url: &'static str,
|
2014-06-05 21:58:44 +04:00
|
|
|
|
content_type: Option<(String, String)>,
|
|
|
|
|
charset: Option<String>,
|
2014-04-29 01:10:41 +04:00
|
|
|
|
data: Option<Vec<u8>>) {
|
2014-04-05 02:52:50 +04:00
|
|
|
|
use std::comm;
|
2014-07-21 23:23:02 +04:00
|
|
|
|
use url::Url;
|
2013-10-11 12:28:08 +04:00
|
|
|
|
|
2014-04-05 02:52:50 +04:00
|
|
|
|
let (start_chan, start_port) = comm::channel();
|
2014-07-21 23:23:02 +04:00
|
|
|
|
load(LoadData::new(Url::parse(url).unwrap()), start_chan);
|
2013-10-11 12:28:08 +04:00
|
|
|
|
|
|
|
|
|
let response = start_port.recv();
|
|
|
|
|
assert_eq!(&response.metadata.content_type, &content_type);
|
|
|
|
|
assert_eq!(&response.metadata.charset, &charset);
|
|
|
|
|
|
|
|
|
|
let progress = response.progress_port.recv();
|
|
|
|
|
|
|
|
|
|
match data {
|
|
|
|
|
None => {
|
2014-06-05 21:58:44 +04:00
|
|
|
|
assert_eq!(progress, Done(Err("invalid data uri".to_string())));
|
2013-10-11 12:28:08 +04:00
|
|
|
|
}
|
|
|
|
|
Some(dat) => {
|
|
|
|
|
assert_eq!(progress, Payload(dat));
|
|
|
|
|
assert_eq!(response.progress_port.recv(), Done(Ok(())));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn empty_invalid() {
|
|
|
|
|
assert_parse("data:", None, None, None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn plain() {
|
2014-08-03 07:41:10 +04:00
|
|
|
|
assert_parse("data:,hello%20world", None, None, Some(b"hello world".iter().map(|&x| x).collect()));
|
2013-10-11 12:28:08 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn plain_ct() {
|
|
|
|
|
assert_parse("data:text/plain,hello",
|
2014-08-03 07:41:10 +04:00
|
|
|
|
Some(("text".to_string(), "plain".to_string())), None, Some(b"hello".iter().map(|&x| x).collect()));
|
2013-10-11 12:28:08 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn plain_charset() {
|
|
|
|
|
assert_parse("data:text/plain;charset=latin1,hello",
|
2014-08-03 07:41:10 +04:00
|
|
|
|
Some(("text".to_string(), "plain".to_string())), Some("latin1".to_string()), Some(b"hello".iter().map(|&x| x).collect()));
|
2013-10-11 12:28:08 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn base64() {
|
2014-04-29 01:10:41 +04:00
|
|
|
|
assert_parse("data:;base64,C62+7w==", None, None, Some(vec!(0x0B, 0xAD, 0xBE, 0xEF)));
|
2013-10-11 12:28:08 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn base64_ct() {
|
|
|
|
|
assert_parse("data:application/octet-stream;base64,C62+7w==",
|
2014-06-05 21:58:44 +04:00
|
|
|
|
Some(("application".to_string(), "octet-stream".to_string())), None, Some(vec!(0x0B, 0xAD, 0xBE, 0xEF)));
|
2013-10-11 12:28:08 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn base64_charset() {
|
|
|
|
|
assert_parse("data:text/plain;charset=koi8-r;base64,8PLl9+XkIO3l5Pfl5A==",
|
2014-06-05 21:58:44 +04:00
|
|
|
|
Some(("text".to_string(), "plain".to_string())), Some("koi8-r".to_string()),
|
2014-04-29 01:10:41 +04:00
|
|
|
|
Some(vec!(0xF0, 0xF2, 0xE5, 0xF7, 0xE5, 0xE4, 0x20, 0xED, 0xE5, 0xE4, 0xF7, 0xE5, 0xE4)));
|
2013-10-11 12:28:08 +04:00
|
|
|
|
}
|