diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2016-02-15 08:53:27 +0530 |
---|---|---|
committer | bors-servo <lbergstrom+bors@mozilla.com> | 2016-02-15 08:53:27 +0530 |
commit | e8ae7e47731eb20ea36dbc55deda84b664c3aab4 (patch) | |
tree | 0018a08a2d4dac0a0cc47d49fbba4692fba97f18 /components/script | |
parent | 024d106f78e0e37adc3ddc24a56a0597c2bacf98 (diff) | |
parent | 0b0e1473cdbebf4e4c738367b2f119b6658a06e5 (diff) | |
download | servo-e8ae7e47731eb20ea36dbc55deda84b664c3aab4.tar.gz servo-e8ae7e47731eb20ea36dbc55deda84b664c3aab4.zip |
Auto merge of #9629 - danlrobertson:i9623, r=KiChjang
Implement Blob response for XMLHttpRequest: #9623
My first attempt at implementing the Blob response for XMLHttpRequest. The expected result for the response `tests/wpt/web-platform-test/HMLHttpRequest/response-blob-data.htm` is also changed to `PASS`. Please let me know if you see any areas in which I can improve this PR!
Fixes #9623
<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.svg" height="40" alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/9629)
<!-- Reviewable:end -->
Diffstat (limited to 'components/script')
-rw-r--r-- | components/script/dom/xmlhttprequest.rs | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/components/script/dom/xmlhttprequest.rs b/components/script/dom/xmlhttprequest.rs index d81bfe4ab7c..130cec9ecf1 100644 --- a/components/script/dom/xmlhttprequest.rs +++ b/components/script/dom/xmlhttprequest.rs @@ -22,6 +22,7 @@ use dom::bindings::js::{Root, RootedReference}; use dom::bindings::refcounted::Trusted; use dom::bindings::reflector::{Reflectable, reflect_dom_object}; use dom::bindings::str::{ByteString, USVString}; +use dom::blob::Blob; use dom::document::DocumentSource; use dom::document::{Document, IsHTMLDocument}; use dom::event::{Event, EventBubbles, EventCancelable}; @@ -54,6 +55,7 @@ use std::ascii::AsciiExt; use std::borrow::ToOwned; use std::cell::{Cell, RefCell}; use std::default::Default; +use std::string::ToString; use std::sync::mpsc::channel; use std::sync::{Arc, Mutex}; use string_cache::Atom; @@ -122,6 +124,7 @@ pub struct XMLHttpRequest { response: DOMRefCell<ByteString>, response_type: Cell<XMLHttpRequestResponseType>, response_xml: MutNullableHeap<JS<Document>>, + response_blob: MutNullableHeap<JS<Blob>>, #[ignore_heap_size_of = "Defined in hyper"] response_headers: DOMRefCell<Headers>, #[ignore_heap_size_of = "Defined in hyper"] @@ -161,6 +164,7 @@ impl XMLHttpRequest { response: DOMRefCell::new(ByteString::new(vec!())), response_type: Cell::new(XMLHttpRequestResponseType::_empty), response_xml: Default::default(), + response_blob: Default::default(), response_headers: DOMRefCell::new(Headers::new()), override_mime_type: DOMRefCell::new(None), override_charset: DOMRefCell::new(None), @@ -725,7 +729,10 @@ impl XMLHttpRequestMethods for XMLHttpRequest { JS_ClearPendingException(cx); return NullValue(); } - } + }, + XMLHttpRequestResponseType::Blob => { + self.blob_response().to_jsval(cx, rval.handle_mut()); + }, _ => { // XXXManishearth handle other response types self.response.borrow().to_jsval(cx, rval.handle_mut()); @@ -1038,6 +1045,21 @@ impl XMLHttpRequest { encoding.decode(&self.response.borrow(), DecoderTrap::Replace).unwrap().to_owned() } + // https://xhr.spec.whatwg.org/#blob-response + fn blob_response(&self) -> Root<Blob> { + // Step 1 + if let Some(response) = self.response_blob.get() { + return response; + } + // Step 2 + let mime = self.final_mime_type().as_ref().map(ToString::to_string).unwrap_or("".to_owned()); + + // Steps 3 && 4 + let blob = Blob::new(self.global().r(), self.response.borrow().to_vec(), &mime); + self.response_blob.set(Some(blob.r())); + blob + } + fn document_response(&self) -> Option<Root<Document>> { let mime_type = self.final_mime_type(); //TODO: prescan the response to determine encoding if final charset is null |