Skip to content

Commit

Permalink
Implement Blob response for XMLHttpRequest: #9623
Browse files Browse the repository at this point in the history
In response to #9623 implement the Blob response for XML HttpRequest and
change the expected result for the script test.
  • Loading branch information
dlrobertson committed Feb 14, 2016
1 parent 3f74c07 commit 0b0e147
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
24 changes: 23 additions & 1 deletion components/script/dom/xmlhttprequest.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions tests/wpt/metadata/XMLHttpRequest/response-data-blob.htm.ini

This file was deleted.

5 changes: 0 additions & 5 deletions tests/wpt/metadata/XMLHttpRequest/send-data-blob.htm.ini

This file was deleted.

0 comments on commit 0b0e147

Please sign in to comment.