Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fetch): accept arraybuffer in consume_body #20406

Merged
merged 4 commits into from Mar 24, 2018
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -7,17 +7,22 @@ use dom::bindings::error::{Error, Fallible};
use dom::bindings::reflector::DomObject;
use dom::bindings::root::DomRoot;
use dom::bindings::str::USVString;
use dom::bindings::trace::RootedTraceableBox;
use dom::blob::{Blob, BlobImpl};
use dom::formdata::FormData;
use dom::globalscope::GlobalScope;
use dom::promise::Promise;
use js::jsapi::Heap;
use js::jsapi::JSContext;
use js::jsapi::JSObject;
use js::jsapi::JS_ClearPendingException;
use js::jsapi::JS_ParseJSON;
use js::jsapi::Value as JSValue;
use js::jsval::UndefinedValue;
use js::typedarray::{ArrayBuffer, CreateWith};
use mime::{Mime, TopLevel, SubLevel};
use std::cell::Ref;
use std::ptr;
use std::rc::Rc;
use std::str;
use url::form_urlencoded;
@@ -27,14 +32,16 @@ pub enum BodyType {
Blob,
FormData,
Json,
Text
Text,
ArrayBuffer
}

pub enum FetchedData {
Text(String),
Json(JSValue),
Json(RootedTraceableBox<Heap<JSValue>>),

This comment has been minimized.

@kwonoj

kwonoj Mar 24, 2018

Author Contributor

Updated Json to store RootedTraceableBox<Heap> as suggested. Still it stores JSValue instead of JSVal, by JSVal doesn't meet trait bounds for RootedTraceableBox.

BlobData(DomRoot<Blob>),
FormData(DomRoot<FormData>),
ArrayBuffer(RootedTraceableBox<Heap<*mut JSObject>>),
}

// https://fetch.spec.whatwg.org/#concept-body-consume-body
@@ -83,6 +90,7 @@ pub fn consume_body_with_promise<T: BodyOperations + DomObject>(object: &T,
FetchedData::Json(j) => promise.resolve_native(&j),
FetchedData::BlobData(b) => promise.resolve_native(&b),
FetchedData::FormData(f) => promise.resolve_native(&f),
FetchedData::ArrayBuffer(a) => promise.resolve_native(&a)
};
},
Err(err) => promise.reject_error(err),
@@ -104,6 +112,9 @@ fn run_package_data_algorithm<T: BodyOperations + DomObject>(object: &T,
BodyType::Json => run_json_data_algorithm(cx, bytes),
BodyType::Blob => run_blob_data_algorithm(&global, bytes, mime),
BodyType::FormData => run_form_data_algorithm(&global, bytes, mime),
BodyType::ArrayBuffer => unsafe {
run_array_buffer_data_algorithm(cx, bytes)

This comment has been minimized.

@kwonoj

kwonoj Mar 24, 2018

Author Contributor

now run_array_buffer_data_algorithm is declared as unsafe fn, and this match arm uses unsafe scope.

}
}
}

@@ -126,7 +137,8 @@ fn run_json_data_algorithm(cx: *mut JSContext,
// TODO: See issue #13464. Exception should be thrown instead of cleared.
return Err(Error::Type("Failed to parse JSON".to_string()));
}
Ok(FetchedData::Json(rval.get()))
let rooted_heap = RootedTraceableBox::from_box(Heap::boxed(rval.get()));
Ok(FetchedData::Json(rooted_heap))
}
}

@@ -166,6 +178,17 @@ fn run_form_data_algorithm(root: &GlobalScope, bytes: Vec<u8>, mime: &[u8]) -> F
}
}

#[allow(unsafe_code)]
unsafe fn run_array_buffer_data_algorithm(cx: *mut JSContext, bytes: Vec<u8>) -> Fallible<FetchedData> {
rooted!(in(cx) let mut array_buffer_ptr = ptr::null_mut::<JSObject>());
let arraybuffer = ArrayBuffer::create(cx, CreateWith::Slice(&bytes), array_buffer_ptr.handle_mut());
if arraybuffer.is_err() {
return Err(Error::JSFailed);
}
let rooted_heap = RootedTraceableBox::from_box(Heap::boxed(array_buffer_ptr.get()));
Ok(FetchedData::ArrayBuffer(rooted_heap))
}

pub trait BodyOperations {
fn get_body_used(&self) -> bool;
fn set_body_promise(&self, p: &Rc<Promise>, body_type: BodyType);
@@ -611,6 +611,12 @@ impl RequestMethods for Request {
fn Json(&self) -> Rc<Promise> {
consume_body(self, BodyType::Json)
}

#[allow(unrooted_must_root)]
// https://fetch.spec.whatwg.org/#dom-body-arraybuffer
fn ArrayBuffer(&self) -> Rc<Promise> {
consume_body(self, BodyType::ArrayBuffer)
}
}

impl BodyOperations for Request {
@@ -354,6 +354,12 @@ impl ResponseMethods for Response {
fn Json(&self) -> Rc<Promise> {
consume_body(self, BodyType::Json)
}

#[allow(unrooted_must_root)]
// https://fetch.spec.whatwg.org/#dom-body-arraybuffer
fn ArrayBuffer(&self) -> Rc<Promise> {
consume_body(self, BodyType::ArrayBuffer)
}
}

fn serialize_without_fragment(url: &ServoUrl) -> &str {
@@ -10,7 +10,7 @@
interface Body {
readonly attribute boolean bodyUsed;

// [NewObject] Promise<ArrayBuffer> arrayBuffer();
[NewObject] Promise<ArrayBuffer> arrayBuffer();
[NewObject] Promise<Blob> blob();
[NewObject] Promise<FormData> formData();
[NewObject] Promise<any> json();
@@ -36,33 +36,4 @@
[UTF-16 without BOM decoded as UTF-8 with Response.text()]
expected: FAIL

[UTF-8 with BOM (Response object)]
expected: FAIL

[UTF-8 with BOM (Request object)]
expected: FAIL

[UTF-8 without BOM (Response object)]
expected: FAIL

[UTF-8 without BOM (Request object)]
expected: FAIL

[UTF-16BE with BOM decoded as UTF-8 (Response object)]
expected: FAIL

[UTF-16BE with BOM decoded as UTF-8 (Request object)]
expected: FAIL

[UTF-16LE with BOM decoded as UTF-8 (Response object)]
expected: FAIL

[UTF-16LE with BOM decoded as UTF-8 (Request object)]
expected: FAIL

[UTF-16 without BOM decoded as UTF-8 (Response object)]
expected: FAIL

[UTF-16 without BOM decoded as UTF-8 (Request object)]
expected: FAIL

@@ -6,12 +6,6 @@
[Consume request's body as formData]
expected: FAIL

[Consume empty blob request body as arrayBuffer]
expected: FAIL

[Consume empty text request body as arrayBuffer]
expected: FAIL

[Consume request's body as text]
expected: FAIL
@@ -1,17 +1,11 @@
[request-consume.html]
type: testharness
[Consume String request's body as arrayBuffer]
expected: FAIL
[Consume String request's body as json]
expected: FAIL
[Consume String request's body as formData]
expected: FAIL

[Consume blob response's body as arrayBuffer]
expected: FAIL

[Trying to consume bad JSON text as JSON: 'undefined']
expected: FAIL

@@ -51,9 +45,6 @@
[Consume FormData request's body as FormData]
expected: FAIL

[Consume blob response's body as arrayBuffer]
expected: FAIL
[Trying to consume bad JSON text as JSON: 'undefined']
expected: FAIL

@@ -30,6 +30,3 @@
[Request interface: new Request("") must inherit property "body" with the proper type]
expected: FAIL

[Request interface: new Request("") must inherit property "arrayBuffer()" with the proper type]
expected: FAIL

@@ -1,5 +1,3 @@
[request-structure.html]
type: testharness
[Request has arrayBuffer method]
expected: FAIL

@@ -24,15 +24,9 @@
[Consume fetched response's body as arrayBuffer]
expected: FAIL

[Consume response's body: from text to arrayBuffer]
expected: FAIL
[Consume response's body: from text with correct multipart type to formData]
expected: FAIL
[Consume response's body: from blob to arrayBuffer]
expected: FAIL
[Consume response's body: from blob with correct multipart type to formData]
expected: FAIL

@@ -48,9 +42,6 @@
[Consume response's body: from FormData to arrayBuffer]
expected: FAIL

[Consume response's body: from URLSearchParams to arrayBuffer]
expected: FAIL
[Consume response's body: from stream to blob]
expected: FAIL
@@ -78,9 +69,6 @@
[Consume response's body: from fetch to blob]
expected: FAIL
[Consume response's body: from fetch to arrayBuffer]
expected: FAIL
[Consume response's body: from multipart form data blob to formData]
expected: FAIL

@@ -21,9 +21,6 @@
[Response interface: new Response() must inherit property "body" with the proper type (8)]
expected: FAIL

[Response interface: new Response() must inherit property "arrayBuffer" with the proper type (11)]
expected: FAIL

[Response interface: new Response() must inherit property "body" with the proper type (9)]
expected: FAIL

@@ -45,6 +42,3 @@
[Response interface: new Response() must inherit property "body" with the proper type]
expected: FAIL

[Response interface: new Response() must inherit property "arrayBuffer()" with the proper type]
expected: FAIL

@@ -1,5 +1,3 @@
[response-stream-disturbed-5.html]
type: testharness
[Getting a body reader after consuming as arrayBuffer]
expected: FAIL

This comment has been minimized.

@KiChjang

KiChjang Mar 24, 2018

Member

Shouldn't this file be deleted altogether?

This comment has been minimized.

@jdm

jdm Mar 24, 2018

Member

It will be automatically in the future.


ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.