diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2018-03-24 13:46:20 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-24 13:46:20 -0400 |
commit | 23b2f42a368cdc68548310e79b31306f40f95553 (patch) | |
tree | 289e52ae449e9a101295e275f5d011288835d46c | |
parent | 2d789646345a21c32f9bed0f7380526469384238 (diff) | |
parent | 80304a133f66ba4bda0070d29a36043d8f4b4334 (diff) | |
download | servo-23b2f42a368cdc68548310e79b31306f40f95553.tar.gz servo-23b2f42a368cdc68548310e79b31306f40f95553.zip |
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 -->
14 files changed, 39 insertions, 269 deletions
diff --git a/components/script/body.rs b/components/script/body.rs index 21c4d49b58c..35ee2dae2c6 100644 --- a/components/script/body.rs +++ b/components/script/body.rs @@ -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>>), 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) + } } } @@ -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); diff --git a/components/script/dom/request.rs b/components/script/dom/request.rs index 85998533ae9..b3bb2f1afe1 100644 --- a/components/script/dom/request.rs +++ b/components/script/dom/request.rs @@ -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 { diff --git a/components/script/dom/response.rs b/components/script/dom/response.rs index e4af7f48187..ff806a5bf8f 100644 --- a/components/script/dom/response.rs +++ b/components/script/dom/response.rs @@ -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 { diff --git a/components/script/dom/webidls/Body.webidl b/components/script/dom/webidls/Body.webidl index bb7aa5c6859..1ea45746e73 100644 --- a/components/script/dom/webidls/Body.webidl +++ b/components/script/dom/webidls/Body.webidl @@ -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(); diff --git a/tests/wpt/metadata/fetch/api/basic/text-utf8.html.ini b/tests/wpt/metadata/fetch/api/basic/text-utf8.html.ini index 890e7cd7f3c..3648d23af2a 100644 --- a/tests/wpt/metadata/fetch/api/basic/text-utf8.html.ini +++ b/tests/wpt/metadata/fetch/api/basic/text-utf8.html.ini @@ -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 diff --git a/tests/wpt/metadata/fetch/api/request/request-consume-empty.html.ini b/tests/wpt/metadata/fetch/api/request/request-consume-empty.html.ini index 8ac3c17618d..0f807052fc9 100644 --- a/tests/wpt/metadata/fetch/api/request/request-consume-empty.html.ini +++ b/tests/wpt/metadata/fetch/api/request/request-consume-empty.html.ini @@ -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 diff --git a/tests/wpt/metadata/fetch/api/request/request-consume.html.ini b/tests/wpt/metadata/fetch/api/request/request-consume.html.ini index c409d5b0189..611766b22ac 100644 --- a/tests/wpt/metadata/fetch/api/request/request-consume.html.ini +++ b/tests/wpt/metadata/fetch/api/request/request-consume.html.ini @@ -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 diff --git a/tests/wpt/metadata/fetch/api/request/request-idl.html.ini b/tests/wpt/metadata/fetch/api/request/request-idl.html.ini index dec37325cb8..f233e7214f9 100644 --- a/tests/wpt/metadata/fetch/api/request/request-idl.html.ini +++ b/tests/wpt/metadata/fetch/api/request/request-idl.html.ini @@ -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 - diff --git a/tests/wpt/metadata/fetch/api/request/request-structure.html.ini b/tests/wpt/metadata/fetch/api/request/request-structure.html.ini index aa3c47b9ad4..5e5f5ed9b56 100644 --- a/tests/wpt/metadata/fetch/api/request/request-structure.html.ini +++ b/tests/wpt/metadata/fetch/api/request/request-structure.html.ini @@ -1,5 +1,3 @@ [request-structure.html] type: testharness - [Request has arrayBuffer method] - expected: FAIL diff --git a/tests/wpt/metadata/fetch/api/response/response-consume.html.ini b/tests/wpt/metadata/fetch/api/response/response-consume.html.ini index de25ae08c7a..e3efe47c53b 100644 --- a/tests/wpt/metadata/fetch/api/response/response-consume.html.ini +++ b/tests/wpt/metadata/fetch/api/response/response-consume.html.ini @@ -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 diff --git a/tests/wpt/metadata/fetch/api/response/response-idl.html.ini b/tests/wpt/metadata/fetch/api/response/response-idl.html.ini index d94659d4f16..e0eb81482e9 100644 --- a/tests/wpt/metadata/fetch/api/response/response-idl.html.ini +++ b/tests/wpt/metadata/fetch/api/response/response-idl.html.ini @@ -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 - diff --git a/tests/wpt/metadata/fetch/api/response/response-stream-disturbed-5.html.ini b/tests/wpt/metadata/fetch/api/response/response-stream-disturbed-5.html.ini index 31c366a0035..f70ac4f2ddc 100644 --- a/tests/wpt/metadata/fetch/api/response/response-stream-disturbed-5.html.ini +++ b/tests/wpt/metadata/fetch/api/response/response-stream-disturbed-5.html.ini @@ -1,5 +1,3 @@ [response-stream-disturbed-5.html] type: testharness - [Getting a body reader after consuming as arrayBuffer] - expected: FAIL diff --git a/tests/wpt/metadata/fetch/data-urls/base64.any.js.ini b/tests/wpt/metadata/fetch/data-urls/base64.any.js.ini index 2aa2f25c9a1..7b7a199b2bd 100644 --- a/tests/wpt/metadata/fetch/data-urls/base64.any.js.ini +++ b/tests/wpt/metadata/fetch/data-urls/base64.any.js.ini @@ -1,46 +1,8 @@ [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="] 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"] 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"] expected: FAIL @@ -50,74 +12,12 @@ [data: URL base64 handling: "ab\\t\\n\\f\\r =\\t\\n\\f\\r =\\t\\n\\f\\r "] 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] - [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="] 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"] 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"] expected: FAIL @@ -127,27 +27,4 @@ [data: URL base64 handling: "ab\\t\\n\\f\\r =\\t\\n\\f\\r =\\t\\n\\f\\r "] 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 diff --git a/tests/wpt/metadata/fetch/data-urls/processing.any.js.ini b/tests/wpt/metadata/fetch/data-urls/processing.any.js.ini index 1660d360fd4..1186217c000 100644 --- a/tests/wpt/metadata/fetch/data-urls/processing.any.js.ini +++ b/tests/wpt/metadata/fetch/data-urls/processing.any.js.ini @@ -14,12 +14,8 @@ ["data:,%FF"] expected: FAIL - ["data:text/plain,X"] - expected: FAIL - ["data:text/plain ,X"] expected: FAIL - ["data:text/plain%20,X"] expected: FAIL @@ -29,9 +25,6 @@ ["data:text/plain%0C,X"] expected: FAIL - ["data:text/plain;,X"] - expected: FAIL - ["data:;x=x;charset=x,X"] expected: FAIL @@ -44,12 +37,6 @@ ["data:text/plain;Charset=UTF-8,%C2%B1"] expected: FAIL - ["data:image/gif,%C2%B1"] - expected: FAIL - - ["data:IMAGE/gif,%C2%B1"] - expected: FAIL - ["data:IMAGE/gif;hi=x,%C2%B1"] expected: FAIL @@ -89,15 +76,6 @@ ["data:X,X"] 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"] expected: FAIL @@ -107,18 +85,12 @@ ["data:;base64;base64,WA"] expected: FAIL - ["data:x/x;base64;base64,WA"] - expected: FAIL - ["data:x/x;base64;charset=x,WA"] expected: FAIL ["data:x/x;base64;charset=x;base64,WA"] expected: FAIL - ["data:x/x;base64;base64x,WA"] - expected: FAIL - ["data:;base64,W%20A"] expected: FAIL @@ -188,15 +160,6 @@ ["data:;CHARSET=\\"X\\",X"] 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] ["data://test/,X"] @@ -214,9 +177,6 @@ ["data:,%FF"] expected: FAIL - ["data:text/plain,X"] - expected: FAIL - ["data:text/plain ,X"] expected: FAIL @@ -229,9 +189,6 @@ ["data:text/plain%0C,X"] expected: FAIL - ["data:text/plain;,X"] - expected: FAIL - ["data:;x=x;charset=x,X"] expected: FAIL @@ -244,12 +201,6 @@ ["data:text/plain;Charset=UTF-8,%C2%B1"] expected: FAIL - ["data:image/gif,%C2%B1"] - expected: FAIL - - ["data:IMAGE/gif,%C2%B1"] - expected: FAIL - ["data:IMAGE/gif;hi=x,%C2%B1"] expected: FAIL @@ -289,15 +240,6 @@ ["data:X,X"] 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"] expected: FAIL @@ -307,18 +249,12 @@ ["data:;base64;base64,WA"] expected: FAIL - ["data:x/x;base64;base64,WA"] - expected: FAIL - ["data:x/x;base64;charset=x,WA"] expected: FAIL ["data:x/x;base64;charset=x;base64,WA"] expected: FAIL - ["data:x/x;base64;base64x,WA"] - expected: FAIL - ["data:;base64,W%20A"] expected: FAIL @@ -388,12 +324,3 @@ ["data:;CHARSET=\\"X\\",X"] expected: FAIL - ["data:application/javascript,X X"] - expected: FAIL - - ["data:text/javascript,X X"] - expected: FAIL - - ["data:text/plain,X X"] - expected: FAIL - |