Add file backend support for Blob and related

Changes include:
- Add BlobImpl to Blob, and related caching mechanism
- Expose ResourceThreads to document_loader, workerglobalscope, worker, and global
- Fix encode_multipart_form_data
- Other small fixes to accommodate the above changes
This commit is contained in:
Zhen Zhang 2016-05-17 12:52:35 +08:00
parent d53507f747
commit 43ad4ba585
18 changed files with 253 additions and 152 deletions

View file

@ -40,6 +40,7 @@ use dom::window::Window;
use encoding::EncodingRef;
use encoding::all::UTF_8;
use encoding::label::encoding_from_whatwg_label;
use encoding::types::DecoderTrap;
use hyper::header::{Charset, ContentDisposition, ContentType, DispositionParam, DispositionType};
use hyper::method::Method;
use msg::constellation_msg::{LoadData, PipelineId};
@ -47,7 +48,6 @@ use rand::random;
use script_thread::{MainThreadScriptMsg, Runnable};
use std::borrow::ToOwned;
use std::cell::Cell;
use std::str::from_utf8;
use std::sync::mpsc::Sender;
use string_cache::Atom;
use task_source::dom_manipulation::DOMManipulationTask;
@ -281,7 +281,7 @@ impl HTMLFormElement {
let encoding = encoding.unwrap_or(self.pick_encoding());
// Step 3
let charset = &*encoding.whatwg_name().unwrap();
let charset = &*encoding.whatwg_name().unwrap_or("UTF-8");
// Step 4
for entry in form_data.iter_mut() {
@ -309,12 +309,18 @@ impl HTMLFormElement {
DispositionParam::Filename(Charset::Ext(String::from(charset.clone())),
None,
f.name().clone().into()));
let content_type = ContentType(f.upcast::<Blob>().Type().parse().unwrap());
// https://tools.ietf.org/html/rfc7578#section-4.4
let content_type = ContentType(f.upcast::<Blob>().Type()
.parse().unwrap_or(mime!(Text / Plain)));
result.push_str(&*format!("Content-Disposition: {}\r\n{}\r\n\r\n",
content_disposition,
content_type));
result.push_str(from_utf8(&f.upcast::<Blob>().get_data().get_bytes()).unwrap());
let slice = f.upcast::<Blob>().get_slice_or_empty();
let decoded = encoding.decode(&slice.get_bytes(), DecoderTrap::Replace)
.expect("Invalid encoding in file");
result.push_str(&decoded);
}
}
}