Auto merge of #20406 - kwonoj:feat-fetch-body-arraybuffer, r=jdm

feat(fetch): accept arraybuffer in consume_body

<!-- Please describe your changes on the following line: -->
Related to https://github.com/servo/servo/issues/20346.

I realized I am not sufficiently knowledgeable about codebases and have high confidence this PR is not ready to be accepted. Raising it as PR early to possibly ask some suggestions around codebases.

If this PR seems unrecoverable by code review, please feel freely close and unassign me from issue 🙏

This PR tries to implement #20346, updating `Body` idl and implements corresponding implementation in `body.rs` for `fetch`. Criteria for changes may includes

- does `run_array_buffer_data_algorithm` implementation is legit for allocating arraybuffer? (probably not)
- does `run_array_buffer_data_algorithm` implementation is acceptable for handling error, by naively returning `Error::JSFailed`?
- there are some number of wpt test started to PASS with this PR. Is this legit side effect, or something incorrect by current implementation?
- etcs, vice versa

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #20346 (github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____
- wpt test has changed in PR, need to be reviewed.

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/20406)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2018-03-24 13:46:20 -04:00 committed by GitHub
commit 23b2f42a36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 39 additions and 269 deletions

View file

@ -7,17 +7,22 @@ use dom::bindings::error::{Error, Fallible};
use dom::bindings::reflector::DomObject; use dom::bindings::reflector::DomObject;
use dom::bindings::root::DomRoot; use dom::bindings::root::DomRoot;
use dom::bindings::str::USVString; use dom::bindings::str::USVString;
use dom::bindings::trace::RootedTraceableBox;
use dom::blob::{Blob, BlobImpl}; use dom::blob::{Blob, BlobImpl};
use dom::formdata::FormData; use dom::formdata::FormData;
use dom::globalscope::GlobalScope; use dom::globalscope::GlobalScope;
use dom::promise::Promise; use dom::promise::Promise;
use js::jsapi::Heap;
use js::jsapi::JSContext; use js::jsapi::JSContext;
use js::jsapi::JSObject;
use js::jsapi::JS_ClearPendingException; use js::jsapi::JS_ClearPendingException;
use js::jsapi::JS_ParseJSON; use js::jsapi::JS_ParseJSON;
use js::jsapi::Value as JSValue; use js::jsapi::Value as JSValue;
use js::jsval::UndefinedValue; use js::jsval::UndefinedValue;
use js::typedarray::{ArrayBuffer, CreateWith};
use mime::{Mime, TopLevel, SubLevel}; use mime::{Mime, TopLevel, SubLevel};
use std::cell::Ref; use std::cell::Ref;
use std::ptr;
use std::rc::Rc; use std::rc::Rc;
use std::str; use std::str;
use url::form_urlencoded; use url::form_urlencoded;
@ -27,14 +32,16 @@ pub enum BodyType {
Blob, Blob,
FormData, FormData,
Json, Json,
Text Text,
ArrayBuffer
} }
pub enum FetchedData { pub enum FetchedData {
Text(String), Text(String),
Json(JSValue), Json(RootedTraceableBox<Heap<JSValue>>),
BlobData(DomRoot<Blob>), BlobData(DomRoot<Blob>),
FormData(DomRoot<FormData>), FormData(DomRoot<FormData>),
ArrayBuffer(RootedTraceableBox<Heap<*mut JSObject>>),
} }
// https://fetch.spec.whatwg.org/#concept-body-consume-body // 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::Json(j) => promise.resolve_native(&j),
FetchedData::BlobData(b) => promise.resolve_native(&b), FetchedData::BlobData(b) => promise.resolve_native(&b),
FetchedData::FormData(f) => promise.resolve_native(&f), FetchedData::FormData(f) => promise.resolve_native(&f),
FetchedData::ArrayBuffer(a) => promise.resolve_native(&a)
}; };
}, },
Err(err) => promise.reject_error(err), 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::Json => run_json_data_algorithm(cx, bytes),
BodyType::Blob => run_blob_data_algorithm(&global, bytes, mime), BodyType::Blob => run_blob_data_algorithm(&global, bytes, mime),
BodyType::FormData => run_form_data_algorithm(&global, bytes, mime), BodyType::FormData => run_form_data_algorithm(&global, bytes, mime),
BodyType::ArrayBuffer => unsafe {
run_array_buffer_data_algorithm(cx, bytes)
}
} }
} }
@ -126,7 +137,8 @@ fn run_json_data_algorithm(cx: *mut JSContext,
// TODO: See issue #13464. Exception should be thrown instead of cleared. // TODO: See issue #13464. Exception should be thrown instead of cleared.
return Err(Error::Type("Failed to parse JSON".to_string())); 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 { pub trait BodyOperations {
fn get_body_used(&self) -> bool; fn get_body_used(&self) -> bool;
fn set_body_promise(&self, p: &Rc<Promise>, body_type: BodyType); fn set_body_promise(&self, p: &Rc<Promise>, body_type: BodyType);

View file

@ -611,6 +611,12 @@ impl RequestMethods for Request {
fn Json(&self) -> Rc<Promise> { fn Json(&self) -> Rc<Promise> {
consume_body(self, BodyType::Json) 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 { impl BodyOperations for Request {

View file

@ -354,6 +354,12 @@ impl ResponseMethods for Response {
fn Json(&self) -> Rc<Promise> { fn Json(&self) -> Rc<Promise> {
consume_body(self, BodyType::Json) 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 { fn serialize_without_fragment(url: &ServoUrl) -> &str {

View file

@ -10,7 +10,7 @@
interface Body { interface Body {
readonly attribute boolean bodyUsed; readonly attribute boolean bodyUsed;
// [NewObject] Promise<ArrayBuffer> arrayBuffer(); [NewObject] Promise<ArrayBuffer> arrayBuffer();
[NewObject] Promise<Blob> blob(); [NewObject] Promise<Blob> blob();
[NewObject] Promise<FormData> formData(); [NewObject] Promise<FormData> formData();
[NewObject] Promise<any> json(); [NewObject] Promise<any> json();

View file

@ -36,33 +36,4 @@
[UTF-16 without BOM decoded as UTF-8 with Response.text()] [UTF-16 without BOM decoded as UTF-8 with Response.text()]
expected: FAIL 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

View file

@ -6,12 +6,6 @@
[Consume request's body as formData] [Consume request's body as formData]
expected: FAIL 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] [Consume request's body as text]
expected: FAIL expected: FAIL

View file

@ -1,17 +1,11 @@
[request-consume.html] [request-consume.html]
type: testharness type: testharness
[Consume String request's body as arrayBuffer]
expected: FAIL
[Consume String request's body as json] [Consume String request's body as json]
expected: FAIL expected: FAIL
[Consume String request's body as formData] [Consume String request's body as formData]
expected: FAIL expected: FAIL
[Consume blob response's body as arrayBuffer]
expected: FAIL
[Trying to consume bad JSON text as JSON: 'undefined'] [Trying to consume bad JSON text as JSON: 'undefined']
expected: FAIL expected: FAIL
@ -51,9 +45,6 @@
[Consume FormData request's body as FormData] [Consume FormData request's body as FormData]
expected: FAIL expected: FAIL
[Consume blob response's body as arrayBuffer]
expected: FAIL
[Trying to consume bad JSON text as JSON: 'undefined'] [Trying to consume bad JSON text as JSON: 'undefined']
expected: FAIL expected: FAIL

View file

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

View file

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

View file

@ -24,15 +24,9 @@
[Consume fetched response's body as arrayBuffer] [Consume fetched response's body as arrayBuffer]
expected: FAIL expected: FAIL
[Consume response's body: from text to arrayBuffer]
expected: FAIL
[Consume response's body: from text with correct multipart type to formData] [Consume response's body: from text with correct multipart type to formData]
expected: FAIL expected: FAIL
[Consume response's body: from blob to arrayBuffer]
expected: FAIL
[Consume response's body: from blob with correct multipart type to formData] [Consume response's body: from blob with correct multipart type to formData]
expected: FAIL expected: FAIL
@ -48,9 +42,6 @@
[Consume response's body: from FormData to arrayBuffer] [Consume response's body: from FormData to arrayBuffer]
expected: FAIL expected: FAIL
[Consume response's body: from URLSearchParams to arrayBuffer]
expected: FAIL
[Consume response's body: from stream to blob] [Consume response's body: from stream to blob]
expected: FAIL expected: FAIL
@ -78,9 +69,6 @@
[Consume response's body: from fetch to blob] [Consume response's body: from fetch to blob]
expected: FAIL expected: FAIL
[Consume response's body: from fetch to arrayBuffer]
expected: FAIL
[Consume response's body: from multipart form data blob to formData] [Consume response's body: from multipart form data blob to formData]
expected: FAIL expected: FAIL

View file

@ -21,9 +21,6 @@
[Response interface: new Response() must inherit property "body" with the proper type (8)] [Response interface: new Response() must inherit property "body" with the proper type (8)]
expected: FAIL 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)] [Response interface: new Response() must inherit property "body" with the proper type (9)]
expected: FAIL expected: FAIL
@ -45,6 +42,3 @@
[Response interface: new Response() must inherit property "body" with the proper type] [Response interface: new Response() must inherit property "body" with the proper type]
expected: FAIL expected: FAIL
[Response interface: new Response() must inherit property "arrayBuffer()" with the proper type]
expected: FAIL

View file

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

View file

@ -1,46 +1,8 @@
[base64.any.worker.html] [base64.any.worker.html]
[data: URL base64 handling: ""]
expected: FAIL
[data: URL base64 handling: "abcd"]
expected: FAIL
[data: URL base64 handling: " abcd"]
expected: FAIL
[data: URL base64 handling: "abcd "]
expected: FAIL
[data: URL base64 handling: "ab"]
expected: FAIL
[data: URL base64 handling: "abc"]
expected: FAIL
[data: URL base64 handling: "ab="] [data: URL base64 handling: "ab="]
expected: FAIL expected: FAIL
[data: URL base64 handling: "ab=="]
expected: FAIL
[data: URL base64 handling: "abc="]
expected: FAIL
[data: URL base64 handling: "ab\\tcd"]
expected: FAIL
[data: URL base64 handling: "ab\\ncd"]
expected: FAIL
[data: URL base64 handling: "ab\\fcd"] [data: URL base64 handling: "ab\\fcd"]
expected: FAIL expected: FAIL
[data: URL base64 handling: "ab\\rcd"]
expected: FAIL
[data: URL base64 handling: "ab cd"]
expected: FAIL
[data: URL base64 handling: "ab\\t\\n\\f\\r cd"] [data: URL base64 handling: "ab\\t\\n\\f\\r cd"]
expected: FAIL expected: FAIL
@ -50,74 +12,12 @@
[data: URL base64 handling: "ab\\t\\n\\f\\r =\\t\\n\\f\\r =\\t\\n\\f\\r "] [data: URL base64 handling: "ab\\t\\n\\f\\r =\\t\\n\\f\\r =\\t\\n\\f\\r "]
expected: FAIL expected: FAIL
[data: URL base64 handling: "/A"]
expected: FAIL
[data: URL base64 handling: "//A"]
expected: FAIL
[data: URL base64 handling: "///A"]
expected: FAIL
[data: URL base64 handling: "A/"]
expected: FAIL
[data: URL base64 handling: "AA/"]
expected: FAIL
[data: URL base64 handling: "AAA/"]
expected: FAIL
[data: URL base64 handling: "YQ"]
expected: FAIL
[data: URL base64 handling: "YR"]
expected: FAIL
[base64.any.html] [base64.any.html]
[data: URL base64 handling: ""]
expected: FAIL
[data: URL base64 handling: "abcd"]
expected: FAIL
[data: URL base64 handling: " abcd"]
expected: FAIL
[data: URL base64 handling: "abcd "]
expected: FAIL
[data: URL base64 handling: "ab"]
expected: FAIL
[data: URL base64 handling: "abc"]
expected: FAIL
[data: URL base64 handling: "ab="] [data: URL base64 handling: "ab="]
expected: FAIL expected: FAIL
[data: URL base64 handling: "ab=="]
expected: FAIL
[data: URL base64 handling: "abc="]
expected: FAIL
[data: URL base64 handling: "ab\\tcd"]
expected: FAIL
[data: URL base64 handling: "ab\\ncd"]
expected: FAIL
[data: URL base64 handling: "ab\\fcd"] [data: URL base64 handling: "ab\\fcd"]
expected: FAIL expected: FAIL
[data: URL base64 handling: "ab\\rcd"]
expected: FAIL
[data: URL base64 handling: "ab cd"]
expected: FAIL
[data: URL base64 handling: "ab\\t\\n\\f\\r cd"] [data: URL base64 handling: "ab\\t\\n\\f\\r cd"]
expected: FAIL expected: FAIL
@ -127,27 +27,4 @@
[data: URL base64 handling: "ab\\t\\n\\f\\r =\\t\\n\\f\\r =\\t\\n\\f\\r "] [data: URL base64 handling: "ab\\t\\n\\f\\r =\\t\\n\\f\\r =\\t\\n\\f\\r "]
expected: FAIL expected: FAIL
[data: URL base64 handling: "/A"]
expected: FAIL
[data: URL base64 handling: "//A"]
expected: FAIL
[data: URL base64 handling: "///A"]
expected: FAIL
[data: URL base64 handling: "A/"]
expected: FAIL
[data: URL base64 handling: "AA/"]
expected: FAIL
[data: URL base64 handling: "AAA/"]
expected: FAIL
[data: URL base64 handling: "YQ"]
expected: FAIL
[data: URL base64 handling: "YR"]
expected: FAIL

View file

@ -14,12 +14,8 @@
["data:,%FF"] ["data:,%FF"]
expected: FAIL expected: FAIL
["data:text/plain,X"]
expected: FAIL
["data:text/plain ,X"] ["data:text/plain ,X"]
expected: FAIL expected: FAIL
["data:text/plain%20,X"] ["data:text/plain%20,X"]
expected: FAIL expected: FAIL
@ -29,9 +25,6 @@
["data:text/plain%0C,X"] ["data:text/plain%0C,X"]
expected: FAIL expected: FAIL
["data:text/plain;,X"]
expected: FAIL
["data:;x=x;charset=x,X"] ["data:;x=x;charset=x,X"]
expected: FAIL expected: FAIL
@ -44,12 +37,6 @@
["data:text/plain;Charset=UTF-8,%C2%B1"] ["data:text/plain;Charset=UTF-8,%C2%B1"]
expected: FAIL expected: FAIL
["data:image/gif,%C2%B1"]
expected: FAIL
["data:IMAGE/gif,%C2%B1"]
expected: FAIL
["data:IMAGE/gif;hi=x,%C2%B1"] ["data:IMAGE/gif;hi=x,%C2%B1"]
expected: FAIL expected: FAIL
@ -89,15 +76,6 @@
["data:X,X"] ["data:X,X"]
expected: FAIL expected: FAIL
["data:image/png,X X"]
expected: FAIL
["data:application/xml,X X"]
expected: FAIL
["data:unknown/unknown,X X"]
expected: FAIL
["data:text/plain;a=\\",\\",X"] ["data:text/plain;a=\\",\\",X"]
expected: FAIL expected: FAIL
@ -107,18 +85,12 @@
["data:;base64;base64,WA"] ["data:;base64;base64,WA"]
expected: FAIL expected: FAIL
["data:x/x;base64;base64,WA"]
expected: FAIL
["data:x/x;base64;charset=x,WA"] ["data:x/x;base64;charset=x,WA"]
expected: FAIL expected: FAIL
["data:x/x;base64;charset=x;base64,WA"] ["data:x/x;base64;charset=x;base64,WA"]
expected: FAIL expected: FAIL
["data:x/x;base64;base64x,WA"]
expected: FAIL
["data:;base64,W%20A"] ["data:;base64,W%20A"]
expected: FAIL expected: FAIL
@ -188,15 +160,6 @@
["data:;CHARSET=\\"X\\",X"] ["data:;CHARSET=\\"X\\",X"]
expected: FAIL expected: FAIL
["data:application/javascript,X X"]
expected: FAIL
["data:text/javascript,X X"]
expected: FAIL
["data:text/plain,X X"]
expected: FAIL
[processing.any.html] [processing.any.html]
["data://test/,X"] ["data://test/,X"]
@ -214,9 +177,6 @@
["data:,%FF"] ["data:,%FF"]
expected: FAIL expected: FAIL
["data:text/plain,X"]
expected: FAIL
["data:text/plain ,X"] ["data:text/plain ,X"]
expected: FAIL expected: FAIL
@ -229,9 +189,6 @@
["data:text/plain%0C,X"] ["data:text/plain%0C,X"]
expected: FAIL expected: FAIL
["data:text/plain;,X"]
expected: FAIL
["data:;x=x;charset=x,X"] ["data:;x=x;charset=x,X"]
expected: FAIL expected: FAIL
@ -244,12 +201,6 @@
["data:text/plain;Charset=UTF-8,%C2%B1"] ["data:text/plain;Charset=UTF-8,%C2%B1"]
expected: FAIL expected: FAIL
["data:image/gif,%C2%B1"]
expected: FAIL
["data:IMAGE/gif,%C2%B1"]
expected: FAIL
["data:IMAGE/gif;hi=x,%C2%B1"] ["data:IMAGE/gif;hi=x,%C2%B1"]
expected: FAIL expected: FAIL
@ -289,15 +240,6 @@
["data:X,X"] ["data:X,X"]
expected: FAIL expected: FAIL
["data:image/png,X X"]
expected: FAIL
["data:application/xml,X X"]
expected: FAIL
["data:unknown/unknown,X X"]
expected: FAIL
["data:text/plain;a=\\",\\",X"] ["data:text/plain;a=\\",\\",X"]
expected: FAIL expected: FAIL
@ -307,18 +249,12 @@
["data:;base64;base64,WA"] ["data:;base64;base64,WA"]
expected: FAIL expected: FAIL
["data:x/x;base64;base64,WA"]
expected: FAIL
["data:x/x;base64;charset=x,WA"] ["data:x/x;base64;charset=x,WA"]
expected: FAIL expected: FAIL
["data:x/x;base64;charset=x;base64,WA"] ["data:x/x;base64;charset=x;base64,WA"]
expected: FAIL expected: FAIL
["data:x/x;base64;base64x,WA"]
expected: FAIL
["data:;base64,W%20A"] ["data:;base64,W%20A"]
expected: FAIL expected: FAIL
@ -388,12 +324,3 @@
["data:;CHARSET=\\"X\\",X"] ["data:;CHARSET=\\"X\\",X"]
expected: FAIL expected: FAIL
["data:application/javascript,X X"]
expected: FAIL
["data:text/javascript,X X"]
expected: FAIL
["data:text/plain,X X"]
expected: FAIL